gpt4 book ai didi

xslt - XSLT 1.0 中的递增计数器

转载 作者:行者123 更新时间:2023-12-04 23:56:04 26 4
gpt4 key购买 nike

这是输入 XML:

<Move-Afile>
<Afile>
<Item>
<PackNumber>1234</PackNumber>
</Item>
<Item>
<PackNumber>567</PackNumber>
</Item>
<Item>
<PackNumber>567</PackNumber>
</Item>
<Item>
<PackNumber>126</PackNumber>
</Item>
<Item>
<PackNumber>876</PackNumber>
</Item>
</Afile>
</Move-Afile>
<Item>是一个无界元素,包含 <PackNumber>作为子元素。对于每个包号,我们需要增加计数器变量,但这里存在一个条件,就像之前的 <PackNumber>等于当前 <PackNumber>我们必须像下面的输出一样忽略计数器(不需要增加)。

for-each , 我们可以得到像下面 XSLT 示例那样的计数器吗?

这是我的 XSLT 模板
<xsl:template match="/">
<A>
<target>
<xsl:for-each select="/inp1:Move-Afile/inp1:Afile/inp1:Item/inp1:PalletNumber">

<xsl:variable name="count">
<!-- get the count here-->
</xsl:variable>

<counter>$count</counter>
<PNumber><xsl:value-of select="."/></PNumber>

</xsl:for-each>
</target>
</A>
</xsl:template>

这是 XML 输出:
<A>
<target>
<Item>
<counter>1</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>1234</PNumber>
</Item>
<Item>
<counter>2</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>567</PNumber>
</Item>
<Item><!-- if previous <PackNumber> is equal to current <PackNumber> ignore the count-->no need to increment
<PNumber>567</PNumber>
</Item>
<Item>
<counter>3</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>126</PNumber>
</Item>
<Item><!-- if previous <PackNumber> is equal to current <PackNumber> ignore the count-->no need to increment
<PNumber>126</PNumber>
</Item>
</target>
</A>

XML 输出 2:
<?xml version="1.0"?>
<A>
<target>
<counter>1</counter>
<PNumber>1234</PNumber>
<counter>2</counter>
<PNumber>567</PNumber>
<!-- IF PNumber is equal we have to ignore the Total loop -->
<counter>3</counter>
<PNumber>126</PNumber>
<counter>4</counter>
<PNumber>876</PNumber>
</target>
</A>

最佳答案

Try this:

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

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="text()" />

<xsl:template match="PackNumber">
<xsl:if test="not(preceding::PackNumber =.)" >
<!-- if previous <PackNumber> is not equal to current <PackNumber> t-->

<counter>
<xsl:value-of select="count(preceding::PackNumber[not(preceding::PackNumber= .)])+1"/>
</counter>
</xsl:if>
<PNumber>
<xsl:value-of select="."/>
</PNumber>

</xsl:template>

<xsl:template match="/">
<A>
<target>
<xsl:apply-templates select="//Item"/>
</target>
</A>
</xsl:template>
</xsl:stylesheet>

更新 根据改变的问题:
在这里获取计数(在每个循环中作为变量)
尝试这个:
    <?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="text()" />

<xsl:template match="PackNumber" mode="count">
<xsl:value-of select="count(preceding::PackNumber
[not(preceding::PackNumber= .)and not( . = current()/. ) ])+1"/>
</xsl:template>

<xsl:template match="/">
<A>
<target>
<xsl:for-each select="//Item/PackNumber">
<xsl:variable name="count">
<xsl:apply-templates select="." mode="count"/>
</xsl:variable>
<counter>
<xsl:value-of select="$count"/>
</counter>
<PNumber>
<xsl:value-of select="."/>
</PNumber>
</xsl:for-each>
</target>
</A>
</xsl:template>
</xsl:stylesheet>

更新输出:
<?xml version="1.0"?>
<A>
<target>
<counter>1</counter>
<PNumber>1234</PNumber>
<counter>2</counter>
<PNumber>567</PNumber>
<counter>2</counter>
<PNumber>567</PNumber>
<counter>3</counter>
<PNumber>126</PNumber>
<counter>4</counter>
<PNumber>876</PNumber>
</target>
</A>

关于xslt - XSLT 1.0 中的递增计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16504727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com