tags:

views:

195

answers:

1

Hi all,

I'm trying to get a node-set from a xsl variable for calculating. But my code only work with Opera, with other browsers, I keep getting the error.

Please help me fix to run with all browser. Thanks in advance.

Here are the xslt code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"&gt;
<xsl:output method="html"/>

<xsl:variable name="multipleSet">
 <xsl:for-each select="myNums/numSet">
  <xsl:element name="multiple"><xsl:value-of select="num1 * num2"/></xsl:element>
 </xsl:for-each>
</xsl:variable>
<xsl:template match="/">
 <table border="1">
    <tr>
     <th>Num 1</th>
     <th>Num 2</th>
     <th>Multiple</th>
    </tr>
   <xsl:for-each select="myNums/numSet">
    <tr>
     <td><xsl:value-of select="num1"/></td>
     <td><xsl:value-of select="num2"/></td>
     <td><xsl:value-of select="num1 * num2"/></td>
    </tr>
   </xsl:for-each>
    <tr>
     <th colspan="2" align="right">Total:</th>
     <td><xsl:value-of select="sum($multipleSet/multiple)"/> </td>
    </tr>
 </table>
</xsl:template>
</xsl:stylesheet>

And the xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<myNums>
 <numSet>
  <num1>5</num1>
  <num2>5</num2>
 </numSet>
 <numSet>
  <num1>10</num1>
  <num2>5</num2>
 </numSet>
 <numSet>
  <num1>15</num1>
  <num2>20</num2>
 </numSet>
</myNums>
A: 

Web browsers usually don't support xslt 2.0. Xpath sum() expects a node-set. What you are passing it is a "result tree fragment". The result tree fragment data-type has been removed in XSLT 1.1. So what you are experiencing is:

  • Opera doesn't tell you by an error message that it doesn't support xslt 2.0 (Opera specification)
  • Opera does not conform fully to 1.0 specification

You can try to specify version="1.1" and see if it helps. Otherwise, you may try if the browsers support exsl:node-set().

Krab
Yeah, you're right. It's my bad not to check xslt 2.0 support in the first place.Using EXSLT will make everything go well, but not with IE, MS have their own msxsl extensions. I want to display the document on any browser with xslt 2.0, have to find another way though. Thanks for showing me the point. :)
Henry