Home All Groups Group Topic Archive Search About

Problem using default xml namespace and selectsignlenode/selectnod

Author
23 Aug 2006 1:54 PM
rrm
Hi

I am verifying a SLD implementation using xml, however I have som problem
using xpath and default namespaces in vb.net

Currently I have the following xml
<?xml version="1.0"?>
<StyledLayerDescriptor version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/sld
http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd"
xmlns:sld="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>TestName</Name>
<Title>TestTitle</Title>
<Abstract>TestAbstract</Abstract>
</StyledLayerDescriptor>

and the following code to read it:
Dim writer As Xml.XmlTextWriter = New Xml.XmlTextWriter(file, Nothing)
doc.Load(file)
where file is the location of the xml and doc is a xmldocument objekt

I ask with:
Dim r As Xml.XmlNode = doc.SelectSingleNode("/StyledLayerDescriptor/Name")
Console.WriteLine("Name " & r.InnerXml)

The return value is nothing, any clue of why??? If I dont use default
namespace, then it can be found.

rrm

Author
23 Aug 2006 2:45 PM
Martin Honnen
rrm wrote:


> Dim r As Xml.XmlNode = doc.SelectSingleNode("/StyledLayerDescriptor/Name")


> The return value is nothing, any clue of why??? If I dont use default
> namespace, then it can be found.

You need a namespace manager e.g.
   Dim namespaceManager As XmlNamespaceManager =_
     new XmlNamespaceManager(doc.NameTable)
   namespaceManager.AddNamespace("pf", "default namespace URI here")
then e.g.
   doc.SelectSingleNode("/pf:StyledLayerDescriptor/pf:Name",_
     namespaceManager)

--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
Author
24 Aug 2006 8:03 AM
rrm
Hi

> You need a namespace manager e.g.
>    Dim namespaceManager As XmlNamespaceManager =_
>      new XmlNamespaceManager(doc.NameTable)
>    namespaceManager.AddNamespace("pf", "default namespace URI here")
> then e.g.
>    doc.SelectSingleNode("/pf:StyledLayerDescriptor/pf:Name",_
>      namespaceManager)

ok

I found an error in my posting: xmlns:sld=... should be xmlns=...
<StyledLayerDescriptor version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/sld
http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd"
xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Would that help me to avoid using namespace "ns:" in selectsinglenode???

Are there any different in the way dotnet 1 and dotnet 2 handles this??
Author
26 Aug 2006 4:36 PM
Martin Honnen
rrm wrote:


> I found an error in my posting: xmlns:sld=... should be xmlns=...


> Would that help me to avoid using namespace "ns:" in selectsinglenode???

You need to use a namespace manager if you want to select elements or
attributes in a namespace. An XPath (1.0) expression in the form of e.g.
   element-name
only selects elements of that name in _no_ namespace. If your elements
are in a namespace then you need a namespace manager that binds a prefix
to the namespace URI and then you need to use that prefix in the XPath
expression e.g.
   pf:element-name

See also "Namespaces in XPath Expressions" in
<http://msdn2.microsoft.com/en-us/library/d271ytdx.aspx>


--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/