2

我正在尝试从 xsl 变量中获取节点集以进行计算。但是我的代码只适用于 Opera,在其他浏览器中,我不断收到错误消息。

请帮我修复以使用所有浏览器运行。提前致谢。

这是xslt代码:

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

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

1 回答 1

0

Web 浏览器通常不支持 xslt 2.0。Xpath sum() 需要一个节点集。你传递的是一个“结果树片段”。结果树片段数据类型已在 XSLT 1.1 中删除。所以你正在经历的是:

  • Opera 不会通过错误消息告诉您它不支持 xslt 2.0(Opera 规范
  • Opera 不完全符合 1.0 规范

您可以尝试指定version="1.1"并查看是否有帮助。否则,您可以尝试浏览器是否支持exsl:node-set()

于 2010-05-08T07:41:40.193 回答