Xpath with namespaces

Hello Everyone,

I am having an issue with xml and namespaces, at least I think it is 
namespaces.  When I use namespaces, I cannot use SelectSingleNode / 
SelectNodes as they always return 0/Nothing respectively.  Anyone have any 
thoughts?

Thanks in advance

VB.Net 2005
WinXP Sp2



Test code:

    Dim objXML As System.Xml.XmlDocument

    'Test XML 1   ==> Always returns 0
    objXML = New System.Xml.XmlDocument
    objXML.Load(Application.StartupPath & "\xmlfile1.xml")
    MsgBox(objXML.SelectNodes("top/middle").Count)

    'Test XML 2   ==> Always returns expected results
    objXML = New System.Xml.XmlDocument
    objXML.Load(Application.StartupPath & "\xmlfile2.xml")
    MsgBox(objXML.SelectNodes("top/middle").Count)

XML File 1
<?xml version="1.0" encoding="utf-8" ?>
<top xmlns="urn:bob" xmlns:firstns="urn:firstns" 
xmlns:secondns="urn:secondns">
  <middle firstns:name="Entry1" secondns:value="Value1">Some Data 
Here</middle>
</top>


XML File 2
<?xml version="1.0" encoding="utf-8" ?>
<top>
  <middle name="Entry1" value="Value1">Some Data Here</middle>
</top>


0
amdrit (18)
10/11/2006 7:19:46 PM
dotnet.xml 7266 articles. 0 followers. Follow

6 Replies
1140 Views

Similar Articles

[PageSpeed] 18

* AMDRIT wrote in microsoft.public.dotnet.xml:
>I am having an issue with xml and namespaces, at least I think it is 
>namespaces.  When I use namespaces, I cannot use SelectSingleNode / 
>SelectNodes as they always return 0/Nothing respectively.  Anyone have any 
>thoughts?

You have to declare your namespaces first using e.g. a custom
XmlNamespaceManager or you have to rewrite your expressions to
avoid them like

  //foo:bar -> //*[local-name() = 'bar' namespace-uri() = '...']
-- 
Bj�rn H�hrmann � mailto:bjoern@hoehrmann.de � http://bjoern.hoehrmann.de
Weinh. Str. 22 � Telefon: +49(0)621/4309674 � http://www.bjoernsworld.de
68309 Mannheim � PGP Pub. KeyID: 0xA4357E78 � http://www.websitedev.de/ 
0
bjoern1 (135)
10/11/2006 7:44:25 PM
I appreciate your response, however it is leading me nowhere.  I have 
discovered XMLNamespaceManager prior to posting, the docs are not clear how 
this resolved my issue.

First, I am not attempting to get to the attributes yet, merely attempting 
to count the number of 'middle' nodes in 'top'.  So I do not see how I would 
change my xpath statement to reflect namespaces.

Second, when I create a document from scratch using these namespaces and 
attempt to xpath through the document I am not having any issues.  Any ideas 
why?

Sample Code to Count Nodes (XML Loaded from File)

    Dim objXML As System.Xml.XmlDocument
    Dim objNSMGR As Xml.XmlNamespaceManager

    objXML = New System.Xml.XmlDocument
    objXML.Load(Application.StartupPath & "\xmlfile1.xml")

    objNSMGR = New System.Xml.XmlNamespaceManager(objXML.NameTable)

    objNSMGR.AddNamespace("firstns", "urn:firstns")
    objNSMGR.AddNamespace("secondns", "urn:secondns")

    MsgBox(objXML.SelectNodes("top/middle", objNSMGR).Count)

