Xml Namespace and xpath extraction problem
-
יום חמישי 15 מרץ 2012 14:41
Hi,
Xml Namespace is causing many headaches for xpath extraction, that's the conclusion I made while trying to find the answer for my question just now. Now I am almost certain that there might not be any good solution to my question, but let me still attempt my luck here.
Here goes my question, we all know that XPath does not support anonymous namespaces, so we have to assign it a name for the query. However, in my case, the query xml tag is constant, while the namespace might/would be different, yet I need to apply the same operation.
Let me explain with a simple example, say that the xml tag that I need to query is always '//saleprice', and the operation is always discount by 10%. However, the namespace might be different: fruit, frozen-food, caned-food, etc, etc.
This is just an example, but explain my problem well. Is there any generic way to apply my discount to all '//saleprice', regardless what the namespaces are?
yours helpless
כל התגובות
-
יום חמישי 15 מרץ 2012 15:05
Well with XPath 1.0 a path like
//saleprice
selects all elements named "saleprice" in no namespace. Thus if you have elements in any namespace where the local name of the element is "saleprice" that path does not help at all, it does not select them. All you can do in XPath 1.0 is
//*[local-name() = 'saleprice']
which selects all elements in any namespace (including no namespace) where the local name is "saleprice".
Does that help?
If you use XPath 2.0 or XQuery 1.0 (there are third party solutions supporting that, even if Microsoft does not support it) then you can also use
//*:saleprice
to select or match all elements named "saleprice" in any namespace.
MVP Data Platform Development My blog
- הוצע כתשובה על-ידי Martin Honnen יום חמישי 15 מרץ 2012 15:05
- סומן כתשובה על-ידי smetah יום חמישי 15 מרץ 2012 18:21
-
יום חמישי 15 מרץ 2012 15:06
Basically, I'm going to handle it with C#, Here is my code snip:
XPathDocument document = new XPathDocument("MyXml.xml"); XmlNode root = document.DocumentElement; XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable); manager.AddNamespace("n1", "uri1"); XmlNode myNode = root.SelectSingleNode("//saleprice", manager);
As for the XML, here is a bunch of sample cases that I need to cover.
one:
<fruit xmlns="http://uri/fruit"><item name="apple"><salesprice>10<salesprice></item></fruit>
And another one,
<canned-food xmlns="http://uri/canned-food"><close-to-expire xmlns="http://uri/close-to-expire"><salesprice>20<salesprice></close-to-expire></canned-food>
etc.
Please help.
Thanks -
יום חמישי 15 מרץ 2012 15:21
Did you see my previous response?
Doing
root.SelectSingleNode("//*[local-name() = 'saleprice']")
should work.
MVP Data Platform Development My blog
- נערך על-ידי Martin Honnen יום חמישי 15 מרץ 2012 16:46 fixing syntax
- סומן כתשובה על-ידי smetah יום חמישי 15 מרץ 2012 18:21
-
יום חמישי 15 מרץ 2012 18:21
Yep!
That works perfectly!
Sorry for responding late.
Thanks a lot!!!