使用 XSLT 代码处理 xml 中的重复属性及其值
Handling repeating Attributes and its value in xml using XSLT code
我有一个 xml 重复的 ITEM 段,它的属性需要使用 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>
预期 xml 使用 XSLT 代码如下:
<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>
我有一个 xml 重复的 ITEM 段,它的属性需要使用 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>
预期 xml 使用 XSLT 代码如下:
<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>