Sample Code to create an query XML

    Dim objXML As System.Xml.XmlDocument
    Dim objNode As System.Xml.XmlNode
    objXML = New System.Xml.XmlDocument

    'Create root node
    objNode = objXML.CreateNode(Xml.XmlNodeType.Element, "top", "")

    'create namespaces
    objNode.Attributes.Append(objXML.CreateAttribute("xmlns"))
    objNode.Attributes.GetNamedItem("xmlns").Value = "urn:bob"

    objNode.Attributes.Append(objXML.CreateAttribute("xmlns:firstns"))
    objNode.Attributes.GetNamedItem("xmlns:firstns").Value = "urn:firstns"

    objNode.Attributes.Append(objXML.CreateAttribute("xmlns:secondns"))
    objNode.Attributes.GetNamedItem("xmlns:secondns").Value = "urn:secondns"

    'Append root node to #document
    objXML.AppendChild(objNode)

    'Create first child node
    objNode = objXML.CreateNode(Xml.XmlNodeType.Element, "middle", "")

    'Create first attribute with namespace-uri
    objNode.Attributes.Append(objXML.CreateAttribute("firstns:name", 
"urn:firstns"))
    objNode.Attributes.GetNamedItem("firstns:name").Value = "test"

    'Append node to root node
    objXML.SelectSingleNode("top").AppendChild(objNode)

    'Count all occurances of middle in top
    MsgBox(objXML.SelectNodes("top/middle").Count)


"Bjoern Hoehrmann" <bjoern@hoehrmann.de> wrote in message 
news:laiqi21nju0oceuqsot3i4o9qe5h0k55qi@hive.bjoern.hoehrmann.de...
>* AMDRIT wrote in microsoft.public.dotnet.xml:
>>I am having an issue with xml and namespaces, at least I think it is
>>namespaces.  When I use namespaces, I cannot use SelectSingleNode /
>>SelectNodes as they always return 0/Nothing respectively.  Anyone have any
>>thoughts?
>
> You have to declare your namespaces first using e.g. a custom
> XmlNamespaceManager or you have to rewrite your expressions to
> avoid them like
>
>  //foo:bar -> //*[local-name() = 'bar' namespace-uri() = '...']
> -- 
> Bj�rn H�hrmann � mailto:bjoern@hoehrmann.de � http://bjoern.hoehrmann.de
> Weinh. Str. 22 � Telefon: +49(0)621/4309674 � http://www.bjoernsworld.de
> 68309 Mannheim � PGP Pub. KeyID: 0xA4357E78 � http://www.websitedev.de/ 


0
amdrit (18)
10/11/2006 8:54:59 PM
* AMDRIT wrote in microsoft.public.dotnet.xml:
>I appreciate your response, however it is leading me nowhere.  I have 
>discovered XMLNamespaceManager prior to posting, the docs are not clear how 
>this resolved my issue.

Any and all elements and attributes in an XML document have a name-
space name and a local name associated with it, in your example

  <?xml version="1.0" encoding="utf-8" ?>
  <top xmlns="urn:bob" xmlns:firstns="urn:firstns" 
  xmlns:secondns="urn:secondns">
    <middle firstns:name="Entry1" secondns:value="Value1">Some Data 
  Here</middle>
  </top>

you have

  <{urn:bob}top>
    <{urn:bob}middle {urn:firstns}name="Entry1"
                     {urn:secondns}value="Value1">Some Data here
  ...

Your XPath expression "top/middle" ignores elements in the 'urn:bob'
namespace, it matches middle elements in no namespace that have a parent
top element in no namespace. In your example, the elements are in a
namespace however. You have to specify this namespace in the XPath, like

  "foo:top/foo:middle"

and the prefix "foo" needs to be bound to "urn:bob"; XmlNamespace-
Manager does just that.
-- 
Bj�rn H�hrmann � mailto:bjoern@hoehrmann.de � http://bjoern.hoehrmann.de
Weinh. Str. 22 � Telefon: +49(0)621/4309674 � http://www.bjoernsworld.de
68309 Mannheim � PGP Pub. KeyID: 0xA4357E78 � http://www.websitedev.de/ 
0
bjoern1 (135)
10/12/2006 8:57:14 AM

AMDRIT wrote:


