If I have this XSL
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:template match='/'>
<xsl:value-of select="//Description" />
</xsl:template>
</xsl:stylesheet>
And this XML
<ArrayOfLookupValue xmlns="http://switchwise.com.au/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<LookupValue>
<Description>AGL</Description>
<Value>8</Value>
</LookupValue>
<LookupValue>
<Description>Australian Power & Gas</Description>
<Value>6</Value>
</LookupValue>
<LookupValue>
<Description>EnergyAustralia</Description>
<Value>13</Value>
</LookupValue>
<LookupValue>
<Description>Origin Energy</Description>
<Value>9</Value>
</LookupValue>
<LookupValue>
<Description>TRU Energy</Description>
<Value>7</Value>
</LookupValue>
</ArrayOfLookupValue>
How do I actually get some data from this line:
<xsl:value-of select="//Description" />
I have spent hours on this and I have come to the conclusion that the xmlns= namespace is what is causing me grief.
Any help greatly appreciated.
BTW the XML is coming from a web service so I can’t just “change” it – I can preprocess it but that isn’t ideal…
Also I have confirmed that removing the namespaces from a mock of the XML does fix the problem.
This is the most FAQ for both XPath and XSLT.
The short answer is that in XPath an unprefixed name is considered to belong to “no namespace”. However, in a document with a default namespace the unprefixed names belong to the default namespace.
Therefore, for such document the expression
selects nothing (because there is no
Description(or any other) element in the document that belongs to “no namespace” — all element names belong to the default namespace).Solution:
Define a namespace in your XSLT that has the same
namespace-uri()as the default namespace of the XML document. Then use the prefix of the so defined namespace for any name used in an Xpath expression:When this transformation is applied to the provided XML document:
the wanted, correct result is produced: