通过XSLT合并<行集>

时间:2019-04-17 05:31:09

标签: xml xslt xslt-1.0 xslt-2.0

我的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>的{​​{1}}合并到其他<Rowset Name="DCS">

我得到的XML应该如下所示:

<Rowset Name="TankList">

我使用的XSLT如下:

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

但是结果XML不会合并<?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> <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>

请帮助!

1 个答案:

答案 0 :(得分: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>