XPath: get element from node with namespace?
I have a problem retrieving an element value via XPath. The XML is like
<?xml version="1.0" encoding="UTF-8"?>
<SOAP:Envelope ...>
<SOAP:Header>
...
</SOAP:Header>
<SOAP:Body>
<A xmlns="myns/">
<B>
<C>
<D>123</D>
...
I want to retrieve node 'D'. I was able to select node 'A' with this XPath:
//SOAP:Body/child::*[namespace-uri()='myns']
but can't get further. E.g.
//SOAP:Body/child::*[namespace-uri()='myns']/B
doesn't work ...?
How about?
<xsl:for-each select = "//SOAP:body">
<xsl:value-of select="." />
<xsl:if test="(position() = last())">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
No, it doesn't work because you don't have a "B" element, you have a "B in the default namespace" element. Do a Google search for "xpath default namespace" and you will see the problems other people have had with this design flaw in XPath.
Apparently XPath 2.0 has a fix for the problem.
Alas, this doesn't work if i want to select a subnode that has an additional namespace attribute, e.g.<C mns:IsValid='true>I tried to state this namespace-uri property everywhere but I don't get the nodes with these XPaths.>
> I have a problem retrieving an element value via
> XPath. The XML is like
>
> > <?xml version="1.0" encoding="UTF-8"?>
> <SOAP:Envelope ...>
><SOAP:Header>
> ...
></SOAP:Header>
> <SOAP:Body>
> <A xmlns="myns/">
><B>
> <C>
> <D>123</D>
> ode]
>
> I want to retrieve node 'D'. I was able to select
> node 'A' with this XPath:
>
> [code]
> //SOAP:Body/child::*[namespace-uri()='myns']
>
>
> but can't get further. E.g.
>
> > //SOAP:Body/child::*[namespace-uri()='myns']/B
>
>
> doesn't work ...?
Is there any solution to this problem?
No, it doesn't work because you don't have a "B" element, you have a "B in the default namespace" element.As I already said, if you scan back to November.I believe that the XPath function "local-name()" might help here.