Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl namespace problem

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 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.

like image 735
Rob Avatar asked Aug 23 '11 03:08

Rob


People also ask

What is XSL namespace?

XSLT Elements The namespace name http://www.w3.org/1999/XSL/Transform is used primarily to identify elements which serve as declarations or instructions in the XSLT language.

Is XSL and XSLT the same?

XSL Transformation (XSLT)XSLT is designed to be used as part of XSL. In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. XSL specifies the styling of an XML document by using XSLT to describe how the document is transformed into another XML document that uses the formatting vocabulary.

Can XSLT transform XML to CSV?

This post shows you how to convert a simple XML file to CSV using XSLT. The following XSL Style Sheet (compatible with XSLT 1.0) can be used to transform the XML into CSV. It is quite generic and can easily be configured to handle different xml elements by changing the list of fields defined ar the beginning.

How many components are available in XSL?

It consists of three parts: XSL Transformations (XSLT) a language for transforming XML; The XML Path Language (XPath)


1 Answers

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

//Description

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:

<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>

When this transformation is applied to the provided XML document:

<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>

the wanted, correct result is produced:

<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>
like image 174
Dimitre Novatchev Avatar answered Oct 09 '22 00:10

Dimitre Novatchev