<?xml version="1.0" encoding="UTF-8"?>
<root>
<order orderId="12345">
<cartId>12346</cartId>
</order>
<orderPayment paymentId="1234">
<debitCardPayment>
<chargeAmount currencyCode="USD">22.20</chargeAmount>
<debitCard>
<PIN>1234</PIN>
</debitCard>
</debitCardPayment>
</orderPayment>
</root>
我输入xml如上所述。我需要在顺序中移动 orderPayment 元素。我写如下
<xsl:template match="v1:order">
<xsl:apply-templates select="v1:Root/v1:orderPayment"/>
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="v1:orderPayment">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:choose>
<xsl:when test=".='' and count(@*)=0">
<xsl:apply-templates select="@*|node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<order>
.....
<orderPayment>
.....
</orderPayment>
</order>
我没有得到预期的输出。这是什么错误?
提前致谢...
答案 0 :(得分:2)
根据您的输入,以下样式表:
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="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="order">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:copy-of select="../orderPayment"/>
</xsl:copy>
</xsl:template>
<xsl:template match="orderPayment"/>
</xsl:stylesheet>
将返回:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<order orderId="12345">
<cartId>12346</cartId>
<orderPayment paymentId="1234">
<debitCardPayment>
<chargeAmount currencyCode="USD">22.20</chargeAmount>
<debitCard>
<PIN>1234</PIN>
</debitCard>
</debitCardPayment>
</orderPayment>
</order>
</root>