XSL - 组和concat值

时间:2016-06-13 17:21:14

标签: xml xslt

我有以下XML:

 <Records>
        <Record>
            <ReportId>1111</ReportId>
            <ReportEntryId>2</ReportEntryId>
            <TaxAuthorityLabel>AA</TaxAuthorityLabel>
            <FutureUse />
            <FutureUse32 />
        </Record>

        <Record>
            <ReportId>1111</ReportId>
            <ReportEntryId>2</ReportEntryId>
            <TaxAuthorityLabel>ZZ</TaxAuthorityLabel>
            <FutureUse />
            <FutureUse32 />
        </Record>

        <Record>
            <ReportId>1111</ReportId>
            <ReportEntryId>3</ReportEntryId>
            <TaxAuthorityLabel>XX</TaxAuthorityLabel>
            <FutureUse />
            <FutureUse32 />
        </Record>

        <Record>
            <ReportId>1111</ReportId>
            <ReportEntryId>3</ReportEntryId>
            <TaxAuthorityLabel>EE</TaxAuthorityLabel>
            <FutureUse />
            <FutureUse32 />
        </Record>
    </Records>

我需要在FutureUse下为每个唯一的ReportEntryId值分组/连接所有TaxAuthorityLabel值。需要改变的是:

<Records>
        <ReportId>1111</ReportId>
        <ReportEntryId>2</ReportEntryId>
        <TaxAuthorityLabel>AA</TaxAuthorityLabel>
        <FutureUse>AA_ZZ</FutureUse>
        <FutureUse32 />
    </Records>

    <Records>
        <ReportId>1111</ReportId>
        <ReportEntryId>2</ReportEntryId>
        <TaxAuthorityLabel>ZZ</TaxAuthorityLabel>
        <FutureUse>AA_ZZ</FutureUse>
        <FutureUse32 />
    </Records>

    <Records>
        <ReportId>1111</ReportId>
        <ReportEntryId>3</ReportEntryId>
        <TaxAuthorityLabel>XX</TaxAuthorityLabel>
        <FutureUse>XX_EE</FutureUse>
        <FutureUse32 />
    </Records>

    <Records>
        <ReportId>1111</ReportId>
        <ReportEntryId>3</ReportEntryId>
        <TaxAuthorityLabel>EE</TaxAuthorityLabel>
        <FutureUse>XX_EE</FutureUse>
        <FutureUse32 />
    </Records>

我已在所有节点下汇总所有TaxAuthorityLabel值的XSL。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mapping="mapping:mapping" exclude-result-prefixes="mapping">
    <xsl:output method="xml"
                encoding="UTF-8"
                omit-xml-declaration="no"
                indent="yes" />

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="FutureUse"> <!-- we are setting every FutureUse -->
        <xsl:for-each select="../ReportEntryId" >
             <FutureUse>
                <xsl:if test=". = 2" >
                    <xsl:for-each select="../../Records/TaxAuthorityLabel" >
                        <xsl:value-of select="." />
                        <xsl:value-of select="'_'" />
                    </xsl:for-each>
                </xsl:if>
            </FutureUse>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我发现很难获得ReportEntryId的先前值,所以我可以比较它们。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我建议你这样试试:

XSLT 1.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:key name="record-by-entry" match="Record" use="ReportEntryId" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="FutureUse">
    <xsl:copy>
        <xsl:for-each select="key('record-by-entry', ../ReportEntryId)" >
            <xsl:value-of select="TaxAuthorityLabel" />
            <xsl:if test="position()!=last()">
                <xsl:text>_</xsl:text>
            </xsl:if>
        </xsl:for-each> 
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>