>     Dim objXML As System.Xml.XmlDocument
>     Dim objNSMGR As Xml.XmlNamespaceManager
> 
>     objXML = New System.Xml.XmlDocument
>     objXML.Load(Application.StartupPath & "\xmlfile1.xml")
> 
>     objNSMGR = New System.Xml.XmlNamespaceManager(objXML.NameTable)
> 
>     objNSMGR.AddNamespace("firstns", "urn:firstns")
>     objNSMGR.AddNamespace("secondns", "urn:secondns")
> 
>     MsgBox(objXML.SelectNodes("top/middle", objNSMGR).Count)

You have to use the prefixes in your XPath expressions to qualify 
element or attributes and you need a prefix for the default namespace 
the XML has e.g.
   objNSMGR.AddNamespace("bb", "urn:bob")

   objXML.SelectNodes("bb:top/bb:middle", objMSNGR).Count

See <http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616> 
for some more explanation.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
0
mahotrash (1778)
10/12/2006 12:10:09 PM

AMDRIT wrote:


> Second, when I create a document from scratch using these namespaces and 
> attempt to xpath through the document I am not having any issues.  

>     Dim objXML As System.Xml.XmlDocument
>     Dim objNode As System.Xml.XmlNode
>     objXML = New System.Xml.XmlDocument
> 
>     'Create root node
>     objNode = objXML.CreateNode(Xml.XmlNodeType.Element, "top", "")

Yes, you have issues, your are _not_ creating the element in any 
namespace. With the DOM the namespace an element or attribute belongs to 
is determined when it is created so your code creates an element named 
"top" in no namespace.

If you serialized your XML then you would see that the top element is in 
no namespace. If you want to use the DOM to create an element in a 
certain namespace then you can do e.g.

   XmlElement topElement = objXML.CreateElement("top", "urn:bob")

that is you use a method that takes a namespace URI and you pass in the 
namespace URI of the namespace you want the element to belong to.

-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
0
mahotrash (1778)
10/12/2006 12:16:33 PM
Thank you for the clear response.  I am off and running again.

"Martin Honnen" wrote:

> 
> 
> AMDRIT wrote:
> 
> 
> >     Dim objXML As System.Xml.XmlDocument
> >     Dim objNSMGR As Xml.XmlNamespaceManager
> > 
> >     objXML = New System.Xml.XmlDocument
> >     objXML.Load(Application.StartupPath & "\xmlfile1.xml")
> > 
> >     objNSMGR = New System.Xml.XmlNamespaceManager(objXML.NameTable)
> > 
> >     objNSMGR.AddNamespace("firstns", "urn:firstns")
> >     objNSMGR.AddNamespace("secondns", "urn:secondns")
> > 
> >     MsgBox(objXML.SelectNodes("top/middle", objNSMGR).Count)
> 
> You have to use the prefixes in your XPath expressions to qualify 
> element or attributes and you need a prefix for the default namespace 
> the XML has e.g.
>    objNSMGR.AddNamespace("bb", "urn:bob")
> 
>    objXML.SelectNodes("bb:top/bb:middle", objMSNGR).Count
> 
> See <http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616> 
> for some more explanation.
> 
> 
> -- 
> 
> 	Martin Honnen --- MVP XML
> 	http://JavaScript.FAQTs.com/
> 
0
AMDIRT (1)
10/12/2006 2:20:02 PM
Reply:

Similar Artilces:

What is the best way to remove XML namespace
Hi, I am trying to remove the namespace in the root node in the following XML, <ordersHistory xmlns="http://tempuri.org/Orders.xsd"> <order number="PO-1" added="12/12/2002"> <client name="Microsoft"> <address country="USA" city="Washington" /> <notes> Produces a great framework for developing applications. </notes> </client> <items> <item id="1" name="Windows XP Professional" qty="2"...

