使用 XSLT 1.0 复制具有附加逻辑的 XML



我想复制整个xml并应用以下附加逻辑: 如果/OrderMessage/IncomingOrderMessage/IncomingOrder/TransferDetails/TransferDetails_item(只需检查第一个(/quantity 标签存在 然后将/OrderMessage/IncomingOrderMessage/IncomingOrder/doNotTransfer 设置为 true。 否则,只需按原样复制/OrderMessage/IncomingOrderMessage/IncomingOrder/doNotTransfer。

输入.xml:

<?xml version="1.0" encoding="UTF-8"?>
<OrderMessage>
<IncomingOrderMessage>
<IncomingOrder>
<totalOrderQuantity>10000</totalOrderQuantity>
<totalOrderPrice>500000</totalOrderPrice>
<TransferDetails>       
<TransferDetails_item>
<quantity>5000</quantity>
<price>250000</price>
<location>India</location>
</TransferDetails_item>
<TransferDetails_item>
<quantity>5000</quantity>
<price>250000</price>
<location>US</location>
</TransferDetails_item>
</TransferDetails>
<orderCurrency>USD</orderCurrency>
<doNotTransfer>false</doNotTransfer>
<orderPayment>Credit</orderPayment>
</IncomingOrder>
</IncomingOrderMessage>
</OrderMessage>

输出.xml

<?xml version="1.0" encoding="UTF-8"?>
<OrderMessage>
<IncomingOrderMessage>
<IncomingOrder>
<totalOrderQuantity>10000</totalOrderQuantity>
<totalOrderPrice>500000</totalOrderPrice>
<TransferDetails>        
<TransferDetails_item>
<quantity>5000</quantity>
<price>250000</price>
<location>India</location>
</TransferDetails_item>
<TransferDetails_item>
<quantity>5000</quantity>
<price>250000</price>
<location>US</location>
</TransferDetails_item>
</TransferDetails>
<orderCurrency>USD</orderCurrency>
<doNotTransfer>true</doNotTransfer>
<orderPayment>Credit</orderPayment>
</IncomingOrder>
</IncomingOrderMessage>
</OrderMessage>

有人可以帮我吗?

或者简称:

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="doNotTransfer[../TransferDetails/TransferDetails_item/quantity]">
<xsl:copy>true</xsl:copy>
</xsl:template>
</xsl:stylesheet>

如果doNotTransfer的值始终为truefalse,则可以创建第二个模板:

<xsl:template match="doNotTransfer">
<xsl:copy>
<xsl:value-of select="boolean(../TransferDetails/TransferDetails_item/quantity)" />
</xsl:copy>
</xsl:template>

我能够使用以下 xslt 获得输出:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="IncomingOrder">
<xsl:choose>
<xsl:when test="//TransferDetails_item[1]/quantity ">
<xsl:copy>
<xsl:apply-templates select="@*|node() except doNotTransfer"/>
<doNotTransfer>true</doNotTransfer>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

相关内容

  • 没有找到相关文章

最新更新