Soham Shah
Soham Shah

Reputation: 571

Merging <Rowset> by XSLT

My XML is as below: (With Two <Rowset>)

<?xml version="1.0" encoding="utf-8"?>
<Rowsets xmlns:xalan="http://xml.apache.org/xalan">
               <Rowset Name="TankList">
                              <Columns>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="PLANT" SQLDataType="1" SourceColumn="PLANT"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="LGORT" SQLDataType="1" SourceColumn="LGORT"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="TANK" SQLDataType="1" SourceColumn="TANK"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="TANKTYPE" SQLDataType="1" SourceColumn="TANKTYPE"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="MATNR" SQLDataType="1" SourceColumn="MATNR"/>
                              </Columns>
                              <Row>
                                             <PLANT>Y111</PLANT>
                                             <LGORT>T101</LGORT>
                                             <TANK>T101</TANK>
                                             <TANKTYPE>OIL</TANKTYPE>
                                             <MATNR>111111</MATNR>
                              </Row>


 <Row>
                                                 <PLANT>Y111</PLANT>
                                                 <LGORT>T101</LGORT>
                                                 <TANK>T101</TANK>
                                                 <TANKTYPE>OIL</TANKTYPE>
                                                 <MATNR>222222</MATNR>
                                  </Row>
               </Rowset>
               <Rowset Name="DCS">
                              <Columns>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="DCSInventory" SQLDataType="1" SourceColumn="DCSInventory"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="DCSInventoryUOM" SQLDataType="1" SourceColumn="DCSInventoryUOM"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="DCSTank" SQLDataType="1" SourceColumn="DCSTank"/>
                              </Columns>
                              <Row>
                                             <DCSInventory>0193948</DCSInventory>
                                             <DCSInventoryUOM>GA</DCSInventoryUOM>
                                             <DCSTank>T101</DCSTank>
                              </Row>
               </Rowset>
</Rowsets>

My requirement is merge <Row> of <Rowset Name="DCS"> into other <Rowset Name="TankList">

My resultant XML should look as below:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
    <Rowset>
        <Columns xmlns:xalan="http://xml.apache.org/xalan">
            <Column Description="" MaxRange="1" MinRange="0" Name="PLANT" SQLDataType="1" SourceColumn="PLANT"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="LGORT" SQLDataType="1" SourceColumn="LGORT"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="TANK" SQLDataType="1" SourceColumn="TANK"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="TANKTYPE" SQLDataType="1" SourceColumn="TANKTYPE"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="MATNR" SQLDataType="1" SourceColumn="MATNR"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="DCSInventory" SQLDataType="1" SourceColumn="DCSInventory"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="DCSInventoryUOM" SQLDataType="1" SourceColumn="DCSInventoryUOM"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="DCSTank" SQLDataType="1" SourceColumn="DCSTank"/>
        </Columns>
        <Row>
            <PLANT xmlns:xalan="http://xml.apache.org/xalan">Y111</PLANT>
            <LGORT xmlns:xalan="http://xml.apache.org/xalan">T101</LGORT>
            <TANK xmlns:xalan="http://xml.apache.org/xalan">T101</TANK>
            <TANKTYPE xmlns:xalan="http://xml.apache.org/xalan">OIL</TANKTYPE>
            <MATNR xmlns:xalan="http://xml.apache.org/xalan">111111</MATNR>
            <DCSInventory xmlns:xalan="http://xml.apache.org/xalan">0193948</DCSInventory>
            <DCSInventoryUOM xmlns:xalan="http://xml.apache.org/xalan">GA</DCSInventoryUOM>
            <DCSTank xmlns:xalan="http://xml.apache.org/xalan">T101</DCSTank>
        </Row>
    <Row>
            <PLANT xmlns:xalan="http://xml.apache.org/xalan">Y111</PLANT>
            <LGORT xmlns:xalan="http://xml.apache.org/xalan">T101</LGORT>
            <TANK xmlns:xalan="http://xml.apache.org/xalan">T101</TANK>
            <TANKTYPE xmlns:xalan="http://xml.apache.org/xalan">OIL</TANKTYPE>
            <MATNR xmlns:xalan="http://xml.apache.org/xalan">222222</MATNR>
            <DCSInventory xmlns:xalan="http://xml.apache.org/xalan">0193948</DCSInventory>
            <DCSInventoryUOM xmlns:xalan="http://xml.apache.org/xalan">GA</DCSInventoryUOM>
            <DCSTank xmlns:xalan="http://xml.apache.org/xalan">T101</DCSTank>
        </Row>
    </Rowset>
