xsl-list
[Top] [All Lists]

[xsl] Temporary tree elements and namespaces

2008-02-13 12:47:05
Hello everyone,

I was about to finish converting a xml document to an excel xml file
when I noticed that some tax values on the input file can have up to 4
decimal places while in the final document they are all being rounded
to 2 decimal places which raises problems when you sum all of them,
since the sum of the rounding might be different from the rounding of
the sum.
Try 0.123 + 0.234 for example:
1: 0.123 => 0.12
2: 0.234 => 0.23
sum: 0.123 + 0.234 = 0.357 => 0.36 (not correct given the previous values)

To get rid of this problem I've decided it was a good idea to use a
temporary tree that saves all the rounded values in advance, so I can
show them individually and also present a correct sum in the end.
Everything went well with my sample tests but it didn't work in the
final version. After some wasted hours I've figured out that the
problem is the default namespace that Excel *requires* to be present.
As soon as I added a default namespace (xmlns="something") to my test
stylesheet it stopped working too. When I thought about it, I realized
that maybe the elements in the temporary tree weren't binded to the
source default namespace and so I've added a namespace attribute to
the xsl:element which unfortunately didn't do anything. What am I
doing wrong?
Below goes both the xml and the xsl files. For the sake of simplicity
I've cut down a lot of the content and a bit of the temp tree to only
keep what matters. Also this version is working but if you had a
default namespace to the stylesheet definition the select stops
returning any nodes.
Finally I'm using Saxon 9 (great piece of software btw).

Thanks in advance,
João C. Morais

=== XML FILE ===

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

=== XSL FILE ===

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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--