xpathxml-namespacesxpath-3.1

XPath to retrive XML tag value without Namespace and prefix


I have the following XML -

<d><m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
                            <d:AllTexts/>
                            <d:BomFlag/>
                            <d:OrderNumber>9489</d:OrderNumber>
                            <d:LineNumber>000000</d:LineNumber>
                            <d:VcFlag>Y</d:VcFlag>
                            <d:PricingFlag/>
                            <d:TextType>H</d:TextType>
                            <d:TextId>ZC01</d:TextId>
                            <d:TextLineNo>1</d:TextLineNo>
                            <d:TextLine>ecom header text 1</d:TextLine>                         

and trying to retrieve the TextLine nodelist as based on TextId = ZC01 - <TextLine>ecom header text1</TextLine>

when I applied the xpath as --> //m:properties[d:TextId = 'ZC01']/d:TextLine

I get the output as - <d:TextLine xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">ecom header text 1</d:TextLine>

how can I remove the prefix and namespace? I tried using local-name(), but that didn't work May be used it wrong way. Thank you for your help!

Thanks Sugata


Solution

  • XPath is a selection language: it can only retrieve nodes that are actually there, it can't change them in any way. If the selected element has a prefix and namespace in the original, then it will have a prefix and namespace in the result.

    However, you need to distinguish what the XPath selects (a node) from the way it the result is displayed. This depends on the application that is evaluating the XPath. The two popular ways of displaying a node selected by an XPath expression are (a) by serialising the node as XML (which is what we see in your case), and (b) by showing a path to the selected node, such as /d/m:properties/d:TextLine. You haven't told us how you are evaluating the XPath expression or displaying its result, and you may have options here.

    But perhaps you should consider XSLT or XQuery, which (unlike XPath) allow you to construct new XML that differs from your original.