xml - 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>
解决方案
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>
推荐阅读
- pandas - Pandas 在某些列上合并;对于其他一些列,如果不是 NaN,请向左
- c++ - 为什么 msg.message 在我的程序中不等于 WM_QUIT
- python - 用于非图像矩阵的 Keras CNN
- javascript - Node.js - 获取 routes.js 的目录
- python - 给定字典和列表的频率计数
- python - 如何使用 pandas 根据季节对时间序列进行分组?
- python - 使用 IFTTT 将温度和湿度信息从 Pi 发送到手机时出现问题
- c++ - (C++) 使用 OR 运算符将值与数组的每个元素进行比较的更简单方法?
- apache-beam - 如何为数值类型指定 BeamSQL UDF
- java - 在 intellij IDEA 中设置 gstreamer 时出现“无法加载库:gstreamer”