【发布时间】:2019-04-17 05:31:08
【问题描述】:
我的 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>
我的要求是将<Rowset Name="DCS">的<Row>合并到其他<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