</Rowsets>

The XSLT that I have used is as below:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
               <xsl:template match="/">
                              <Rowsets>
                                             <Rowset>
                                                            <xsl:for-each select="Rowsets/Rowset">
                                                                           <xsl:copy-of select="Columns"/>
                                                            </xsl:for-each>
                                                            <xsl:for-each select="Rowsets/Rowset/Row">
                                                                           <Row>
                                                                                          <xsl:for-each select="child::*">
                                                                                                         <xsl:copy-of select="."/>
                                                                                          </xsl:for-each>
                                                                           </Row>
                                                            </xsl:for-each>
                                             </Rowset>
                              </Rowsets>
               </xsl:template>
</xsl:stylesheet>

But the resultant XML does not merge <Row> and <Columns> instead it copies <Row> and <Columns> as extra node.

<?xml version="1.0" encoding="UTF-8"?><Rowsets>
    <Rowset>
        <Columns xmlns:xalan="http://xml.apache.org/xalan">
                                             <Column Description="" MaxRange="1" MinRange="0" Name="PLANT" SQLDataType="1" SourceColumn="PLANT"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="LGORT" SQLDataType="1" SourceColumn="LGORT"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="TANK" SQLDataType="1" SourceColumn="TANK"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="TANKTYPE" SQLDataType="1" SourceColumn="TANKTYPE"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="MATNR" SQLDataType="1" SourceColumn="MATNR"/>
                              </Columns>
        <Columns xmlns:xalan="http://xml.apache.org/xalan">
                                             <Column Description="" MaxRange="1" MinRange="0" Name="DCSInventory" SQLDataType="1" SourceColumn="DCSInventory"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="DCSInventoryUOM" SQLDataType="1" SourceColumn="DCSInventoryUOM"/>
                                             <Column Description="" MaxRange="1" MinRange="0" Name="DCSTank" SQLDataType="1" SourceColumn="DCSTank"/>
                              </Columns>
        <Row>
            <PLANT xmlns:xalan="http://xml.apache.org/xalan">Y111</PLANT>
            <LGORT xmlns:xalan="http://xml.apache.org/xalan">T101</LGORT>
            <TANK xmlns:xalan="http://xml.apache.org/xalan">T101</TANK>
            <TANKTYPE xmlns:xalan="http://xml.apache.org/xalan">OIL</TANKTYPE>
            <MATNR xmlns:xalan="http://xml.apache.org/xalan">111111</MATNR>
        </Row>
<Row>
                <PLANT xmlns:xalan="http://xml.apache.org/xalan">Y111</PLANT>
                <LGORT xmlns:xalan="http://xml.apache.org/xalan">T101</LGORT>
                <TANK xmlns:xalan="http://xml.apache.org/xalan">T101</TANK>
                <TANKTYPE xmlns:xalan="http://xml.apache.org/xalan">OIL</TANKTYPE>
                <MATNR xmlns:xalan="http://xml.apache.org/xalan">222222</MATNR>
            </Row>
        <Row>
            <DCSInventory xmlns:xalan="http://xml.apache.org/xalan">0193948</DCSInventory>
            <DCSInventoryUOM xmlns:xalan="http://xml.apache.org/xalan">GA</DCSInventoryUOM>
            <DCSTank xmlns:xalan="http://xml.apache.org/xalan">T101</DCSTank>
        </Row>
    </Rowset>
</Rowsets>

What am I doing wrong? I'm just not able to get to the point wherein I can remove extra <Columns> and <Row> for the last <Rowset>

Please help !

Upvotes: 0

Views: 167

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117102

The question is somewhat ambiguous; the example would be much more useful if the rows weren't identical. AFAICT, the following stylesheet will return the expected result:

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="/Rowsets">
    <xsl:variable name="dcs" select="Rowset[@Name='DCS']"/>
    <xsl:copy>
        <xsl:for-each select="Rowset[@Name='TankList']">
            <Rowset>
                <Columns>
                    <xsl:copy-of select="Columns/Column"/>
                    <xsl:copy-of select="$dcs/Columns/Column"/>
                </Columns>
                <xsl:for-each select="Row">
                    <xsl:copy>
                        <xsl:copy-of select="*"/>
                        <xsl:copy-of select="$dcs/Row/*"/>
                    </xsl:copy>
                </xsl:for-each>
            </Rowset>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions