使用xslt和xsd从字符串获取枚举值

时间:2018-10-04 17:31:40

标签: xslt-1.0 xsd-validation

我正在尝试获取相应字符串的枚举值。我正在将一种XML转换为另一种。

例如,源元素是

<VehicleBodyType>Van</VehicleBodyType>

我需要将其转换为

<VehicleTypeCode>5</VehicleTypeCode>

这在XSD中:

<xs:simpleType name="VehicleBodyType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="NotProvided" />
        <xs:enumeration value="NotApplicable" />
        <xs:enumeration value="PassengerCar" />
        <xs:enumeration value="TruckPickupOrPassengerTruck" />
        <xs:enumeration value="Van" />
        <xs:enumeration value="TruckSingleUnitTruck2Axles" />
        <xs:enumeration value="MotorHomeRecreationalVehicle" />
    </xs:restriction>
</xs:simpleType>

我尝试过:

<VehicleTypeCode>
    <xsl:template match="xs:simpleType[@name='VehicleBodyType']/xs:restriction">
     <xsl:for-each select="xs:enumeration">
         <xsl:value-of select="@value"/>
     </xsl:for-each>
   </xsl:template>
</VehicleTypeCode>

但会收到一条错误消息,指示我无法将模板作为'VehicleTypeCode'元素的子元素。

我更喜欢使用XSD,但是如果这样做更容易,我也可以将其放入XSLT。我什至不知道for-each是否有效,但是我在here上找到了它,看起来像我想要的一样。

以下是源xml的示例:

<?xml version="1.0" encoding="UTF-8"?>
<Crash>
    <Vehicles>
        <Vehicle>
            <VehicleBodyType>Van</VehicleBodyType>
            <VehicleColor>Red</VehicleColor>
        </Vehicle>
    </Vehicles>
</Crash>

这里我正在使用精简的xslt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" exclude-result-prefixes="xsl xs fn xsi">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/Crash" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <CrashEntity>
            <WorkZoneWorkers>
                    <xsl:value-of select="WorkersPresentCde"/>
            </WorkZoneWorkers>
            <xsl:choose>
                <xsl:when test="not(Vehicles/Vehicle)">
                    <CrashUnits/>
                </xsl:when>
                <xsl:otherwise>
                    <CrashUnits>
                        <xsl:apply-templates select="Vehicles/Vehicle" />
                    </CrashUnits>
                </xsl:otherwise>
            </xsl:choose>
        </CrashEntity>
    </xsl:template>

    <!-- Vehicles/Vehicle -->
    <xsl:template match="Vehicles/Vehicle">
        <CrashUnitsEntity>
            <VehicleTypeCode>
                <xsl:value-of select="VehicleBodyType"/>
            </VehicleTypeCode>

            <!--Can't use a template within a template-->
            <!--<xsl:template match="xs:simpleType[@name='VehicleBodyType']/xs:restriction">
                    <VehicleTypeCode>
                        <xsl:value-of select="count(xs:enumeration[@value = 'Van']/preceding-sibling::xs:enumeration) + 1"/>
                    </VehicleTypeCode>
                </xsl:template>-->

            <!--Can't use a template within a template (original attempt)-->
            <!--<VehicleTypeCode>
                    <xsl:template match="xs:simpleType[@name=$data]/xs:restriction">
                      <xsl:for-each select="xs:enumeration">
                          <xsl:value-of select="@value"/>
                      </xsl:for-each>
                    </xsl:template>
                </VehicleTypeCode>-->

            <!--Try to call a function-->
            <VehicleTypeCode function="Enum1">
                <xsl:call-template name="getEnum1">
                    <xsl:with-param name="type" select="VehicleBodyType"/>
                    <xsl:with-param name="data" select="VehicleBodyType"/>
                </xsl:call-template>
            </VehicleTypeCode>

            <!--Try to call a function-->
            <VehicleTypeCode function="Enum2">
                <xsl:call-template name="getEnum2">
                    <xsl:with-param name="type" select="VehicleBodyType"/>
                    <xsl:with-param name="data" select="VehicleBodyType"/>
                </xsl:call-template>
            </VehicleTypeCode>

            <Vehicle>
                <UnitNumber>
                    <xsl:value-of select="@VehicleNumber"/>
                </UnitNumber>
            </Vehicle>
        </CrashUnitsEntity>
    </xsl:template>

    <!-- ##################################### functions ##################################### -->

    <!-- Can't call a template inside another template-->
    <xsl:template name="getEnum1">
    <xsl:param name="type"/>
    <xsl:param name="data"/>
    <xsl:if test="$data">
      <!--<xsl:template match="xs:simpleType[@name=$type]/xs:restriction">
        <xsl:for-each select="xs:enumeration">
            <xsl:value-of select="@value"/>
        </xsl:for-each>
      </xsl:template>-->
    </xsl:if>
  </xsl:template>

    <!-- Can't call a template inside another template-->
    <xsl:template name="getEnum2">
    <xsl:param name="type"/>
    <xsl:param name="data"/>
    <xsl:if test="$data">
        <!--<xsl:template match="xs:simpleType[@name=$type]/xs:restriction">
            <xsl:value-of select="count(xs:enumeration[@value = $data]/preceding-sibling::xs:enumeration) + 1"/>
        </xsl:template>-->
    </xsl:if>
  </xsl:template>

    <xs:simpleType name="VehicleBodyType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="NotProvided" />
            <xs:enumeration value="NotApplicable" />
            <xs:enumeration value="PassengerCar" />
            <xs:enumeration value="Van" />
            <xs:enumeration value="Truck" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="VehicleColor">
        <xs:restriction base="xs:string">
            <xs:enumeration value="NotProvided" />
            <xs:enumeration value="Red" />
            <xs:enumeration value="Green" />
            <xs:enumeration value="Blue" />
        </xs:restriction>
    </xs:simpleType>

</xsl:stylesheet>

预期的输出xml:

<?xml version="1.0" encoding="UTF-8"?>
<CrashEntity>
  <WorkZoneWorkers/>
  <CrashUnits>
    <CrashUnitsEntity>
      <VehicleTypeCode>4</VehicleTypeCode>
      <VehicleColor>2</VehicleColor>
    </CrashUnitsEntity>
  </CrashUnits>
</CrashEntity>

0 个答案:

没有答案