在 xslt 3.0 中使用累加器无法正常工作

迅速

我有一个 XML,其中有一个带有标题、价格、作者、附加价格、艺术家、国家等属性的书籍列表。 XML 如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <additionalprice>12.90</additionalprice>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <additionalprice>10.90</additionalprice>
    <year>1988</year>
  </cd>

等等

我想编写一个应用于 XML 的 XSLT 3.0 来获取 HTML。我想使用累加器来获取目录中的书籍总数。虽然有更好的方法,但我只是想使用累加器进行练习,并在包含标题、作者和总价的书籍的表格末尾打印总数。对于填写标题和作者,我使用了 for-each。总价=价格+附加价格。我想使用 iterate 来归档总价。任何人都可以帮我解决这个问题。我不完整的样式表如下所示:

<?xml version="3.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <h2>My CD Collection</h2>
  <table border="2">
    <tr bgcolor="#9acd32">
       <th style="text-align:left">Title</th>
       <th style="text-align:left">Artist</th>
       <th style="text-align:left">Total Price</th>
    </tr>
 <xsl:accumulator name="total" as="xs:integer" initial-value="0" streamable="no">
    <xsl:accumulator-rule match="title" select="$value+1"/>
 </xsl:accumulator>
    <xsl:for-each select="catalog/cd">
     <tr>
       <td><xsl:value-of select="title"/></td>
       <td><xsl:value-of select="artist"/></td>
     </tr>
   </xsl:for-each>
         <tr bgcolor="#FFA500">
           <td> Total number of Books </td>
           <td> <xsl:value-of select="accumulator-before('total')"/</td>
         </tr>
     </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

我不想使用流媒体。

马丁·霍南

如果您查看规范https://www.w3.org/TR/xslt-30/#accumulator-declaration,那么您会看到这xsl:accumulator是一个声明,这意味着您必须将其用作xsl:stylesheet, 不在模板内。

并且一个累加器值与一个节点相关联,函数如https://www.w3.org/TR/xslt-30/#func-accumulator-before "返回上下文节点处所选累加器的下降前值" 所以在你的情况下,你有一个带有上下文节点的模板,/在值之前访问累加器没有多大意义,你更需要这个accumulator-after值。

最后,您必须声明累加器将应用于默认模式

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:mode use-accumulators="#all"/>

    <xsl:accumulator name="total" as="xs:integer" initial-value="0" streamable="no">
        <xsl:accumulator-rule match="title" select="$value + 1"/>
    </xsl:accumulator>

    <xsl:template match="/">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <table border="2">
                    <tr bgcolor="#9acd32">
                        <th style="text-align:left">Title</th>
                        <th style="text-align:left">Artist</th>
                        <th style="text-align:left">Total Price</th>
                    </tr>

                    <xsl:for-each select="catalog/cd">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <td>
                                <xsl:value-of select="artist"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                    <tr bgcolor="#FFA500">
                        <td> Total number of Books </td>
                        <td>
                            <xsl:value-of select="accumulator-after('total')"/>
                        </td>
                    </tr>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

XSLT 3 中的条件流累加器

来自分类Dev

Spark:在Range中使用累加器时,它无法正常工作

来自分类Dev

XSLT contains()无法正常工作

来自分类Dev

Java XSLT无法正常工作

来自分类Dev

XSLT translate()无法正常工作

来自分类Dev

XSLT 命名空间无法读取 ns0:

来自分类Dev

使用xslt 10我想将负值转换为0

来自分类Dev

使用XSLT分组数据无法完全正常工作

来自分类Dev

XSLT for-each,排序无法正常工作

来自分类Dev

XSLT for-each,排序无法正常工作

来自分类Dev

使用 xslt-3 使 xml 元素唯一

来自分类Dev

没有带SWI-Prolog的累加器,是否可以从0递增到n?

来自分类Dev

没有带SWI-Prolog的累加器,是否可以从0递增到n?

来自分类Dev

在XSLT中使用for循环

来自分类Dev

在类的无序映射中使用滚动累加器

来自分类Dev

glDrawArrays无法正常工作。在GTK3中使用GtkGLArea

来自分类Dev

在“ jquery”选项卡中使用时,c3.js图表无法正常工作

来自分类Dev

xslt从xml读取单独的元素,无法正常工作

来自分类Dev

XSLT工作太慢

来自分类Dev

XSLT工作太慢

来自分类Dev

DHCP3-服务器无法正常工作

来自分类Dev

在XSLT select中使用变量

来自分类Dev

在XSLT select中使用变量

来自分类Dev

XSLT无法处理实体

来自分类Dev

升压累加器示例无法编译

来自分类Dev

使用XSLT 1.0在3个级别上对数据进行分组

来自分类Dev

解析 html 文件,使用 xslt 3 从嵌套类别层次结构中获取数据

来自分类Dev

使用 xsl 流在 XSLT3 中分组,但出现“模板规则不可流”错误

来自分类Dev

Topmargin 0无法正常工作?缓存问题?