我有一個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