XPath
Using XPath I am querying parts of XML document, " get quoted by \ - is it possible to specify not to quote " with \? <RootElement> <Element Attribute="SomeValue" /> </RootElement> XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNode xmlNode = xmlDoc.SelectSingleNode("//RootElement/Element") Print xmlNode.OuterXml <Element Attribute=\"SomeValue\"/> where I was expecting to get <Element Attribute="SomeValue"/> TIA Dennis wrote: > Using XPath I am querying parts of XML document,...

Getting a specific parent of any node via xpath navigation
I am storing my site structure as such: <menuItems> <menuItem> <pageID>1</pageID> </menuItem> <menuItem> <pageID>2</pageID> <menuItem> <pageID>4</pageID> <menuItem> <pageID>5</pageID> <menuItem> <pageID>6</pageID> </menuItem> </menuItem> </menuItem> </menuItem> <menuItem> <pageID>3</pageID> ...

XPath pb
Hi In infopath, I use a XPath expression to get some nodes : var oEMailNodeList = XDocument.DOM.selectNodes("//Message/EMail"); My problem is that this method returns 0 items. Here is the DOM.xml property : <?xml version="1.0" ?> <?mso-infoPathSolution productVersion=\"11.0.5329\" PIVersion=\"1.0.0.0\" href=\"file:///C:\Documents%20and%20Settings\t-stevbe\Local%20Settings\Appli cation%20Data\Microsoft\InfoPath\7ab6e8af39fc42e2\manifest.xsf\" solutionVersion=\"1.0.0.9\" initialView=\"messagesList\" ?> <?...

cannot load schema for the namespace
Hi, I am trying to validate an XML file using the XmlValidatingReader and XmlUrlResolver. The code for which goes like this: // Read the XML file. XmlTextReader xmlReader = new XmlTextReader( _PathToXMLDoc); XmlValidatingReader valReader = new XmlValidatingReader(xmlReader); valReader.ValidationType = ValidationType.Schema; XmlResolver xRes = new XmlUrlResolver(); valReader.XmlResolver = xRes; valReader.ValidationEventHandler += new ValidationEventHandler(this.ValidationCallBack); while(valReader.Read()) { } I have the XML document which refers to a xsd file located on a di...

How to pass a Nodeset to an XPath extension function?
I am using the .NET framework v2.0.40607 with Visual C# Express and I would like to create some custom XPath functions to use directly in a XSLT stylesheet. I called System.Xml.Query.XmlArgumentList.AddExtensionObject(string, object) to register the functions I've written. The functions that take a String, Boolean or Number parameter work perfectly. However, I do not know how to create a function that will accept a Nodeset as a parameter. I tried writting a function taking a XPathNavigator object, but it does not work for all XPath queries that return a Nodeset: I get an error &qu...

XPath Validation
I'm looking for a way to Validate an XPath prior to executing the expression, without using a try catch clause. For example: I'm looking for something that would tell me that "\some/Xpath\to\Node" is invalid... Any Ideas? Hi, As far as I know, the .NET framework could not validate XPath directly with class library. The only thing we can do is to use try/catch block. Or you can try to find some 3rd-party tool such as XML spy to validate it. HTH. Kevin Yu ======= "This posting is provided "AS IS" with no warranties, and confers no rights." Thanks K...

Xpath with namespaces
Hello Everyone, I am having an issue with xml and namespaces, at least I think it is namespaces. When I use namespaces, I cannot use SelectSingleNode / SelectNodes as they always return 0/Nothing respectively. Anyone have any thoughts? Thanks in advance VB.Net 2005 WinXP Sp2 Test code: Dim objXML As System.Xml.XmlDocument 'Test XML 1 ==> Always returns 0 objXML = New System.Xml.XmlDocument objXML.Load(Application.StartupPath & "\xmlfile1.xml") MsgBox(objXML.SelectNodes("top/middle").Count) 'Test XML 2 ==> Always re...

