Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xslt - How to merge multiple xml files using xsl?

I need to merge below 3 xml files uisng xsl in 1.0

In XML FILE : 01.xml

<xmlResponse>
    <Person>
        <FirstName>FirstName_1</FirstName>
        <LastName>LastName_1</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_2</FirstName>
        <LastName>LastName_2</LastName>
    </Person>
</xmlResponse>

In XML FILE : 02.xml

<xmlResponse>
    <Person>
        <FirstName>FirstName_2</FirstName>
        <LastName>LastName_2</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_3</FirstName>
        <LastName>LastName_3</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_4</FirstName>
        <LastName>LastName_4</LastName>
    </Person>
</xmlResponse>

In XML FILE : 03.xml

<xmlResponse>
    <Person>
        <FirstName>FirstName_5</FirstName>
        <LastName>LastName_5</LastName>
    </Person>
</xmlResponse>

I need output like below (01.xml + 02.xml + 03.XML)

<xmlResponse>
    <Person>
        <FirstName>FirstName_1</FirstName>
        <LastName>LastName_1</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_2</FirstName>
        <LastName>LastName_2</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_2</FirstName>
        <LastName>LastName_2</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_3</FirstName>
        <LastName>LastName_3</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_4</FirstName>
        <LastName>LastName_4</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_5</FirstName>
        <LastName>LastName_5</LastName>
    </Person>
</xmlResponse> 

hoping your response, tks...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use document function:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/xmlResponse">
        <xsl:copy>
            <xsl:apply-templates select="Person"/>
            <xsl:apply-templates select="document('2.xml')/*/Person"/>
            <xsl:apply-templates select="document('3.xml')/*/Person"/>
        </xsl:copy>
    </xsl:template>

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...