合并 通过XSLT

问题描述 投票:0回答:1

我的XML如下:(有两个<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>

我的要求是将<Row><Rowset Name="DCS">合并到其他<Rowset Name="TankList">

我的结果XML应如下所示:

<?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>

我使用的XSLT如下:

<?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>

但结果XML不合并<Row><Columns>而是将<Row><Columns>复制为额外节点。

<?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>

我究竟做错了什么?我只是无法达到这样的程度,我可以删除额外的<Columns><Row>为最后<Rowset>

请帮忙 !

xml xslt xslt-1.0 xslt-2.0
1个回答
1
投票

这个问题有些含糊不清;如果行不相同,该示例将更有用。 AFAICT,以下样式表将返回预期结果:

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>
© www.soinside.com 2019 - 2024. All rights reserved.