我的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> of <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>
请帮帮忙!
发布于 2019-04-18 03:17:43
这个问题有点模棱两可;如果行不是完全相同的,那么这个例子就更有用了。AFAICT,以下样式表将返回预期结果:
XSLT1.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>https://stackoverflow.com/questions/55720502
复制相似问题