使用XSL将XML转换为另一个xml时遇到问题

时间:2019-01-18 19:56:39

标签: xml xslt

我需要将%*c标记添加到我现有的XML模式中。我正在尝试通过XSL转换来实现此结果,但未获得所需的输出。我的架构示例和XSL在下面提到。

XML:1

<?xml version="1.0" ?>

使用XSL

XSL示例:

<decreaseStickerVO>
    <taxOfficeCod>04.20</taxOfficeCod>
 <vehicleDetailsVOList>
    <VehicleDetailsVO>
 <chassis>878JHJH</chassis>
<circulationTax>6750</circulationTax>
<cylinderCapacity>De 1.801 c.c. a 2.400 c.c.</cylinderCapacity>
<registration>LD-11-11-KK</registration>
<registrationDate>2019-01-01</registrationDate>
<sealNum>K</sealNum>
<vehicleBrand>K</vehicleBrand>
<vehicleExemption>false</vehicleExemption>
<vehicleModel>K</vehicleModel>
<vehicleType>Ligeiros</vehicleType>
    </VehicleDetailsVO>
    <VehicleDetailsVO>
<chassis>878JHJHJ</chassis>
<circulationTax>2450</circulationTax>
<cylinderCapacity>De 126 c.c. a 450 c.c.</cylinderCapacity>
<registration>LDA-44-44</registration>
<registrationDate>2019-01-01</registrationDate>
<sealNum>K</sealNum>
<vehicleBrand>K</vehicleBrand>
<vehicleExemption>false</vehicleExemption>
<vehicleModel>K</vehicleModel>
<vehicleType>Motociclos</vehicleType>
    </VehicleDetailsVO>
     </vehicleDetailsVOList>
 </decreaseStickerVO>

到下面的XML

XML:2

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/decreaseStickerVO">
<decreaseStickerVO>
<taxOfficeCod>
  <xsl:value-of select="taxOfficeCod"/>
  </taxOfficeCod>
    <vehicleDetailsVOList>
      <xsl:for-each select="VehicleDetailsVO">
        <VehicleDetailsVO>
          <chassis>
            <xsl:value-of select="chassis"/>
          </chassis>
          <circulationTax>
            <xsl:value-of select="circulationTax"/>
          </circulationTax>
          <cylinderCapacity>
            <xsl:value-of select="cylinderCapacity"/>
          </cylinderCapacity>
          <registration>
            <xsl:value-of select="registration"/>
          </registration>
          <registrationDate>
            <xsl:value-of select="registrationDate"/>
          </registrationDate>
          <sealNum>
            <xsl:value-of select="sealNum"/>
          </sealNum>
          <vehicleBrand>
            <xsl:value-of select="vehicleBrand"/>
          </vehicleBrand>
          <vehicleExemption>
            <xsl:value-of select="vehicleExemption"/>
          </vehicleExemption>
          <vehicleModel>
            <xsl:value-of select="vehicleModel"/>
          </vehicleModel>
          <vehicleType>
            <xsl:value-of select="vehicleType"/>
          </vehicleType>
        </VehicleDetailsVO>
      </xsl:for-each>
    </vehicleDetailsVOList>
</decreaseStickerVO>
  </xsl:template>
</xsl:stylesheet>

但是得到结果

<?xml version="1.0" ?>
<decreaseStickerVO>
 <taxOfficeCod>04.20</taxOfficeCod>
<vehicleDetailsVOList>
<VehicleDetailsVO>
<chassis>878JHJH</chassis>
<circulationTax>6750</circulationTax>
<cylinderCapacity>De 1.801 c.c. a 2.400 c.c.</cylinderCapacity>
<registration>LD-11-11-KK</registration>
<registrationDate>2019-01-01</registrationDate>
<sealNum>K</sealNum>
<vehicleBrand>K</vehicleBrand>
<vehicleExemption>false</vehicleExemption>
<vehicleModel>K</vehicleModel>
<vehicleType>Ligeiros</vehicleType>
</VehicleDetailsVO>
<VehicleDetailsVO>
<chassis>878JHJHJ</chassis>
<circulationTax>2450</circulationTax>
<cylinderCapacity>De 126 c.c. a 450 c.c.</cylinderCapacity>
<registration>LDA-44-44</registration>
<registrationDate>2019-01-01</registrationDate>
<sealNum>K</sealNum>
<vehicleBrand>K</vehicleBrand>
<vehicleExemption>false</vehicleExemption>
<vehicleModel>K</vehicleModel>
<vehicleType>Motociclos</vehicleType>
</VehicleDetailsVO>
</vehicleDetailsVOList>
</decreaseStickerVO>

请提出更改我的XSL模板的建议,以获取所需的结果。

1 个答案:

答案 0 :(得分:1)

大多数结果为空的原因是您这样做:

<xsl:for-each select="VehicleDetailsVO">

来自以下内容:

<xsl:template match="/decreaseStickerVO">

当前节点decreaseStickerVO没有VehicleDetailsVO个子节点,因此您的xsl:for-each指令不选择任何内容。如果您这样做,将会得到不同的结果

<xsl:for-each select="vehicleDetailsVOList/VehicleDetailsVO">

但是,如果-似乎要保留所有内容在现有XML中,那么您可以做简单的事情:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

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

</xsl:stylesheet>

或更短:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>