XPath and XMLs with namespaces
I have the following XML:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAgence xmlns:xsi="http://www.w3.org/2001开发者_如何学运维/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.artos.co.il/">
<Agence>
<CodeAval>20008</CodeAval>
<CodeSource>ArtPO</CodeSource>
<LogicielSource>NTLIS</LogicielSource>
</Agence>
</ArrayOfAgence>
I want to get the CodeAval value, so I tried:
ArrayOfAgence/Agence/CodeAval
It obviously didn't work since this XML has a namespace, so, how do I need to approach this?
Thanks,
You mentioned in a comment that you use this XPath in XSLT. In that case you need to add a namespace definition with a prefix for the input documents default namespace http://www.artos.co.il/
and then use this prefix in your XPath expression.
Example of namespace definition (it doesn't need to be in the stylesheet
element)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:artos="http://www.artos.co.il/">
Example of the prefix usage in the XPath expression
<xsl:value-of select="artos:ArrayOfAgence/artos:Agence/artos:CodeAval"/>
Note the added prefix in front of every element name.
If your XPath library is namespace aware (which yours seems to be) then it should provide you with a way to alias namespaces.
For example, if you're using Java, then the javax.xml.xpath.XPath
object has a setNamespaceContext(NamespaceContext context)
method which lets you provide a namespace resolver.
So, you might tell it that a
is an alias for the namespace http://www.artos.co.il/
, and then your query would become a:ArrayOfAgence/a:Agence/a:CodeAval
精彩评论