Xsl namespace issue

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 &amp; 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 can I get some data from this line:

<xsl:value-of select="//Description" />

I spent several hours on this, and I came to the conclusion that the xmlns = namespace makes me sad.

Any help is greatly appreciated.

By the way, XML comes from a web service, so I can’t just “change” it - I can pre-process it, but this is not ideal ...

I also confirmed that removing namespaces from the XML layout fixes the problem.

+5
source share
1 answer

This is the most frequently asked question for XPath and XSLT .

, XPath " ". , .

//Description

( Description ( ), "no namespace" - ).

XSLT, namespace-uri(), XML-. , Xpath:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://switchwise.com.au/">
    <xsl:output method="html"/>
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/>

    <xsl:template match='/'>
      <xsl:copy-of select="//x:Description" />
    </xsl:template>
</xsl:stylesheet>

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 &amp; 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>

, :

<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>AGL</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>Australian Power &amp; Gas</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>EnergyAustralia</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>Origin Energy</Description>
<Description xmlns="http://switchwise.com.au/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>TRU Energy</Description>
+12

All Articles