1

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,

snoofkin
  • 8,725
  • 14
  • 49
  • 86
  • 1
    Firs exact duplicate found [XPATHS and Default Namespaces](http://stackoverflow.com/questions/11345/xpaths-and-default-namespaces) –  Feb 11 '11 at 21:28

2 Answers2

5

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.

jasso
  • 13,736
  • 2
  • 36
  • 50
4

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

dty
  • 18,795
  • 6
  • 56
  • 82