Re: [xsl] Temporary tree elements and namespaces

Subject: Re: [xsl] Temporary tree elements and namespaces
From: "João C. Morais" <spiderfish@xxxxxxxxx>
Date: Thu, 14 Feb 2008 03:49:06 +0000
Yes that works too. Didn't occur to me that I could construct the Workbook element by hand and get rid of the default namespace in the stylesheet.
By the way sum($taxes/value) and sum($taxes//value) gives (as expected) the same value using your stylesheet in Saxon 9.0.0.2


Just to finalize our discussion, yet another way is to simply bind the new elements to the no namespace, since xpath defaults to this behaviour.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns="required-excel-namespace">
<xsl:variable name="taxes" as="item()*">
<xsl:for-each select="//value">
<xsl:element name="tax" namespace="">
<xsl:element name="value" namespace="">
<xsl:value-of select="format-number(.,'#.00')"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="format-number(sum($taxes/value),'#.00')"/>
</xsl:template>
</xsl:stylesheet>


Once again, thanks for your help and patient.

Cheers,
Joco

Sam Byland wrote:
Joco,

I think we're nearly there. Your input XML has no namespaces to deal with; you just need all elements in the output to be in the "required-excel-stylesheet" namespace, correct? If this is the case, you don't need to change or set an xpath default namespace in your stylesheet. You just need to make sure any elements you throw to the output are in the appropriate namespace. For example, this modified version of your XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >


<xsl:variable name="taxes" as="item()*">

<xsl:for-each select="//value">

<xsl:element name="tax">
<xsl:element name="value">
<xsl:value-of select="format-number(.,'#.00')"/>
</xsl:element>
</xsl:element>


</xsl:for-each>

</xsl:variable>

<xsl:template match="/">
<xsl:element name="DocumentExcelElement" namespace="required-excel-stylesheet">
<xsl:element name="SomeExcelElement" namespace="required-excel-stylesheet">Hello World</xsl:element>
<xsl:element name="SomeOtherExcelElement" namespace="required-excel-stylesheet">
<xsl:value-of select="format-number(sum($taxes//value),'#.00')"/>
</xsl:element>
</xsl:element>
</xsl:template>


</xsl:stylesheet>

transforms this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<values>
               <value>0.123</value>
               <value>0.234</value>
</values>

into this:

<?xml version="1.0" encoding="UTF-8"?>
<DocumentExcelElement xmlns="required-excel-stylesheet">
               <SomeExcelElement>Hello World</SomeExcelElement>
               <SomeOtherExcelElement>0.35</SomeOtherExcelElement>
</DocumentExcelElement>


Is that what you are looking for? Note also that I changed sum($taxes/value) to sum($taxes//value)...otherwise I always got 0.00...


Cheers,

...sam

Current Thread