xml - 使用 XSLT 代码处理 xml 中的重复属性及其值
问题描述
我有一个重复 ITEM 段的 xml,它的属性需要使用 XSLT 1.0 处理
<TransportationRequestQuotation>
<Item>
<GrossWeightTransportationQuantity>
<unitCode>KG</unitCode>
<text>60</text>
</GrossWeightTransportationQuantity>
<InsuranceDeclaredAmount>
<currencyCode>SAR</currencyCode>
<text>5000</text>
</InsuranceDeclaredAmount>
</Item>
<Item>
<GrossWeightTransportationQuantity>
<unitCode>KG</unitCode>
<text>80</text>
</GrossWeightTransportationQuantity>
<InsuranceDeclaredAmount>
<currencyCode>SAR</currencyCode>
<text>5000</text>
</InsuranceDeclaredAmount>
</Item>
</TransportationRequestQuotation>
使用 XSLT 代码的预期 xml 如下:
<TransportationRequestQuotation>
<Item>
<GrossWeightTransportationQuantity unitCode="KG">60</GrossWeightTransportationQuantity>
<InsuranceDeclaredAmount currencyCode="SAR">5000</InsuranceDeclaredAmount>
</Item>
<Item>
<GrossWeightTransportationQuantity unitCode="KG">80</GrossWeightTransportationQuantity>
<InsuranceDeclaredAmount currencyCode="SAR">5000</InsuranceDeclaredAmount>
</Item>
</TransportationRequestQuotation>
我使用了以下代码,但无法获得所需的输出:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://sap.com/xi/SAPGlobal20/Global">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GrossWeightTransportationQuantity">
<xsl:element name="GrossWeightTransportationQuantity">
<xsl:for-each select="*">
<xsl:attribute name="{name()}" >
<xsl:value-of select="text()" />
</xsl:attribute>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="InsuranceDeclaredAmount">
<xsl:element name="InsuranceDeclaredAmount">
<xsl:for-each select="*">
<xsl:attribute name="{name()}" >
<xsl:value-of select="text()" />
</xsl:attribute>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
请求帮助我处理 ITEM 属性级别中的重复值。
解决方案
您可以做的是将模板应用到子级GrossWeightTransportationQuantity
和InsuranceDeclaredAmount
除外text
并将其更改为属性。然后只输出 的值text
。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GrossWeightTransportationQuantity|InsuranceDeclaredAmount">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::text)]"/>
<xsl:value-of select="text"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GrossWeightTransportationQuantity/*|InsuranceDeclaredAmount/*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- html - 从 hta 按钮启动 plink-ssh 连接并将值传递给 ssh 命令
- android - 我应该在 onResume() 调用中从数据库中检索记录吗?
- jenkins-pipeline - Jenkins Scripted Pipeline: sshCommand 执行 statusCode
- php - 无法找到我的 iOS 设备 ID 并连接到 APNS 服务器以在 PHP 中进行推送通知
- swift - 如何在 swift (FCM) 中接收数据通知
- javascript - 如何修复“http://localhost:3000”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。
- git - 如何在 Colab 上克隆共享的私有 git 存储库?
- compression - 结构化消息压缩
- java - 为什么扫描仪不读取我调用程序的第一行?
- android - 错误:E2564 找不到程序“C:\bin\JarSigner.exe”