使用 xsl:number 元素时从 0 开始计数而不是 1

谢谢你

编辑:
我想我在仍然使用 XSLT 1.0 的同时找到了解决这个问题的方法,虽然非常冗长并且可能容易出错。


有没有办法使用<xsl:number>元素但从零而不是一开始计数,尤其是在使用时level="multiple"

我正在使用 XSLT 1.0。

当前结果:1.2.2. Subitem_B
预期结果:0.1.1. Subitem_B

我试过了,format="0"但它只输出点(即... Subitem_B

<xsl:number level="multiple" format="0. "/>

额外问题:我找到了一种使用 的方法translate($number,'.','-'),但我仍然很想知道是否有一种方法可以使用自定义字符而不是点作为分隔符,甚至format完全自定义

XML 输入

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl" version="1.0"?>
<root>
  <item>Main_A
    <item>Item_A</item>
    <item>Item_B
      <item>Subitem_A</item>
      <item>Subitem_B</item>
    </item>
    <item>Item_C</item>
  </item>
  <item>Main_B
    <item>Item_A
      <item>Subitem_A</item>
      </item>
    <item>Item_B</item>
  </item>
</root>

XSLT 1.0 样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="root">
  <html>
    <body>
      <xsl:apply-templates match="item"/>
    </body>
  </html>
</xsl:template>

<xsl:template match="item">
  <xsl:number level="multiple" format="1. "/>
  <xsl:apply-templates match="item"/>
</xsl:template>

</xsl:stylesheet>

HTML 输出

<html>
  <body>
  1. Main_A
    1.1. Item_A
    1.2. Item_B
      1.2.1. Subitem_A
      1.2.2. Subitem_B

    1.3. Item_C

  2. Main_B
    2.1. Item_A
      2.1.1. Subitem_A

    2.2. Item_B

</body>
</html>
michael.hor257k

如果您的处理器支持 EXSLTstr:tokenize()扩展功能(正如我认为的那样),您可以执行以下操作:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">

<xsl:template match="/root">
    <html>
        <body>
            <ul>
                <xsl:apply-templates select="item"/>
            </ul>
        </body>
    </html>
</xsl:template>

<xsl:template match="item">
    <xsl:variable name="num">
        <xsl:number level="multiple" format="1."/>
    </xsl:variable>
    <li>
        <xsl:text>path:</xsl:text>
        <xsl:for-each select="str:tokenize($num, '.')">         
            <xsl:value-of select=". - 1" />
            <xsl:if test="position()!=last()">
                <xsl:text>:</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text> = </xsl:text>
        <xsl:value-of select="text()" />
        <xsl:if test="item">
            <ul>
                <xsl:apply-templates select="item"/>
            </ul>
        </xsl:if>
    </li>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,结果将是:

<html>
<body>
<ul>
<li>path:0 = Main_A
    <ul>
<li>path:0:0 = Item_A</li>
<li>path:0:1 = Item_B
      <ul>
<li>path:0:1:0 = Subitem_A</li>
<li>path:0:1:1 = Subitem_B</li>
</ul>
</li>
<li>path:0:2 = Item_C</li>
</ul>
</li>
<li>path:1 = Main_B
    <ul>
<li>path:1:0 = Item_A
      <ul>
<li>path:1:0:0 = Subitem_A</li>
</ul>
</li>
<li>path:1:1 = Item_B</li>
</ul>
</li>
</ul>
</body>
</html>

呈现为:

在此处输入图片说明

我相信这是您在其他问题中要求的结果


请注意,这match不是 的有效属性xsl:apply-templates

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 ui 文本计算上次访问的航点时,如何使其从 1 而不是 0 开始计数?

如何让vim使用从1而不是0开始计数的相对行号

forEach 索引从 1 而不是 0 开始计数?

如何使tmux计数窗口从1而不是0开始?

使用范围和单元格时,数组似乎是从0开始而不是从1开始

使用 xsl:import 而不是 xsl:include 时的 TransformerException

当索引不是1时如何从0开始数组

使用 xsl:number 递减变量计数器

使用 xsl:number 来计算元素 XSLT 3.0 XSL-FO 的问题

使用 numpy 和 matplotlib 时绘图不是从 0 开始

使用IIFE时,抛出“ TypeError`:`[0,1]`不是函数”

使用<sql:query>时如何使用<xsl:attribute>制作元素

为什么从列表[-1]而不是列表[0]开始计算此列表中的元素?(Python 3)

使用xsl:for-each循环而不是xsl:variable中的select =“ expr”进行查询时遇到的问题

indexpath.row从1而不是0开始

如何从1而不是0开始排列键?

Terraform Range 函数从 1 而不是 0 开始

XSL如果元素的计数为0,则向用户显示一条消息

在servicestack中使用protobuf,为什么order只能从1开始而不是0?

从末尾索引列表时,为什么Python从索引-1(而不是0)开始?

如何使tmux会话从1而不是0计数?

如何使用xsl:number count作为xsl:if测试条件的条件?

跳过xsl:for-each迭代时,xsl:number递增

MSQL:如何获得0计数而不是1,并使用联接表或视图返回null

从1开始而不是列表的0开始计算索引

识别新值时从 1 开始计数

从1开始,当我最多可以使用N次时,我可以计数多远

在Java中使用System.nanoTime()时,为什么要使用t1-t0 <0,而不是t1 <t0

为什么javascript getMonth从0开始计数,getDate从1开始计数?