用于将一个元素转换为多个元素之间的属性的xslt

用于将一个元素转换为多个元素之间的属性的xslt,xslt,attributes,xslt-1.0,Xslt,Attributes,Xslt 1.0,我有一个包含子元素的xml,需要使用XSLT1.0将其中一个子元素更改为属性 <TransportationRequest> <actionCode>01</actionCode> <ContractConditionCode>DC</ContractConditionCode> <ShippingTypeCode>17</ShippingTypeCode> <MovementTypeCode>3&l

我有一个包含子元素的xml,需要使用XSLT1.0将其中一个子元素更改为属性

<TransportationRequest>
<actionCode>01</actionCode>
<ContractConditionCode>DC</ContractConditionCode>
<ShippingTypeCode>17</ShippingTypeCode>
<MovementTypeCode>3</MovementTypeCode>
<DangerousGoodsIndicator>false</DangerousGoodsIndicator>
<DefaultCurrencyCode>SAR</DefaultCurrencyCode>

01
直流
17
3.
假的
合成孔径雷达
使用XSLT代码,预期的xml如下所示:

<TransportationRequest actionCode="01">
  <ContractConditionCode>DC</ContractConditionCode>
  <ShippingTypeCode>17</ShippingTypeCode>
  <MovementTypeCode>3</MovementTypeCode>
  <DangerousGoodsIndicator>false</DangerousGoodsIndicator>
  <DefaultCurrencyCode>SAR</DefaultCurrencyCode>

直流
17
3.
假的
合成孔径雷达

首先,关闭xml上的根标记:

<TransportationRequest>
<actionCode>01</actionCode>
<ContractConditionCode>DC</ContractConditionCode>
<ShippingTypeCode>17</ShippingTypeCode>
<MovementTypeCode>3</MovementTypeCode>
<DangerousGoodsIndicator>false</DangerousGoodsIndicator>
<DefaultCurrencyCode>SAR</DefaultCurrencyCode>
</TransportationRequest>

01
直流
17
3.
假的
合成孔径雷达
然后,此xsl将执行您要求的操作:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="xml" encoding="UTF-8" />
    <xsl:template match="/">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|node()">
    <!-- Copy every node as is -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="TransportationRequest">
    <!-- transform the TransportationRequest node in a special way -->
        <xsl:element name="TransportationRequest">
            <xsl:attribute name="actionCode"><xsl:value-of select="actionCode" /></xsl:attribute>
        <!-- don't transform the actionCode node (is in the attribute) -->
            <xsl:apply-templates select="@*|node()[name()!='actionCode']"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet> 

您可以这样做:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="TransportationRequest">
    <xsl:copy>
      <xsl:attribute name="actionCode">
        <xsl:value-of select="actionCode"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="actionCode"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>


看到它在这里工作:

你的问题是什么?