首页 > 解决方案 > M 尝试将 shipfromlocationref 交换为 shiptolocationref 以获取以下 XML

问题描述

我正在尝试在下面的 XML 中交换两个值,在下面的 xml 中ShipFromLocationRef的值为 RO91,ShipToLocationRef的值为 6449706。我的要求是将 R091 交换为ShipToLocationRef,将 6449706 交换为ShipFromLocationRef

请求您在此处提供帮助以下是我想要转换的 XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Transmission xmlns="http://www.w3.org/1999/XSL/Transform">
<TransmissionHeader>
</TransmissionHeader>
<TransmissionBody>
<GLogXMLElement>
<Release>
<ReleaseGid>
<Gid>
<DomainName>M02</DomainName>
<Xid>4008060679_XD</Xid>
</Gid>
</ReleaseGid>
<TransactionCode>IU</TransactionCode>
<ShipFromLocationRef>
<LocationRef>
<LocationGid>
<Gid>
<DomainName>M02</DomainName>
<Xid>RO91</Xid>
</Gid>
</LocationGid>
</LocationRef>
</ShipFromLocationRef>
<ShipToLocationRef>
<LocationRef>
<LocationGid>
<Gid>
<DomainName>M02</DomainName>
<Xid>6449706</Xid>
</Gid>
</LocationGid>
</LocationRef>
</ShipToLocationRef>
</Release>
</GLogXMLElement>
</TransmissionBody>
</Transmission>

Belo 是我的 XSL 代码:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:Transmission="http://www.w3.org/1999/XSL/Transform">
                <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
                <xsl:template match="@*|node()">
                                <xsl:copy>
                                                <xsl:apply-templates select="@*|node()"/>
                                </xsl:copy>
                </xsl:template>
    <xsl:template match="Transmission:ShipFromLocationRef/LocationRef/LocationGid/Gid/Xid">
        <xsl:copy>
            <xsl:apply-templates select="@*|../Transmission:ShipToLocationRef/LocationRef/LocationGid/Gid/Xid/text()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Transmission:ShipToLocationRef/LocationRef/LocationGid/Gid/Xid">
        <xsl:copy>
            <xsl:apply-templates select="@*|../Transmission:ShipFromLocationRef/LocationRef/LocationGid/Gid/Xid/text()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

我的预期输出是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Transmission xmlns="http://www.w3.org/1999/XSL/Transform">
    <TransmissionHeader>
    </TransmissionHeader>
    <TransmissionBody>
    <GLogXMLElement>
    <Release>
    <ReleaseGid>
    <Gid>
    <DomainName>M02</DomainName>
    <Xid>4008060679_XD</Xid>
    </Gid>
    </ReleaseGid>
    <TransactionCode>IU</TransactionCode>
    <ShipFromLocationRef>
    <LocationRef>
    <LocationGid>
    <Gid>
    <DomainName>M02</DomainName>
    <Xid>**6449706**</Xid>
    </Gid>
    </LocationGid>
    </LocationRef>
    </ShipFromLocationRef>
    <ShipToLocationRef>
    <LocationRef>
    <LocationGid>
    <Gid>
    <DomainName>M02</DomainName>
    <Xid>**RO91**</Xid>
    </Gid>
    </LocationGid>
    </LocationRef>
    </ShipToLocationRef>
    </Release>
    </GLogXMLElement>
    </TransmissionBody>
    </Transmission>

标签: xmlxslttagsswap

解决方案


AFAICT,这将返回预期结果:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ns0:ShipFromLocationRef//ns0:Xid">
    <xsl:copy>
        <xsl:value-of select="//ns0:ShipToLocationRef//ns0:Xid"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ns0:ShipToLocationRef//ns0:Xid">
    <xsl:copy>
        <xsl:value-of select="//ns0:ShipFromLocationRef//ns0:Xid"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

推荐阅读