xmlxslttransformation

XSLT - Merge One Header with One LineItem to One Header Multiple Line Items


Need to merge Line Ids with same Header ID under one Header

please see Below example , there are two lines with same header ID , need to merge the lines with same header id under one header. please see sample input and requested out

Input:

<Header>
<ackProcess>Error</ackProcess>
<headerId>2005178919</headerId>
<Line>
    <LineId>112864555</LineId>
    <errorMessage>To Organization is not valid</errorMessage>
</Line>
</Header>
<Header>
<ackProcess>Error</ackProcess>
<headerId>2005178537</headerId>
<Line>
    <LineId>112864083</LineId>
    <errorMessage>To Organization is not valid </errorMessage>
</Line>
</Header>
<Header>
<ackProcess>Error</ackProcess>
<headerId>2005178537</headerId>
<Line>
    <LineId>112864084</LineId>
    <errorMessage>From Organization is not valid</errorMessage>
</Line>
</Header>

Requested Output

<Header>
<ackProcess>Error</ackProcess>
<headerId>2005178919</headerId>
<Line>
    <LineId>112864555</LineId>
    <errorMessage>To Organization is not valid</errorMessage>
</Line>
</Header>
<Header>
<ackProcess>Error</ackProcess>
<headerId>2005178537</headerId>
<Line>
    <LineId>112864083</LineId>
    <errorMessage>To Organization is not valid </errorMessage>
</Line>
<Line>
    <LineId>112864084</LineId>
    <errorMessage>From Organization is not valid</errorMessage>
</Line>
</Header>

Solution

  • Based on 'Adding an Element to Several Groups' from

    https://www.w3.org/TR/xslt20/#grouping-examples

    I used

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
       <xsl:template match="ErrorList">
          <xsl:for-each-group select="Header" group-by="headerId">
             <header>
                <xsl:copy-of select="ackProcess"/>
                <xsl:copy-of select="headerId"/>
                <xsl:for-each select="current-group()">
                   <xsl:copy-of select="Line"/>
                </xsl:for-each>
             </header>
          </xsl:for-each-group>
       </xsl:template>
    </xsl:stylesheet>
    

    on

    <ErrorList>
    <Header>
    <ackProcess>Error</ackProcess>
    <headerId>2005178919</headerId>
    <Line>
        <LineId>112864555</LineId>
        <errorMessage>To Organization is not valid</errorMessage>
    </Line>
    </Header>
    <Header>
    <ackProcess>Error</ackProcess>
    <headerId>2005178537</headerId>
    <Line>
        <LineId>112864083</LineId>
        <errorMessage>To Organization is not valid </errorMessage>
    </Line>
    </Header>
    <Header>
    <ackProcess>Error</ackProcess>
    <headerId>2005178537</headerId>
    <Line>
        <LineId>112864084</LineId>
        <errorMessage>From Organization is not valid</errorMessage>
    </Line>
    </Header>
    </ErrorList>
    

    at https://linangdata.com/xslt-tester/ to get

    <?xml version="1.0" encoding="UTF-8"?><header><ackProcess>Error</ackProcess><headerId>2005178919</headerId><Line>
        <LineId>112864555</LineId>
        <errorMessage>To Organization is not valid</errorMessage>
    </Line></header><header><ackProcess>Error</ackProcess><headerId>2005178537</headerId><Line>
        <LineId>112864083</LineId>
        <errorMessage>To Organization is not valid </errorMessage>
    </Line><Line>
        <LineId>112864084</LineId>
        <errorMessage>From Organization is not valid</errorMessage>
    </Line></header>
    

    as required up to formatting.