如果状态代码有四个字符,然后将其转换为2个字符代码,如何检查状态代码?

时间:2015-05-11 16:39:37

标签: xml xslt

我要做的是检查XML文档中的元素

<DriversLicenseState Word="CDON">Ontario</DriversLicenseState>

如果找到的状态代码是四个字符并以“CD”开头,则只显示最后两个字符。在这种情况下(例子)我会显示<nc:JurisdictionCanadianProvinceCode>ON</nc:JurisdictionCanadianProvinceCode>

我不确定如何添加逻辑来执行此操作。

XML文档

<Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper" PackageID="IXML Case Notification Test" MessageID="67077793" xmlns="">
    <Party ID="8265760" InternalPartyID="392728694">
        <PartyName ID="4614549" Current="true" InternalNameID="1612416995">
            <NameType>Standard</NameType>
            <NameFirst>Ismael</NameFirst>
            <NameLast>Montemayor-Lira</NameLast>
            <FormattedName>Montemayor-Lira, Ismael</FormattedName>
        </PartyName>
        <DriversLicense Current="true">
            <DriversLicenseNumber>321456782541A</DriversLicenseNumber>
            <DriversLicenseState Word="CDON">Ontario</DriversLicenseState>
        </DriversLicense>
     </Party>
</Integration>

XSLT

<xsl:template name="ChargeDetails">
  <nc:IdentificationJurisdiction>
    <nc:JurisdictionCanadianProvinceCode>
        <xsl:for-each select="/Integration/Party/DriversLicense[@current='true']/DriversLicenseState">
            <xsl:if test="/Integration/Party/DriversLicense[@current='true']/DriversLicenseState[@Word]">
            <xsl:value-of select="substring(/Integration/Party/DriversLicense[@Current='true']/DriversLicenseState/@Word, 1, 2)"/>
            </xsl:if>
        </xsl:for-each>
    </nc:JurisdictionCanadianProvinceCode>
  </nc:IdentificationJurisdiction>
</xsl:template>

1 个答案:

答案 0 :(得分:0)

我猜你的代码使用了错误的XPATH表达式。此外,属性@current在您的XML中写为Current

xsl:for-each中提供XPATH表达式即可,以下代码段应显示所需的结果:

<xsl:template name="ChargeDetails">
  <nc:IdentificationJurisdiction>
     <nc:JurisdictionCanadianProvinceCode>
        <xsl:for-each select="/Integration/Party/DriversLicense[@Current='true']">
           <xsl:if test="DriversLicenseState[string-length(@Word) = 4 and substring(@Word,1,2) = 'CD']">
              <xsl:value-of select="substring(DriversLicenseState/@Word, 3, 2)"/>
           </xsl:if>
        </xsl:for-each>
     </nc:JurisdictionCanadianProvinceCode>
  </nc:IdentificationJurisdiction>
</xsl:template>

在目标元素中调用xsl:for-each听起来有点奇怪。也许你想要交换它们。