XSLT to Implement Parent Child Relationship

时间:2018-04-20 21:18:57

标签: xslt

Below is my input and desired output and looking for XSLT 1.0 code to achieve the desired output. I hope someone is out there you can help me.

Input:

<invoices>
    <invoice>
        <INVOICE_ID>6766009</INVOICE_ID>
        <LINE_NUMBER>1</LINE_NUMBER>
    </invoice>
    <invoice>
        <INVOICE_ID>6766009</INVOICE_ID>
        <LINE_NUMBER>2</LINE_NUMBER>
    </invoice>
    <invoice>
        <INVOICE_ID>6766009</INVOICE_ID>
        <LINE_NUMBER>3</LINE_NUMBER>
    </invoice>
        <invoice>
        <INVOICE_ID>6766010</INVOICE_ID>
        <LINE_NUMBER>1</LINE_NUMBER>
    </invoice>
    <invoice>
        <INVOICE_ID>6766010</INVOICE_ID>
        <LINE_NUMBER>2</LINE_NUMBER>
    </invoice>
</invoices>

Desired Output:

<invoices>
    <invoice>
        <INVOICE_ID>6766009</INVOICE_ID>
        <invoicelines>
            <invoiceline>
                <LINE_NUMBER>1</LINE_NUMBER>
            </invoiceline>
            <invoiceline>
                <LINE_NUMBER>2</LINE_NUMBER>
            </invoiceline>
            <invoiceline>
                <LINE_NUMBER>3</LINE_NUMBER>
            </invoiceline>
        <invoicelines>
    </invoice>
    <invoice>
        <INVOICE_ID>6766010</INVOICE_ID>
        <invoicelines>
            <invoiceline>
                <LINE_NUMBER>1</LINE_NUMBER>
            </invoiceline>
            <invoiceline>
                <LINE_NUMBER>2</LINE_NUMBER>
            </invoiceline>
        </invoicelines>
    </invoice>  
</invoices>

I've tried few things but with no luck. I've not been successfully.

1 个答案:

答案 0 :(得分:1)

使用XSLT-1.0的最佳方法是Muenchian Grouping。这导致以下XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
<xsl:output method="xml"/>
<xsl:key name="inv" match="invoice" use="INVOICE_ID" />

    <xsl:template match="/invoices">
        <invoices>
            <xsl:for-each select="invoice[generate-id() = generate-id(key('inv',INVOICE_ID)[1])]">
                <invoice>
                    <INVOICE_ID><xsl:value-of select="INVOICE_ID" /></INVOICE_ID>
                    <invoicelines>
                        <xsl:for-each select="key('inv',INVOICE_ID)">
                            <invoiceline>
                                <LINE_NUMBER><xsl:value-of select="LINE_NUMBER"/></LINE_NUMBER>
                            </invoiceline>
                        </xsl:for-each>
                    </invoicelines>
                </invoice>
            </xsl:for-each>
        </invoices>
    </xsl:template>
</xsl:stylesheet>

输出为:

<?xml version="1.0"?>
<invoices>
    <invoice>
        <INVOICE_ID>6766009</INVOICE_ID>
        <invoicelines>
            <invoiceline>
                <LINE_NUMBER>1</LINE_NUMBER>
            </invoiceline>
            <invoiceline>
                <LINE_NUMBER>2</LINE_NUMBER>
            </invoiceline>
            <invoiceline>
                <LINE_NUMBER>3</LINE_NUMBER>
            </invoiceline>
        </invoicelines>
    </invoice>
    <invoice>
        <INVOICE_ID>6766010</INVOICE_ID>
        <invoicelines>
            <invoiceline>
                <LINE_NUMBER>1</LINE_NUMBER>
            </invoiceline>
            <invoiceline>
                <LINE_NUMBER>2</LINE_NUMBER>
            </invoiceline>
        </invoicelines>
    </invoice>
</invoices>