XSLT行计数器不包括Null和零
这些行是我的输入。我希望输出排除@Amount为“”或0的行。当@Amount=''和=0时,也不计算行数。 预期结果:XSLT行计数器不包括Null和零,xslt,Xslt,这些行是我的输入。我希望输出排除@Amount为“”或0的行。当@Amount=''和=0时,也不计算行数。 预期结果: <Rows> <Row RowNo="1" Amount="0,0"/> <Row RowNo="2" Amount="12"/> <Row RowNo="3" Amount=""/> <Row RowNo="4" Amount="00"/> <Row RowNo="5"
<Rows>
<Row RowNo="1" Amount="0,0"/>
<Row RowNo="2" Amount="12"/>
<Row RowNo="3" Amount=""/>
<Row RowNo="4" Amount="00"/>
<Row RowNo="5" Amount="33"/>
<Row RowNo="6" Amount="0,00"/>
<Row RowNo="7" Amount="0"/>
<Row RowNo="8" Amount="00,2"/>
</Rows>
如何做到这一点?对于给定的输入,XSLT 1.0中特定的解决方案(基于输入)可以如下所示:
<Rows>
<Row RowNo="1" Amount="12"/>
<Row RowNo="2" Amount="33"/>
<Row RowNo="3" Amount="00,2"/>
</Rows>
换个口味,简单点怎么样 XSLT1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Rows>
<xsl:for-each select="Rows/Row">
<xsl:choose>
<xsl:when test="@Amount != '' and (number(@Amount) * 1) != '0' and translate(@Amount, '0', '') != ','">
<xsl:copy>
<xsl:attribute name="RowNo">
<xsl:value-of select="count(preceding-sibling::*[@Amount != '' and (number(@Amount) * 1) != '0' and translate(@Amount, '0', '') != ',']) + 1" />
</xsl:attribute>
<xsl:for-each select="@*">
<xsl:if test="name() != 'RowNo'">
<xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</Rows>
</xsl:template>
要学习XSLT,首先应该学习XPath作为表达式语言,然后至少知道如何选择要输出的行
元素,或者根据您的方法选择不想输出的元素。可以通过多种方式对项目进行计数/编号,通常使用xsl:number
,在简单的情况下,只处理您感兴趣的项目,然后使用position()
生成数字。因此,试一试。我看到的唯一稍微复杂的地方是数字格式,如果您使用逗号,
而不是点,
作为十进制分隔符,则只有在输入值首先转换或依赖字符串比较的情况下,才可能使用XSLT/XPath中的数字类型进行直接比较。位置()的问题这里是它给了我输入的位置。我的尝试结果如下:
您能否编辑您的问题以显示您的尝试?非常感谢。你应该向我们展示你的XSLT尝试,这样我们就可以提出修复它的方法,所以考虑编辑你的问题并向我们展示你的当前代码。如果您使用正确的XPath,如行[@Amount and not(number(translate(@Amount,,,,,,,))=0)]
和为每个应用模板或,以排除您不想从处理中输出的行
元素,那么position()
应该为您提供正确的值。谢谢大家。非常感谢!
<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:strip-space elements="*"/>
<xsl:template match="/Rows">
<xsl:copy>
<xsl:for-each select="Row[translate(@Amount, ',0', '')]">
<Row RowNo="{position()}" Amount="{@Amount}"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>