Q:

Xslt : From XML to CSV where we have multiple root items that are reoccuring

I have this data in xml format where there are two root tags occurring many times. These two roots are connected by the key element. Meaning the half of the root1 data is in root2.

<sample_static>

    <root1>
        <elemnent1_key>1112</element1_key>
        <elemnent2>boss</elemnent2>
        <elemnent3>movie</elemnent3>
        <elemnent4>entertainment</elemnent4>
    </root1>
    <root1>
        <elemnent1_key>1113</element1_key>
        <elemnent2>king</elemnent2>
        <elemnent3>position</elemnent3>
        <elemnent4>entertainer</elemnent4>
    </root1>
    <root1>
        <elemnent1_key>1114</element1_key>
        <elemnent2>employee</elemnent2>
        <elemnent3>human</elemnent3>
        <elemnent4>puppet</elemnent4>
    </root1>
    .
    .
    .
    .

    <root2>
        <elemnent5_key>1112</element5_key>
        <elemnent6>good</elemnent6>
        <elemnent7>hit</elemnent7>
        <elemnent8>earning</elemnent8>
    </root2>
    <root2>
        <elemnent5_key>1113</element5_key>
        <elemnent6>loser</elemnent6>
        <elemnent7>duffer</elemnent7>
        <elemnent8>history</elemnent8>
    </root2>
    <root2>
        <elemnent5_key>1114</element5_key>
        <elemnent6>onsite</elemnent6>
        <elemnent7>rejected</elemnent7>
        <elemnent8>switch</elemnent8>
    </root2>
    .
    .
    .
    .

</sample_static>

Desired OUTPUT: 
SO I want to make a csv file in the below format using XSLT mapping.
element1_key|elemnent2|elemnent3|elemnent4|elemnent6|elemnent7|elemnent8

1112|boss|movie|entertainment|good|hit|earning

1113|king|position|entertainer|loser|duffer|history

1114|employee|human|puppet|onsite|rejected|switch

I want to connect the root 1 element and root2 element and form a single row using the XSLT only.
NOTE: There are many ROOT1 one tags and ROOT2 tags .. for sample i have give 3 root1 and 3 root2 tags to demonstrate the connection.
Root1's first data can be connected using element1_key 1112 with element5_key 1112
How do we do that? any idea

A:

This is rather simple to accomplish by using a key:
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:key name="root2" match="root2" use="element5_key" />

<xsl:template match="/sample_static">
    <xsl:for-each select="root1">
        <xsl:for-each select="*|key('root2', element1_key)/*[not(self::element5_key)] ">
            <xsl:value-of select="." />
            <xsl:if test="position()!=last()">
                <xsl:text>|</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:if test="position()!=last()">
            <xsl:text>&#10;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Edit

if there is no root2 for any root 1 the field should be blank.. for
  example if one root data is like 1113|a|b|c|d|e|f the other can be
  like 1114|x|y|||||

If you know in advance how many elements there are in root2, you can do:
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:key name="root2" match="root2" use="element5_key" />

<xsl:template match="/sample_static">
    <xsl:for-each select="root1">
        <xsl:for-each select="*">
            <xsl:value-of select="." />
            <xsl:text>|</xsl:text>
        </xsl:for-each>
        <xsl:variable name="root2" select="key('root2', element1_key)"/>
        <xsl:choose>
            <xsl:when test="$root2">
                <xsl:for-each select="$root2/*[not(self::element5_key)]">
                    <xsl:value-of select="." />
                    <xsl:if test="position()!=last()">
                        <xsl:text>|</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>|||</xsl:otherwise>
        </xsl:choose>
        <xsl:if test="position()!=last()">
            <xsl:text>&#10;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

