鉴于此 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>