首页 > 解决方案 > 使用 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 属性级别中的重复值。

标签: xmlxsltattributesxslt-1.0

解决方案


您可以做的是将模板应用到子级GrossWeightTransportationQuantityInsuranceDeclaredAmount除外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>

推荐阅读