1
votes

I am very new to XSLT and trying to transform this XML:

<Company>
   <Employee>
       <name>Jane</name>
       <id>200</id>
       <title>Dir</title>
       <name>Joe</name>
       <id>100</id>
       <title>Mgr</title>
       <name>Sue</name>
       <id>300</id>
       <title>Analyst</title>
   </Employee>
 </Company>

To this expected output:

<Company>
   <Employee>
       <name>Jane</name>
       <id>200</id>
       <title>Dir</title>
   </Employee>
   <Employee>
       <name>Joe</name>
       <id>100</id>
       <title>Mgr</title>
   </Employee>
   <Employee>
       <name>Sue</name>
       <id>300</id>
       <title>Analyst</title>
   </Employee>
</Company>

Any help would be greatly appreciated, thanks!

1
Can one assume a uniform structure of {name; id; title}?michael.hor257k

1 Answers

1
votes

Assuming they always come in groups of three, you could do:

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:template match="/Company">
    <xsl:copy>
        <xsl:for-each select="Employee/name">
            <Employee>
                <xsl:copy-of select=". | following-sibling::id[1] | following-sibling::title[1]"/>
            </Employee>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Or more generic:

<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:param name="group-size" select="3" />

<xsl:template match="/Company">
    <xsl:copy>
        <xsl:for-each select="Employee/*[position() mod $group-size = 1]">
            <Employee>
                <xsl:copy-of select=". | following-sibling::*[position() &lt; $group-size]"/>
            </Employee>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>