Tricky XPath query
Hi all, I've just changed the structure of my XML file and now I need to construct an XPathNavigator object that uses tags from two different levels in the XML file. For example: <someTag> <innerTagA> <innerInnerTagA> <innerInnerInnerTagA> Some Data That Belongs Here </innerInnerInnerTagA> <innerInnerInnerTagB> Some Data that Belongs More Specifically Here </innerInnerInnerTagB> </innerInnerTagA> <innerInnerTagB> <innerInnerInnerTagZ> This Data should real...

XML namespaces and XSD.exe
Oh boy i'm getting tangled in knots here. I have a schema which contains <xs:schema targetNamespace="http://Southend.Schemas.PAS/PASPatientDemographics.xsd" id="PASPatientDetails" xmlns:sh="http://Southend.Schemas.PAS/PASPatientDemographics.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> now i used the xsd.exe and it now generates elements when I serilaize the class I get <RequestingPatient_ID xmlns="">D1234567890</RequestingPatient_ID> So each emlement contains xmlns=&qu...

xpath??
Hi, I am very new to xml. I have a datatable I need to group by and was suggested that xml and xpath might be the way to go. I converted the datatable to xml but don't know what to do next. Can anyone point me to the right direction. This is what I am trying to do: - have datatable (~300k rows and 5 cols) - need to group by column1(string catagory) and column2(date) - display the totals by column1 and column2 in datagrid. Any help is appreciated. Thanks. The advice to convert the database to XML was clearly a bad joke. RDBMSes are best at what they can do best -- work with relational da...

Sorting with XPATH
Given an XML node List with the individual nodes looking like the following, what kind of XPATH statement can I use to sort the nodes by element F8 and inside F8 by element F3? <OffNetworkRequestPackage> <Transaction> <OffNetworkRequest> <OffNetworkData> <UserEmail>loretta.pelisserok@getronics.com</UserEmail> <AdminEmail>NoEmail</AdminEmail> </OffNetworkData> <xxirequest> <function uuid="SBPProcess" name="CBICashCheck" bpname="TellerStd"/> </xxirequest> <TF&...

