这是源 xml。 我们有 1 个标头级别标签。 行级别标签包含在 1 个标题级别下。
<Tran>
<Company>ABC corp</Company>
<Country>IN</Country>
<Line>
<No>1</No>
<Type>Item</Type>
<Amount>100</Amount>
</Line>
<Line>
<No>2</No>
<Type>Item</Type>
<Amount>200</Amount>
</Line>
<Line>
<No>3</No>
<Type>Freight</Type>
<Amount>50</Amount>
</Line>
<Line>
<No>4</No>
<Type>Freight</Type>
<Amount>60</Amount>
</Line>
<Line>
<No>5</No>
<Type>Tax</Type>
<Amount>100</Amount>
</Line>
<Tran>
我想按标题级别
<Company>
和行级别 <Type>
进行分组。
但不知道如何按xsl分组。
预期结果是按标题级别和行级别拆分标题级别。 预期的结果就在这里。
<Pmtheader>
<Comp>ABC corp</Comp>
<Type>Item</Type>
<CountLine>2</CountLine>
<TotalAmt>300</TotalAmt>
<Pmtline>
<No>1</No>
<Amt>100</Amt>
</Pmtline>
<Pmtline>
<No>2</No>
<Amt>200</Amt>
</Pmtline>
</Pmtheader>
<Pmtheader>
<Comp>ABC corp</Comp>
<Type>Freight</Type>
<CountLine>2</CountLine>
<TotalAmt>110</TotalAmt>
<Pmtline>
<No>3</No>
<Amt>50</Amt>
</Pmtline>
<Pmtline>
<No>4</No>
<Amt>60</Amt>
</Pmtline>
<Pmtheader>
<Comp>ABC corp</Comp>
<Type>Tax</Type>
<CountLine>1</CountLine>
<TotalAmt>100</TotalAmt>
<Pmtline>
<No>5</No>
<Amt>100</Amt>
</Pmtline>
</Pmtheader>
我从不使用 xsl:for-each-group 和 for-each 函数。 所以我不确定从哪里开始。 请给我任何建议。
这是使用 XSLT 3 的示例:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="/" name="xsl:initial-template">
<xsl:copy>
<xsl:for-each-group select="//Tran/Line" composite="yes" group-by="../Company, Type">
<Pmtheader>
<Comp>{current-grouping-key()[1]}</Comp>
<Type>{current-grouping-key()[2]}</Type>
<CountLine>{count(current-group())}</CountLine>
<TotalAmt>{sum(current-group()/Amount)}</TotalAmt>
<xsl:apply-templates select="current-group()"/>
</Pmtheader>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="Line">
<Pmtline>
<xsl:apply-templates/>
</Pmtline>
</xsl:template>
<xsl:template match="Line/Type"/>
</xsl:stylesheet>