同级计数器部分工作



早上好。似乎在 XSLT 中使用同级属性时遇到问题我的 XML 如下所示:

<Move-Afile>
<Afile>
    <Item>
        <PackNumber>C</PackNumber>
    </Item>
    <Item>
        <PackNumber>S</PackNumber>
    </Item>
    <Item>
        <PackNumber>C</PackNumber>
    </Item>
    <Item>
        <PackNumber>D</PackNumber>
    </Item>
    <Item>
        <PackNumber>D</PackNumber>
    </Item>
    <Item>
        <PackNumber>D</PackNumber>
    </Item>
    <Item>
        <PackNumber>C</PackNumber>
    </Item>
    <Item>
        <PackNumber>F</PackNumber>
    </Item>
    <Item>
        <PackNumber>C</PackNumber>
    </Item>
</Afile>
</Move-Afile>

我的 XSLT 看起来像这样:

<?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="text()" />  -->
<xsl:template match="PackNumber" mode="count">
    <xsl:choose>
        <xsl:when test="not(current()/. = preceding::PackNumber) ">
            <xsl:text>1</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="count(preceding::PackNumber
                [not(preceding::PackNumber= .)and not( . = current()/. ) ]) +1 "/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>    
<xsl:template match="/">
    <A>
        <target>
            <xsl:for-each select="//Item/PackNumber">
                <PNumber>
                    <xsl:value-of select="."/>
                </PNumber>
                <xsl:variable name="count">
                    <xsl:apply-templates select="." mode="count"/>
                </xsl:variable>
                <counter>
                    <xsl:value-of select="$count"/>
                </counter>
            </xsl:for-each>
        </target>
    </A>
</xsl:template>
</xsl:stylesheet>

运行时,问题从第二个 D 元素开始。 我无法确定是什么原因导致它不增加计数器。 任何帮助/建议不胜感激

假设我猜对了你在这里要完成的任务,难道不能简单地

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Move-Afile">
    <A>
        <target>
            <xsl:for-each select="Afile/Item">
                <PNumber>
                    <xsl:value-of select="PackNumber"/>
                </PNumber>
                <counter>
                    <xsl:value-of select="count(preceding-sibling::Item[PackNumber=current()/PackNumber]) + 1"/>
                </counter>
            </xsl:for-each>
        </target>
    </A>
</xsl:template>
</xsl:stylesheet>

导致:

<?xml version="1.0" encoding="UTF-8"?>
<A>
   <target>
      <PNumber>C</PNumber>
      <counter>1</counter>
      <PNumber>S</PNumber>
      <counter>1</counter>
      <PNumber>C</PNumber>
      <counter>2</counter>
      <PNumber>D</PNumber>
      <counter>1</counter>
      <PNumber>D</PNumber>
      <counter>2</counter>
      <PNumber>D</PNumber>
      <counter>3</counter>
      <PNumber>C</PNumber>
      <counter>3</counter>
      <PNumber>F</PNumber>
      <counter>1</counter>
      <PNumber>C</PNumber>
      <counter>4</counter>
   </target>
</A>

(我无法充分渗透您的代码以指出错误。

相关内容

  • 没有找到相关文章

最新更新