Search code examples
xsltxslt-1.0xslt-2.0xslt-grouping

Grouping xml recordswith required child elements based on one element in xslt


I am trying to group XML records based on the condition that one field is having equal data in remaining records. We will receive the A_ROW details which contain the document num and I have group the B_ROW records under A_ROW which has same document number. Can anyone help please to read and group xml records in xslt.

<recordSet>
<A_Row><company_code>1234</company_code>
        <document_num>5606</document_num>
        <document_type>CS</document_type>
        <doc_date>20190206</doc_date>
</A_Row>
<B_Row>
<document_num>5606</document_num>
        <pos>001</pos>
        <account>4564343</account>
        <account_type>ss</account_type>
</B_Row>
<B_Row>
<document_num>5606</document_num>
        <pos>001</pos>
        <account>4564344</account>
        <account_type>ss</account_type>
</B_Row>
<A_Row><company_code>1234</company_code>
        <document_num>5607</document_num>
        <document_type>CS</document_type>
        <doc_date>20190206</doc_date>
</A_Row>
<B_Row>
<document_num>5607</document_num>
        <pos>001</pos>
        <account>4564346</account>
        <account_type>ss</account_type>
</B_Row>
<B_Row>
<document_num>5607</document_num>
        <pos>001</pos>
        <account>4564342</account>
        <account_type>ss</account_type>
</B_Row>
<A_Row>
    <company_code>1234</company_code>
        <document_num>5608</document_num>
        <document_type>CS</document_type>
        <doc_date>20190206</doc_date>
</A_Row>
<B_Row>
<document_num>5608</document_num>
        <pos>001</pos>
        <account>4564349</account>
        <account_type>ss</account_type>
</B_Row>


</recordSet>

XSLT code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns="http://springer.com/xi/SAP/MEDIASUITE">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Recordset">
        <xsl:for-each select="BT_ROW">
            <xsl:element name="BT_ROW">
                <xsl:apply-templates/>
                <xsl:variable name="document" select="/ns:MT_MEDIASUITE_ORDER_DATA/Recordset/BT_ROW/document_number"/>
                <xsl:for-each select="/ns:MT_MEDIASUITE_ORDER_DATA/Recordset/BS_ROW">
                    <xsl:if test="/ns:MT_MEDIASUITE_ORDER_DATA/Recordset/BS_ROW/document_number =$document">

                    <xsl:element name="BS_ROW">
                    <xsl:apply-templates/>
                    </xsl:element>

                    </xsl:if>
                </xsl:for-each>

            </xsl:element>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Expected result:

<recordSet>
<A_Row><company_code>1234</company_code>
        <document_num>5606</document_num>
        <document_type>CS</document_type>
        <doc_date>20190206</doc_date>
    <B_Row>
        <document_num>5606</document_num>
            <pos>001</pos>
            <account>4564343</account>
            <account_type>ss</account_type>
    </B_Row>
    <B_Row>
        <document_num>5606</document_num>
            <pos>001</pos>
            <account>4564344</account>
            <account_type>ss</account_type>
    </B_Row>
</A_Row>

<A_Row>
<company_code>1234</company_code>
        <document_num>5607</document_num>
        <document_type>CS</document_type>
        <doc_date>20190206</doc_date>
    <B_Row>
        <document_num>5607</document_num>
            <pos>001</pos>
            <account>4564346</account>
            <account_type>ss</account_type>
    </B_Row>
    <B_Row>
        <document_num>5607</document_num>
            <pos>001</pos>
            <account>4564342</account>
            <account_type>ss</account_type>
    </B_Row>
</A_Row>

<A_Row>
    <company_code>1234</company_code>
        <document_num>5608</document_num>
        <document_type>CS</document_type>
        <doc_date>20190206</doc_date>
    <B_Row>
       <document_num>5608</document_num>
           <pos>001</pos>
           <account>4564349</account>
           <account_type>ss</account_type>
    </B_Row>

</A_Row>

</recordSet>

Solution

  • I have used below code and it worked fine

    <xsl:element name="A_ROW">
    				<xsl:apply-templates/>
    				<xsl:variable name="doc_num" select="document_number"/>
    				<xsl:variable name="pos1" select="fn:position()"/>
    				<xsl:for-each select="//B_ROW">
    					<xsl:variable name="pos" select="fn:position()"/>
    					<xsl:if test="//B_ROW[$pos]/document_number=$doc_num">
    						<xsl:element name="BS_ROW">
    							<xsl:apply-templates/>
    						</xsl:element>
    					</xsl:if>
    				</xsl:for-each>
    	</xsl:element>