在我的样式表中,我有一个xsl:变量,用于查看XML,查看对象列表并返回每个对象的价格值的总和,示例代码如下:
<xsl:variable name="ExampleTotal">
<xsl:value-of select="sum(/ListOfObjects/Object/PriceOfObject)"/>
</xsl:variable>
这个很好,我的问题是,我不需要列表中每个对象的和,只需要包含true值的对象。
XML示例:
<ListOfObjects>
<Object>
<PriceOfObject>35.00</PriceOfObject>
<PriceNeeded>true</PriceNeeded>
<OtherData>example</OtherData>
</Object>
<Object>
<PriceOfObject>36.00</PriceOfObject>
<PriceNeeded>true</PriceNeeded>
<OtherData>example</OtherData>
</Object>
<Object>
<PriceOfObject>35.00</PriceOfObject>
<PriceNeeded>true</PriceNeeded>
<OtherData>example</OtherData>
</Object>
<Object>
<PriceOfObject>33.00</PriceOfObject>
<PriceNeeded>false</PriceNeeded>
<OtherData>example</OtherData>
</Object>
</ListOfObjects>
是否有可能遍历对象列表并仅将值为true的对象的总数相加?
试试这个:
<xsl:variable name="ExampleTotal">
<xsl:value-of select="sum(/ListOfObjects/Object[PriceNeeded='true']/PriceOfObject)"/>
</xsl:variable>
以上代码按照Michael Kay的建议修改,
<xsl:variable name="ExampleTotal"
select="sum(/ListOfObjects/Object[PriceNeeded='true']/PriceOfObject)"/>