Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Help in using XPath for XML having multiple namespace declarations >Thread Next - Re: Help in using XPath for XML having multiple namespace declarat Re: Help in using XPath for XML having multiple namespace declarationsTo: NULL Date: 12/4/2006 11:41:00 AM "sai" <sai@d...> wrote in message news:B2EB7545-DFD0-45B9-A79D-8EBABD18951F@m...... > I have the following xml files. > > xml1: > <?xml version=\"1.0\"?> > <presence xmlns=\"urn:ietf:params:xml:ns:pidf\" > xmlns:ex=\"urn:ietf:params:xml:ns:pidf:ex\" > > <tuple id='xyz'> > <status><basic>online</basic><ex:ex>busy</ex:ex></status> > </tuple> > <tuple id='xyz1'> > <status><basic>offline</basic></status> > </tuple> > </presence> > xml2: > <?xml version=\"1.0\"?> > <tns:presence xmlns:tns=\"urn:ietf:params:xml:ns:pidf\" > xmlns:ex=\"urn:ietf:params:xml:ns:pidf:ex\" > > <tuple id='xyz'> > <status><basic>online</basic><ex:ex>busy</ex:ex></status> > </tuple> > <tuple id='xyz1'> > <status><basic>offline</basic></status> > </tuple> > </tns:presence> > > 1. If I use the "xml1" xml file and execute the following XPath query > "//tuple", I got the response as 0 nodes matched > 2: If I use the "xml2" file and execute the same XPath query as above, then > I got the response as 2 nodes matched. > > Q1. I do not see any difference in the xml files and both represent same > thing but why do i get differnet response for the same query. Is is > mandatory to write namespace declarations similar to that of in xml file > "xml2". In XML1 you set the default namespace whereas in XML2 you do not. Since tuple has no namepace alias it is part of the default namespace. However XPath searches are not affected by alias declarations in the document hence it will search the no-name namespace. In XML2 tuple belongs to the no-name namespace hence the XPath finds it whereas in XML1 tuple belongs to the urn:ietf:params:xml:ns:pidf namespace so it doesn't. > Q2. How should be the XPath query for searching for "ex:ex" element? If I > use the following XPath query, I am getting the response as 0 nodes matched. In order to search other namespaces using XPath it needs to be informed of them. This is done using a secondary property on the DOM which is set via the setPropery method on the IXMLDOMDocument2 interface. E.g.- oDOM.setProperty("SelectionNamespaces", "xmlns:tns='urn:ietf:params:xml:ns:pidf' xmlns:ex='urn:ietf:params:xml:ns:pidf:ex'") (I don't know C well enough to port this for you) Note that eh alias names uses in the above do not need to match the alias names used in the document. Eg. the following XPath will find the tuple nodes in XML1:- "//tns:tuple" Even though tns isn't used in XML1. I suspect that you will also need:- oDOM.setProperty("SelectionLanguage", "XPath") to ensure MSXML3 is actually using XPath. > > Thanks in advance for your help. | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
