XSLT累加器-特定节点之间的节点数

时间:2020-08-12 14:33:37

标签: xml xslt xslt-2.0 xslt-3.0

我有以下xml模式:

<tag name="(some_string)_start">
<tag name="step1">
<tag name="step2">
<tag name="step3">
<tag name="step4">
<tag name="(some_string)_end">
<tag name="(some_other_string)_start">
<tag name="step1">
<tag name="step2">
<tag name="step3">
<tag name="step4">
<tag name="step5">
<tag name="(some_other_string)_end">

我需要使用XSLT将其转换为文本文件,如下所示:

name=(some_string), step_index=0
name=step1, step_index=1
name=step2, step_index=2
name=step3, step_index=3
name=step4, step_index=4
name=(some_string), step_index=5
name=(some_other_string), step_index=0
name=step1, step_index=1
name=step2, step_index=2
name=step3, step_index=3
name=step4, step_index=4
name=step5, step_index=5
name=(some_other_string), step_index=6

我要计算从后缀'start'到'end'的节点数 当我遇到带有后缀“ start”的名字时,计数器应重新初始化为零

以下是我尝试过的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="text" omit-xml-declaration="yes" indent="no"/>
    <xsl:accumulator name="step-index" initial-value="0">
        <xsl:accumulator-rule match="sample[matches(@name, '.*Start$')]" select="0">
        </xsl:accumulator-rule>
        <xsl:accumulator-rule match="sample[matches(@name, '.*End$')]" select="$value+1">
        </xsl:accumulator-rule>         
    </xsl:accumulator>
    <xsl:template match="tag">
        <xsl:text>,name=</xsl:text>
        <xsl:value-of select="@name"/>
        <xsl:text>,step-index=</xsl:text>
            <xsl:value-of select="accumulator-after('step-index')"/>
    </xsl:template>
</xsl:stylesheet>

但是使用上述累加器方法无法获得所需的输出。 如何使用XSLT计算step_index值?

谢谢!

1 个答案:

答案 0 :(得分:1)

正如我在评论中说的那样,您尚未解释您的方法是如何完全失败的,以及是否至少尝试使用XSLT 3处理器运行该XSLT 3功能。

一个最小的样本是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">
    
    <xsl:mode use-accumulators="step-index"/>

    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:accumulator name="step-index" initial-value="0">
        <xsl:accumulator-rule match="tag[ends-with(@name, '_start')]" select="0"/>
        <xsl:accumulator-rule match="tag[not(ends-with(@name, '_start'))]" select="$value + 1"/>
    </xsl:accumulator>
    
    <xsl:template match="tag">
        <xsl:text>name={@name => replace('_(start|end)$', '')},step-index={accumulator-after('step-index')}&#10;</xsl:text>
    </xsl:template>
  
</xsl:stylesheet>

使用Saxon 9.8 HE在https://xsltfiddle.liberty-development.net/ehW12fW/1上对结构良好的样品进行

<root>
<tag name="(some_string)_start"/>
<tag name="step1"/>
<tag name="step2"/>
<tag name="step3"/>
<tag name="step4"/>
<tag name="(some_string)_end"/>
<tag name="(some_other_string)_start"/>
<tag name="step1"/>
<tag name="step2"/>
<tag name="step3"/>
<tag name="step4"/>
<tag name="step5"/>
<tag name="(some_other_string)_end"/>
</root>

它输出

name=(some_string),step-index=0
name=step1,step-index=1
name=step2,step-index=2
name=step3,step-index=3
name=step4,step-index=4
name=(some_string),step-index=5
name=(some_other_string),step-index=0
name=step1,step-index=1
name=step2,step-index=2
name=step3,step-index=3
name=step4,step-index=4
name=step5,step-index=5
name=(some_other_string),step-index=6
相关问题