2

So I have an xml file like this:

<?xml version="1.0"?>
<class>

    <students>
        <name origin="English" firstname="Jeff" lastname="Richards"/>
        <name origin="French" firstname="Martel" lastname="Francois"/>
    </students>

    <teachers>
        <name origin="Spanish" firstname="Enrique" lastname="Rosa"/>
    </teachers>

</class>

And have another xml file like this:

 <?xml version="1.0"?>
    <name origin="English" firstname="Richard" lastname="Priestly"/>
    <name origin="Russian" firstname="Alexey" lastname="Romanov"/>

Using xslt, how can I add the two elements in the second file into the student element in the first file? In other words, how can I create a file that looks like this:

<?xml version="1.0"?>
    <class>

        <students>
            <name origin="English" firstname="Jeff" lastname="Richards"/>
            <name origin="French" firstname="Martel" lastname="Francois"/>
            <name origin="English" firstname="Richard" lastname="Priestly"/>
            <name origin="Russian" firstname="Alexey" lastname="Romanov"/>
        </students>

        <teachers>
            <name origin="Spanish" firstname="Enrique" lastname="Rosa"/>
        </teachers>

    </class>

If it is not possible using xslt, is it doable using XPath?

Thanks a bunch!

Hash
  • 7,726
  • 9
  • 34
  • 53
user2300040
  • 83
  • 1
  • 4
  • Check this http://stackoverflow.com/questions/15194718/how-to-merge-two-xml-files-with-xslt and your XML examples are not valid. The second don't have a root node, and the last one you miss the `` tag – X-Pippes Oct 15 '13 at 23:27
  • Your second document is not an XML file. Once you fix that, you could use the `document()` function to parse and process the second file and include it's content as part of the transformation of the first XML file. – Mads Hansen Oct 15 '13 at 23:29

1 Answers1

5

Here is one way, assuming you make the second file well-formed by adding a 'students' root node and name it 'students.xml':

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output indent="yes" method="xml"/>

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

<xsl:template match="students">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:for-each select="document('students.xml')/students/name">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
smj
  • 1,264
  • 1
  • 7
  • 14
  • I tried this and am getting the following error--Error:The processing instruction target matching "[xX][mM][lL]" is not allowed.' – user2300040 Oct 16 '13 at 01:50
  • Never mind. It was because one of my files had a space before the xml header began. Thank you!! – user2300040 Oct 16 '13 at 01:52
  • Is there any way to copy it so that it indents properly. Using the above, the name elements are shifted all the way to the left. I would like them to be indented. – user2300040 Oct 17 '13 at 23:57
  • You are missing the xsl:stylesheet end tag. – 4thex Sep 12 '19 at 13:07
  • @4thex sorry about that. The closing tag was there but it was hidden by code formatting. Fixed now. – smj Oct 01 '19 at 19:09