Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 25th, 2003, 06:26 PM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding numbers

I am struggling to figure out XML and XSL as I go, so please treat me like a complete novice.

I have the following XML output from an SQL database:

- <Target>
- <Target_T Test_Run_ID="47" Target_ID="76" Target_Name_VC="auto-target12">
  <Test_T PF="1" Duration="324" />
  <Test_T PF="1" Duration="371" />
  <Test_T PF="1" Duration="166" />
  <Test_T PF="1" Duration="169" />
  <Test_T PF="2" Duration="0" />
  <Test_T PF="1" Duration="1658" />
  </Target_T>
  </Target>

I'm trying to get a sum of the Duration values, and displaying it in a table. I've created the template at the end of this message, and am calling it via the following:

<xsl:for-each select="Target/Target_T">
<TR>
<TD>
    <xsl:apply-template name="totDuration"/>
</TD>
</TR>
</xsl:for-each>

IE6 reports "Keyword xsl:apply-template may not be used here." I can't figure out why. Can anyone help me? Any hints would be most appreciated.

Here's the template:

<xsl:template name="totDuration">
  <xsl:param name="Tests" select="Test_T" />
  <xsl:param name="subtotal" select="0" />
  <xsl:choose>
    <xsl:when test="$Tests">
      <xsl:call-template name="totDuration">
        <xsl:with-param name="Tests"
                        select="$Tests[position() > 1]" />
        <xsl:with-param name="subtotal"
          select="$subtotal + substring-before($Tests[1], ':')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$subtotal" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
 
Old June 26th, 2003, 12:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by kend
 I am struggling to figure out XML and XSL as I go, so please treat me like a complete novice.

I have the following XML output from an SQL database:

- <Target>
- <Target_T Test_Run_ID="47" Target_ID="76" Target_Name_VC="auto-target12">
  <Test_T PF="1" Duration="324" />
  <Test_T PF="1" Duration="371" />
  <Test_T PF="1" Duration="166" />
  <Test_T PF="1" Duration="169" />
  <Test_T PF="2" Duration="0" />
  <Test_T PF="1" Duration="1658" />
  </Target_T>
  </Target>

I'm trying to get a sum of the Duration values, and displaying it in a table. I've created the template at the end of this message, and am calling it via the following:

<xsl:for-each select="Target/Target_T">
<TR>
<TD>
    <xsl:apply-template name="totDuration"/>
</TD>
</TR>
</xsl:for-each>

IE6 reports "Keyword xsl:apply-template may not be used here." I can't figure out why. Can anyone help me? Any hints would be most appreciated.

Here's the template:

<xsl:template name="totDuration">
  <xsl:param name="Tests" select="Test_T" />
  <xsl:param name="subtotal" select="0" />
  <xsl:choose>
    <xsl:when test="$Tests">
      <xsl:call-template name="totDuration">
        <xsl:with-param name="Tests"
                        select="$Tests[position() > 1]" />
        <xsl:with-param name="subtotal"
          select="$subtotal + substring-before($Tests[1], ':')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$subtotal" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
Hi,
IE6 complains because there is no element "xsl:apply-template" in XSLT's namespace (you should write "xsl:apply-templates"). Besides that this element has no "name" attribute; I guess you intended to write "xsl:call-template" instead.
In any case, there is no need to write separate template to sum the values: you can use built-in "sum" function. Drop the template named "totDuration", and change one line in the code you supplied here:
Code:
<xsl:for-each select="Target/Target_T">
<TR>
<TD>

    <xsl:value-of select="sum(Test_T/@Duration)"/>
</TD>
</TR>
</xsl:for-each>
Regards,
Armen
 
Old June 26th, 2003, 10:13 AM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Armen,

Thanks for your reply. You suggested dropping the template and using a one-line xsl:value-of with the SUM function. This is what I started with, but it doesn't work. IE reports

"NodeTest expected here.-->sum(--Test_T/@Duration)"

I'll try correcting my syntax error with the template and see how it goes.

Thanks again,
Ken
 
Old June 26th, 2003, 12:40 PM
Authorized User
 
Join Date: Jun 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, here's the solution I came up with.

The list of durations is the result of an SQL 2000 query, output for XML. It turned out to be easier to edit my stored procedure to do the summation, rather than doing it in my .XSL.

Thanks for your help. Your comments did trigger some thoughts that eventually led me down the right path.

Ken
 
Old June 27th, 2003, 12:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

I'm very glad to help you and it's fine of course that you've found the solution. But it's strange that your IE6 reports the error... My IE6 works fine with the "sum" function. Here is the full code:

target.xsl is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:template match="/">
   <table border="1">
        <xsl:for-each select="Target/Target_T">
         <TR>
          <TD>
            <xsl:value-of select="sum(Test_T/@Duration)"/>
          </TD>
         </TR>
        </xsl:for-each>
    </table>
 </xsl:template>
</xsl:stylesheet>
and target.xml is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="target.xsl"?>
<Target>
    <Target_T Test_Run_ID="47" Target_ID="76" Target_Name_VC="auto-target12">
        <Test_T PF="1" Duration="324"/>
        <Test_T PF="1" Duration="371"/>
        <Test_T PF="1" Duration="166"/>
        <Test_T PF="1" Duration="169"/>
        <Test_T PF="2" Duration="0"/>
        <Test_T PF="1" Duration="1658"/>
    </Target_T>
</Target>
XML doc and XSL stylesheet are in the same directory.

If you'll receive the same error message, it will mean that something with MSXML is wrong(MSXML4 fully conforms to XSLT1.0 specification unlike MSXML3; may be MSXML3 is working in your IE6 and causes the error?).

Regards,
Armen





Similar Threads
Thread Thread Starter Forum Replies Last Post
adding up 2 numbers in xsl umair.aziz XSLT 3 July 14th, 2006 09:24 AM
regarding adding numbers in xsl umair.aziz XSLT 1 June 24th, 2006 12:12 PM
Adding numbers Hudson40 Excel VBA 3 November 23rd, 2005 03:02 AM
adding numbers gmoney060 Access 1 August 15th, 2004 08:14 PM
Adding numbers kend XSLT 1 June 26th, 2003 10:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.