I'm trying to swap two of the values in my XML below, In my below xml ShipFromLocationRef has value RO91 and ShipToLocationRef has value 6449706. My requirement is to swap R091 to ShipToLocationRef and 6449706 to ShipFromLocationRef.
Requesting you to please help here Below is my XML what i want to get transformed
<?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 is my XSL code:
<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>
My expected output is:
<?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>
AFAICT, this returns the expected result:
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>