0
我有以下的XML和要複製<itemInformation>
元素低於<orderLineItem>
元素:複製子節點與匹配位置號碼
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<order>
<orderLineItem>1
<requestedQuantity>
<value>2</value>
</requestedQuantity>
<tradeItemIdentification>
<gtin>07610400013192</gtin>
</tradeItemIdentification>
</orderLineItem>
<orderLineItem>2
<requestedQuantity>
<value>1</value>
</requestedQuantity>
<tradeItemIdentification>
<gtin>07610400014632</gtin>
</tradeItemIdentification>
</orderLineItem>
<extension>
<orderExtension>
<itemInformation>1
<tradeItemIdentification>
<gtin>07610400013192</gtin>
</tradeItemIdentification>
<grossCost>
<currencyCode>
<currencyISOCode>ZAR</currencyISOCode>
</currencyCode>
<monetaryAmount>243.60</monetaryAmount>
</grossCost>
</itemInformation>
<itemInformation>2
<tradeItemIdentification>
<gtin>07610400014632</gtin>
</tradeItemIdentification>
<grossCost>
<currencyCode>
<currencyISOCode>ZAR</currencyISOCode>
</currencyCode>
<monetaryAmount>243.60</monetaryAmount>
</grossCost>
</itemInformation>
</orderExtension>
</extension>
</order>
</Envelope>
我想出了以下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="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="order/orderLineItem">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xxxx>
<xsl:apply-templates select="parent::order/extension/orderExtension/itemInformation
[normalize-space(text())=normalize-space(ancestor::order/orderLineItem/text())]"/>
</xxxx>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我預計這一目標XML:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<order>
<orderLineItem>
<xxxx>
<itemInformation>1
<tradeItemIdentification>
<gtin>07610400013192</gtin>
</tradeItemIdentification>
<grossCost>
<currencyCode>
<currencyISOCode>ZAR</currencyISOCode>
</currencyCode>
<monetaryAmount>243.60</monetaryAmount>
</grossCost>
</itemInformation>
</xxxx>1
<requestedQuantity>
<value>2</value>
</requestedQuantity>
<tradeItemIdentification>
<gtin>07610400013192</gtin>
</tradeItemIdentification>
</orderLineItem>
<orderLineItem>
<xxxx>
<itemInformation>2
<tradeItemIdentification>
<gtin>07610400014632</gtin>
</tradeItemIdentification>
<grossCost>
<currencyCode>
<currencyISOCode>ZAR</currencyISOCode>
</currencyCode>
<monetaryAmount>243.60</monetaryAmount>
</grossCost>
</itemInformation>
</xxxx>2
<requestedQuantity>
<value>1</value>
</requestedQuantity>
<tradeItemIdentification>
<gtin>07610400014632</gtin>
</tradeItemIdentification>
</orderLineItem>
<extension>
<orderExtension>
<itemInformation>1
<tradeItemIdentification>
<gtin>07610400013192</gtin>
</tradeItemIdentification>
<grossCost>
<currencyCode>
<currencyISOCode>ZAR</currencyISOCode>
</currencyCode>
<monetaryAmount>243.60</monetaryAmount>
</grossCost>
</itemInformation>
<itemInformation>2
<tradeItemIdentification>
<gtin>07610400014632</gtin>
</tradeItemIdentification>
<grossCost>
<currencyCode>
<currencyISOCode>ZAR</currencyISOCode>
</currencyCode>
<monetaryAmount>243.60</monetaryAmount>
</grossCost>
</itemInformation>
</orderExtension>
</extension>
</order>
</Envelope>
但在第二<orderLineItem>
元素我得到的第一個<itemInformation>
而不是第二個。 所以我必須匹配<itemInformation>
1,2,...與<orderLineItem>
1,2,...我只是無法弄清楚我需要使用的XPATH。
卡羅您好,非常感謝您 - 它完美的作品! (+1)我現在看到它,謝謝你的解釋。我的謂詞的第二部分是錯誤的(我很接近但看不到錯誤)。最好的問候,彼得 – Peter