Xml 循环和求和XSLT中不同标记值的值
我的XSLT有一个问题,总结中的总数不会显示出来,有人能帮我吗 我有这个XMLXml 循环和求和XSLT中不同标记值的值,xml,xslt-1.0,Xml,Xslt 1.0,我的XSLT有一个问题,总结中的总数不会显示出来,有人能帮我吗 我有这个XML <?xml version="1.0" encoding="utf-8"?> <Document> <Header> <Year>2019</Year> <Month>08</Month> <DocNumber>1</DocNumber> <Company>
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Header>
<Year>2019</Year>
<Month>08</Month>
<DocNumber>1</DocNumber>
<Company>
<Name>My Company</Name>
<City>City</City>
<Country>Country</Country>
</Company>
<Items>
<Item>
<Type>A</Type>
<Name>Item 2</Name>
<Amount>20</Amount>
<Quantity>2</Quantity>
<Discount>3</Discount>
</Item>
<Item>
<Type>B</Type>
<Name>Item 3</Name>
<Amount>40</Amount>
<Quantity>1</Quantity>
<Discount>3</Discount>
</Item>
<Item>
<Type>C</Type>
<Name>Item 1</Name>
<Amount>30</Amount>
<Quantity>2</Quantity>
<Discount>4</Discount>
</Item>
</Items>
</Header>
</Document>
这是XSLT1.0
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="noDisplay" select="'|B|D|'" />
<xsl:template match="Document">
<Root>
<xsl:apply-templates select="Header" />
</Root>
</xsl:template>
<xsl:template match="Header">
<xsl:variable name="iTotQty" select ="sum(Items/Item/Quantity)" />
<xsl:variable name="iTotAmt" select ="sum(Items/Item/Amount)" />
<xsl:variable name="iTotDisc" select ="sum(Items/Item/Discount)" />
<Summary>
<xsl:attribute name="Year">
<xsl:value-of select ="Year" />
</xsl:attribute>
<xsl:attribute name="Month">
<xsl:value-of select ="Month" />
</xsl:attribute>
<xsl:attribute name="Number">
<xsl:value-of select ="DocNumber" />
</xsl:attribute>
<xsl:attribute name="Number">
<xsl:value-of select ="DocNumber" />
</xsl:attribute>
<xsl:attribute name="Number">
<xsl:value-of select ="DocNumber" />
</xsl:attribute>
<xsl:attribute name="Number">
<xsl:value-of select ="DocNumber" />
</xsl:attribute>
<xsl:attribute name="TotalQty">
<xsl:value-of select ="$iTotQty" />
</xsl:attribute>
<xsl:attribute name="TotalAmt">
<xsl:value-of select ="$iTotAmt" />
</xsl:attribute>
<xsl:attribute name="TotalDisc">
<xsl:value-of select ="$iTotDisc" />
</xsl:attribute>
<xsl:apply-templates select="Items/Item" />
</Summary>
</xsl:template>
<xsl:template match="Items/Item">
<xsl:variable name="bNoDisplay" select ="contains($noDisplay, concat('|', Type, '|'))" />
<Item>
<xsl:attribute name="Type">
<xsl:value-of select ="Type" />
</xsl:attribute>
<xsl:attribute name="Name">
<xsl:value-of select ="Name" />
</xsl:attribute>
<xsl:if test ="not($bNoDisplay)">
<xsl:attribute name="Qty">
<xsl:value-of select ="Quantity" />
</xsl:attribute>
<xsl:attribute name="Amt">
<xsl:value-of select ="Amount" />
</xsl:attribute>
<xsl:attribute name="Disc">
<xsl:value-of select ="Discount" />
</xsl:attribute>
</xsl:if>
</Item>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
其结果是:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Summary Year="2019" Month="08" Number="1" TotalQty="5" TotalAmt="90" TotalDisc="10">
<Item Type="A" Name="Item 2" Qty="2" Amt="20" Disc="3" />
<Item Type="B" Name="Item 3" />
<Item Type="C" Name="Item 1" Qty="2" Amt="30" Disc="4" />
</Summary>
</Root>
汇总的条件是,它只应汇总项目中显示的所有数据数量、金额和折扣,如果未显示,则不应作为汇总的一部分,如果数据数量、金额和折扣根本不包含任何内容或结果为0,则不应显示
我只能使用XSLT 1.0,因此任何人都可以更正我的样式表,谢谢考虑以下样式表,最小化以解决您编辑的问题中出现的问题: XSLT1.0 应用于输入XML示例时,结果将是: 结果
嗨,Michael,我编辑了我的示例,似乎昨晚发布时我对结果感到困惑,标题应该是文档的唯一子级,在结果中,摘要应该是Root的唯一子级,所以问题是总数总是把它作为一个整体来求和,而不是去掉数据中隐藏的数量Total@zyberjock这是一个全新的问题,与我最初的回答完全不同。您应该将其作为新问题发布,而不是编辑。您好,Michael,谢谢,但是如果所有树项都是type='B',这将导致不显示金额,我如何隐藏所有0或使用{}创建的任何空属性?您可以使用有条件地输出它们。
<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:param name="noDisplay" select="'|B|D|'" />
<xsl:template match="/Document">
<Root>
<xsl:apply-templates select="Header" />
</Root>
</xsl:template>
<xsl:template match="Header">
<xsl:variable name="displayed-items" select="Items/Item[not(contains($noDisplay, concat('|', Type, '|')))]" />
<Summary Year="{Year}" Month="{Month}" Number="{DocNumber}" TotalQty="{sum($displayed-items/Quantity)}" TotalAmt="{sum($displayed-items/Amount)}" TotalDisc="{sum($displayed-items/Discount)}">
<!-- omitted -->
</Summary>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Summary Year="2019" Month="08" Number="1" TotalQty="4" TotalAmt="50" TotalDisc="7"/>
</Root>