-1

鉴于此 XML

<Employee>
  <Record>
    <ID>1</ID>
    <Amount>10</Amount>
  </Record>
  <Record>
    <ID>2</ID>
    <Amount>0</Amount>
  </Record>
  <Record>
    <ID>3</ID>
    <Amount>0</Amount>
  </Record>
  <Record>
    <ID>4</ID>
    <Amount>20</Amount>
  </Record>
  <Record>
    <ID>5</ID>
    <Amount>50</Amount>
  </Record>
  <Record>
    <ID>6</ID>
    <Amount>0</Amount>
  </Record>
  <Record>
    <ID>7</ID>
    <Amount>40</Amount>
  </Record>
</Employee>

使用 XSLT 1.0,我希望输出如下:

Zero amount node: 0
Zero amount node: 0
Zero amount node: 1
Zero amount node: 2
Zero amount node: 2
Zero amount node: 2
Zero amount node: 3

我想for-each在节点中使用 and 循环,Record并在每次迭代时输出包含 zero 的先前节点的数量Amount

我的 XSLT:

  <xsl:output method="text" indent="no"/>

  <xsl:template match="/">

  <xsl:for-each select="Employee/Record">

    <xsl:variable name="amount-so-far" select=". | preceding-sibling::Record"/>
    <xsl:variable name="amount-so-far-not-zero" select="$amount-so-far[not('0' = preceding-sibling::Record/Amount)]"/>
    <xsl:value-of select="count($amount-so-far-not-zero)"/>

    <xsl:text>
</xsl:text>

</xsl:for-each>

</xsl:template>
</xsl:stylesheet>
4

1 回答 1

1

试试这个 XSLT 以获得你想要的输出:

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

  <xsl:template match="Employee">
    <xsl:for-each select="Record">
      <xsl:value-of select="concat('Zero amount node: ', count(preceding-sibling::Record[Amount='0']))"/><xsl:text>

      </xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
于 2013-05-15T04:41:37.037 回答