RE: XmlDocument.SelectSingleNode XPathException
- From: "David Thielen" <thielen@xxxxxxxxxxxxx>
- Date: Wed, 13 Apr 2005 08:05:08 -0700
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.)
>
>
>
>
>
.
- Follow-Ups:
- RE: XmlDocument.SelectSingleNode XPathException
- From: Steven Cheng[MSFT]
- RE: XmlDocument.SelectSingleNode XPathException
- References:
- XmlDocument.SelectSingleNode XPathException
- From: David Thielen
- RE: XmlDocument.SelectSingleNode XPathException
- From: Steven Cheng[MSFT]
- RE: XmlDocument.SelectSingleNode XPathException
- From: David Thielen
- RE: XmlDocument.SelectSingleNode XPathException
- From: Steven Cheng[MSFT]
- RE: XmlDocument.SelectSingleNode XPathException
- From: David Thielen
- RE: XmlDocument.SelectSingleNode XPathException
- From: Steven Cheng[MSFT]
- XmlDocument.SelectSingleNode XPathException
- Prev by Date: Re: Posting XML to Server from ASP.NET/C# webapp
- Next by Date: Re: Posting XML to Server from ASP.NET/C# webapp
- Previous by thread: RE: XmlDocument.SelectSingleNode XPathException
- Next by thread: RE: XmlDocument.SelectSingleNode XPathException
- Index(es):
Relevant Pages
|
Loading