我有一个xml文档,现在我想将它翻译成具有相同内容但不同元素顺序的另一个xml文档。重新排列包含子节点的xml节点by xslt
原始的XML文档,如:
<?xml version = "1.0" encoding = "UTF-8"?>
<order xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" >
<ship>
<zipcode>78712</zipcode>
<street>1234 Main Street</street>
<country>CN</country>
<city>Beijing</city>
</ship>
<items>
<quantity>1</quantity>
<itemno>1234</itemno>
</items>
<items>
<quantity>3</quantity>
<itemno>1235</itemno>
</items>
<price>456</price>
<customer>Tom Hill</customer>
</order>
预期输出XML文档,如:
<?xml version = "1.0" encoding = "UTF-8"?>
<order xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" >
<customer>Tom Hill</customer>
<ship>
<street>1234 Main Street</street>
<city>Beijing</city>
<zipcode>78712</zipcode>
<country>CN</country>
</ship>
<items>
<itemno>1234</itemno>
<quantity>1</quantity>
</items>
<items>
<itemno>1235</itemno>
<quantity>3</quantity>
</items>
<price>456</price>
</order>
我用下面的XSLT文档进行翻译。
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/order">
<xsl:copy>
<xsl:copy-of select="customer" />
<xsl:copy-of select="ship" >
<xsl:call-template name="TShip" />
</xsl:copy-of>
<xsl:copy-of select="items">
<xsl:call-template name="TItems" />
</xsl:copy-of>
<xsl:copy-of select="price" />
</xsl:copy>
</xsl:template>
<xsl:template name="TShip">
<xsl:copy>
<xsl:copy-of select="street" />
<xsl:copy-of select="city" />
<xsl:copy-of select="zipcode" />
<xsl:copy-of select="country" />
</xsl:copy>
</xsl:template>
<xsl:template name="TItems">
<xsl:for-each select="items">
<xsl:copy>
<xsl:copy-of select="itemno" />
<xsl:copy-of select="quantity" />
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但是,翻译结果不是我的预期。 翻译结果XML:
<?xml version = "1.0" encoding = "UTF-8"?>
<order xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" >
<customer>Tom Hill</customer>
<ship>
<zipcode>78712</zipcode>
<street>1234 Main Street</street>
<country>CN</country>
<city>Beijing</city>
</ship>
<items>
<quantity>1</quantity>
<itemno>1234</itemno>
</items>
<items>
<quantity>3</quantity>
<itemno>1235</itemno>
</items>
<price>456</price>
</order>
它只是取得了预期的顺序中的第一级节点。所有的子节点都保持原来的顺序。我如何按照我的预期制作所有节点的顺序?
这是完美的,谢谢你的指导。 – zgcharley