XPath and namespaces
Hi; First off, I think I still do not understand exactly how we are supposed to handle namespaces when doing xpath queries under .NET so I may have some bad assumptions here. As I understand it, we need to set the namespaces on an XmlDocument - the ..NET libraries do not read it from the xmlns:... attributes in the xml. In our case we have to handle xml our customers will pass to our library so we do not know up front what namespaces will be in there or how they are set up. So we do the following: private void SetNamespace(XPathNavigator nav) { XPathNodeIterator list = nav.Select...

XML namespaces and VB.Net
I posted this a few days ago and didn't get any response. I try again but ask it a little differently. I'm recieving any XML document from a client and I need to convert it to an ASCII delimited string to input into a legacy system. I've put together a VB.Net class that does this using DOM commands such as SelectSingleNode(). It works fine but I've noticed that it can not read past inbedded namespaces that are found throghout the document. I don't use namespaces in my own XML document so I do not know how to make this work or if I can. The system they are usi...

XPath with namespace
Hello all, I am working on an Add-in to the .NET framework using C#. I wanted to search the XML document using XPath but failed. The XML is like this: <unit xmlns="http://www.some.info/srcML" xmlns:cpp="http://www.some.info/srcML/cpp"> <aaa>something</aaa> </unit> The code I am using is as follows: XPathNavigator nav; XPathDocument docNav; XPathNodeIterator NodeIter; string fname="sth.xml"; docNav = new XPathDocument(@fname); nav = docNav.CreateNavigator(); nav.Evaluate(input); NodeIter = nav.Select(input); It works fine if there is n...

Tricky XPath query
Hey all, I've attempted to simplify my question (or at least the XML that is part of my example in my question): I have the following XML: <A> <B><C><D> I want to query the data in this node for the element B </D></C><E> I also want to query the data in this node for the element B </E> </B> <B><!--Additional data for another B element--> </B> </A> My idea is to do something like this: --------------------------------------------------- Dim nav As XPathNavigator = doc.CreateNaviga...

What is the Xpath
Hello, Thanks for the response Oleg. Now I need to know few answers. How do I get the value of a node using the xpath. I want the value of the MessageID and ResultCode from secondxml. And using this I want to delete the Child Nodes in the firstxml document. I am using the fowllowing code but the ni.current.value has values of all nodes in the file. I am confused. XmlDocument doc = new XmlDocument(); doc.Load("Firstxml.xml") hDocument doc2 = new XPathDocument("second.xml"); XPathNavigator nav = doc2.CreateNavigator(); foreach (XmlNo...

XMLnodelist and XPATH Problem
I am having a problem selecting nodes using the XMLnodelist Selectnodes using XPATH when I use XML SPY is successfully queries but when is use VB.net it comes up with nothing. Here is my code Dim nodess As XmlNode nodess = myNode.SelectSingleNode("//Web[@Url= 'http://localhost']") If nodess Is Nothing Then SQLrw.Delete() End If nodess = Nothing The myNode Properites ------------------------------------------------------------------------------------------------ {System.Xml.XmlElement} [System.Xm...

XPath string
Hi, I want to get the node with text=”{123}”. 1/ The following statement works fine: XmlNode wksNode = parentNode.SelectSingleNode("child::wks[text()='{123}']"); 2/ When I try to use a variable for the value of text, it doesn’t work anymore: string str = "child::wks[text()='" + myText + "']"; XmlNode wksNode3 = parentNode.SelectSingleNode(str); What is wrong? The variable str is exactly the same like the string used within the function SelectSingleNode in case 1/ Thanks for help, Lubomir Lubomir wrote: > When I try to use a varia...

Get attributes from XmlSchema for specific Node referenced by Xpath
I have a XSD loaded into a XmlSchema class. I need to retrieve the attribut names for/under a specific node in this schema, and I have the full xpath for the node in the schema. How do I do this? I'm not sure I should use the XmlSchema class to load the XSD. Regards Paw ...

XPath and SQLXML
I have the following XSD created in VS.NET 2003: <?xml version="1.0" encoding="utf-8" ?> <xs:schema id="ReferralSchama" targetNamespace="http://test.org/Referral" elementFormDefault="unqualified" xmlns="http://test.org/Referral" xmlns:mstns="http://test.org/Referral" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:complexType name="ReferralType"> <xs:sequence> <xs:element name="nReferralId" msdata:...

Problem selecting a node with XPATH if attribute value contains backslashes
VB6, MSXML 3.2 installed: Q1. I am having a problem selecting nodes with XPATH expressions when an attribute values contain backslashes (\\) in as part of its value: For example the following statement fails to find a node (even though one exists in the XML) if the value of the LDAPServerURL attribute is \\LocalServer but works if the value is "LocalServer". Set xmlElement = _ xmlConfigFile.documentElement.selectSingleNode("Facility[@LDAPServerURL='" & <SomeValue> & "']") I assumed that anything inside the '' was treated as a li...

XPath #4
This is a multi-part message in MIME format. ------=_NextPart_000_0016_01C44593.E9775F00 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I am new to XPath and I am stuck with an expression. I would like to = select all servers which the user subscribed to. Here is my XML: <forums> <forum> <name>AAA</name> <subscribed>1</subscribed> </forum> <forum> <name>BBB</name> <subscribed>0</subscribed> ...

xpath syntax
I have the following xml: <?xml version="1.0" ?> <course> <globalProperties> <externalMetadata> <source>ADL</source> <model>ADL SCORM 1.0</model> </externalMetadata> </globalProperties> <block id="B1"> <identification> <title>Yes/No Questions</title> <description>This course has one lesson</description> <labels> <curricular>MODULE</curricular> <developer>Me</developer> </labels> </identification&g...

XPath resource
hi, i've started working on xml but its location-path subject is too confusing. any good reference on web or book ? thanks ...