RE: XmlDocument.SelectSingleNode XPathException



Hi;

I agree with everything you said except...

The "xmlns:sns="http://www.test.org/sub";>" in the xml itself provides the
necessary information to turn:
//sns:subItem[@sid="11"]
into:
//http://www.test.org/sub:subItem[@sid="11";]

That's how java parsers like dom4j handle it.

And in fact, it would be very weird if I could pass in xml with
"xmlns:sns="http://www.test.org/sub";>" in it and then call
mgr.AddNamespace("sns","http://www.dave.com";); and then I would have changed
the meaning of the xml from what is set in it's own header.

Am I missing something here?

thanks - dave



"Steven Cheng[MSFT]" wrote:

> Hi Dave,
>
> Thanks for your followup.
> As for using XmlNamespaceManager to help executing XPATH in xmldocument
> which contains namespace/prefix, I don't think it's the limitation of
> .net's XML interfaces. I'm sure this is a quite reasonable feature, here is
> the reason why the XmlNamespaceManager is necessary:
>
> When there is namespace in an xml document to scope elements in different
> area(schemas), we use prefixs to identify namespaceURI. Like:
>
> ===============================
> <?xml version="1.0" encoding="utf-8" ?>
> <root xmlns="http://www.test.org";
> xmlns:sns="http://www.test.org/sub";>
> <data>
> <items>
> <item id="1">
> <sns:subItem sid="11">
>
> </sns:subItem>
> <sns:subItem sid="12">
>
> </sns:subItem>
> </item>
> </items>
> </data>
> </root>
> ==============================
>
> when we need to quer the <sns:subItem sid="11">
>
> maybe we'll think that use the following xpath is quite normal:
>
> //sns:subItem[@sid="11"]
>
> However, according to the W3C XPATH1.0 specification. The full qualified
> name of an element is actually it's namespaceURI(not prefix) combined with
> its localname, that means the above path should be:
>
> //http://www.test.org/sub:subItem[@sid="11";]
>
> And as we known, namespace prefix is just a alias of namespaceURI, which
> can be replaced by any other one as long as it can uniquely identity that
> namespaceURI in the document. So .net framework provide the
> XmlNamespaceManager which can help specify Namespace with arbitary prefix
> value. For example, the above selection can be replaced by
>
> XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable());
>
> mgr.AddNamespace("aaa","http://www.test.org/sub";);
>
> string xpath = "//aaa:subItem[@sid='11']";
>
> "sns" is just an alias, the actual idenitfy is "http://www.test.org/sub";,
> and the prefix in xpath should be able to be replaced by another alias.
> (not only limited to "sns")
>
> I'm not sure if those JAVA xml api is capable of this. However, at least I
> think using prefix directly in xpath without any mapping info between
> prefix---namespaceURI is not a reasonable behavior. Also, some times we're
> not executing Xpath on a whole XmlDocument, just on a XmlNode , then where
> does the API to look for the namespace /prefix declaration on the fly?
> That's also why the .net using the XmlNamespaceManager to specify the
> prefix---namespaceURI mapping.
>
> #HOW TO: Specify Namespaces When You Use an XmlDocument to Execute XPath
> Queries in Visual C# .NET
> http://support.microsoft.com/default.aspx?scid=kb;en-us;318545
>
> If you still have anything unclear, please feel free to post here. Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>
>
>
.



Relevant Pages

  • Re: master / detail datagrid...
    ... Macromedia uses their prefix in the exact same way an XML ... A patent on the other hand can be obtained but unlike the paltry ... and an XML Namespace is the equivalent of a short musical ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: node() method ... xmlns
    ... error near 'declare namespace', expected string literal." ... You have saved me a ton of time and effort in understanding tsql xml ... prefix to qualify element names in your expression. ...
    (microsoft.public.sqlserver.xml)
  • Re: Need help with Xpath
    ... XML, I define "ns1" as local prefix for the NS. ... XmlNamespaceManager nsMgr = new XmlNamespaceManager; ... >> XPath queries are namespace aware. ...
    (microsoft.public.dotnet.xml)
  • Re: Validation not occuring
    ... is the only solution to include the specific prefix ... the XML would look something like this: ... XSL file would also match on the girder prefix? ... You have a namespace declaration that binds the prefix girder to the ...
    (microsoft.public.dotnet.xml)
  • Re: Namespace Prefixes
    ... Does your "party" object to the use of a target namespace or just the ... You can work with XML documents without the target ... namespace and prefix by using custom XML pipelines so long as you don't have ... others want an xml document with no target namespace ...
    (microsoft.public.biztalk.general)

Loading