如何根据xml中的状态代码显示正确的地址类型?

如何根据xml中的状态代码显示正确的地址类型?,xml,xslt,Xml,Xslt,我希望根据xml文档中的案例Party/State或Party/State将Party address显示为非标准或标准或外部地址。 在我的xsl中,我已经检查了引用xsd文档中的状态。 如果在两份参考文件中未找到该州,我想将该党的地址显示为外国地址。 我的代码没有将外部地址显示为外部地址,该地址的格式与我在所需输出中显示的格式不同 期望输出 <nc:Address> <nc:AddressFullText>123 Mexico RD Cancun, MM

我希望根据xml文档中的案例Party/State或Party/State将Party address显示为非标准或标准或外部地址。 在我的xsl中,我已经检查了引用xsd文档中的状态。 如果在两份参考文件中未找到该州,我想将该党的地址显示为外国地址。 我的代码没有将外部地址显示为外部地址,该地址的格式与我在所需输出中显示的格式不同

期望输出

<nc:Address>
        <nc:AddressFullText>123 Mexico RD Cancun, MM, 12345 Mexico</nc:AddressFullText>
    </nc:Address>

墨西哥坎昆墨西哥路123号,邮编:12345
错误输出

<nc:Address>
        <nc:LocationStreet>
            <nc:StreetNumberText>123</nc:StreetNumberText>
            <nc:StreetPredirectionalText/>
            <nc:StreetName>Mexico</nc:StreetName>
            <nc:StreetCategoryText>Road</nc:StreetCategoryText>
            <nc:StreetPostdirectionalText/>
            <nc:StreetExtensionText/>
        </nc:LocationStreet>
        <nc:LocationCityName>Cancun</nc:LocationCityName>
        <nc:LocationStateUSPostalServiceCode>MM</nc:LocationStateUSPostalServiceCode>
        <nc:LocationPostalCode>12345</nc:LocationPostalCode>
    </nc:Address>

123
墨西哥
道路
坎昆
嗯
12345
我的xml文档

<Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper" PackageID="DL Notice to DVS" MessageID="67084884" xmlns="">

<Party ID="16770378" InternalPartyID="1614673416">
    <NotifyElectronically>0</NotifyElectronically>
    <PartyInJailFlag>false</PartyInJailFlag>
    <DateOfBirth Current="true">05/31/1960</DateOfBirth>
    <Address PartyCorrespondence="true" PartyCurrent="true" ID="17875835" Type="Standard">
        <AddressLine2>123 Mexico RD</AddressLine2>
        <AddressLine4>Cancun, MM, 12345</AddressLine4>
        <Block>123</Block>
        <Street>Mexico</Street>
        <AddrSfxKy Word="RD">Road</AddrSfxKy>
        <City>Cancun</City>
        <State>MM</State>
        <Zip>12345</Zip>
        <Foreign>false</Foreign>
        <TimestampCreate>5/28/2015 10:31:50 AM</TimestampCreate>
    </Address>
</Party>
</Integration>

0
假的
05/31/1960
墨西哥路123号
坎昆,MM,12345
123
墨西哥
道路
坎昆
嗯
12345
假的
2015年5月28日上午10:31:50
参考usps_states.xsd文件的示例

<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:structures="http://release.niem.gov/niem/structures/3.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:appinfo="http://release.niem.gov/niem/appinfo/3.0/" xmlns:usps-3.0.1="http://publication.niem.gov/niem/codes/usps_states/3.0/1/" xmlns:ct="http://release.niem.gov/niem/conformanceTargets/3.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="http://publication.niem.gov/niem/codes/usps_states/3.0/1/" version="1" xsi:schemaLocation="http://release.niem.gov/niem/appinfo/3.0/ ../../../../appinfo/3.0/appinfo.xsd http://release.niem.gov/niem/conformanceTargets/3.0/ ../../../../conformanceTargets/3.0/conformanceTargets.xsd" ct:conformanceTargets="http://reference.niem.gov/niem/specification/naming-and-design-rules/3.0/#ReferenceSchemaDocument">
    <xs:annotation>
        <xs:documentation>U.S. Postal Service Supplement</xs:documentation>
    </xs:annotation>
    <xs:import namespace="http://release.niem.gov/niem/structures/3.0/" schemaLocation="../../../../structures/3.0/structures.xsd"/>
    <xs:simpleType name="USStateCodeSimpleType">
        <xs:annotation>
            <xs:documentation>A data type for states.</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:token">
            <xs:enumeration value="AK">
                <xs:annotation>
                    <xs:documentation>ALASKA</xs:documentation>
                </xs:annotation>
            </xs:enumeration>
            <xs:enumeration value="AR">
                <xs:annotation>
                    <xs:documentation>ARKANSAS</xs:documentation>
                </xs:annotation>
            </xs:enumeration>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

美国邮政服务补充
状态的数据类型。
阿拉斯加州
阿肯色州
参考post-canada.xsd文档的示例

<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:structures="http://release.niem.gov/niem/structures/3.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:appinfo="http://release.niem.gov/niem/appinfo/3.0/" xmlns:can="http://release.niem.gov/niem/codes/canada_post/3.0/" xmlns:ct="http://release.niem.gov/niem/conformanceTargets/3.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="http://release.niem.gov/niem/codes/canada_post/3.0/" version="1" xsi:schemaLocation="http://release.niem.gov/niem/appinfo/3.0/ ../../../appinfo/3.0/appinfo.xsd http://release.niem.gov/niem/conformanceTargets/3.0/ ../../../conformanceTargets/3.0/conformanceTargets.xsd" ct:conformanceTargets="http://reference.niem.gov/niem/specification/naming-and-design-rules/3.0/#ReferenceSchemaDocument">
    <xs:import namespace="http://release.niem.gov/niem/structures/3.0/" schemaLocation="../../../structures/3.0/structures.xsd"/>
    <xs:simpleType name="CanadianProvinceCodeSimpleType">
        <xs:restriction base="xs:token">
            <xs:enumeration value="BC">
                <xs:annotation>
                    <xs:documentation>British Columbia</xs:documentation>
                </xs:annotation>
            </xs:enumeration>
            <xs:enumeration value="MB">
                <xs:annotation>
                    <xs:documentation>Manitoba</xs:documentation>
                </xs:annotation>
            </xs:enumeration>
            <xs:enumeration value="NB">
                <xs:annotation>
                    <xs:documentation>New Brunswick</xs:documentation>
                </xs:annotation>
            </xs:enumeration>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

不列颠哥伦比亚省
马尼托巴省
新不伦瑞克省
我的xsl代码

    <xsl:for-each select="/Integration/Party[@ID=current()/@ID]/Address[@PartyCurrent='true']">
                    <xsl:call-template name="Address"/>
                </xsl:for-each>

<--Address Template-->

        <xsl:template name="Address">
        <xsl:variable name="vUsState">
            <xsl:value-of select="document(concat($gEnvPath,'\Schemas\NiemExchanges\DvsDriverLicenseNotification\niem\codes\usps_states\3.0\1\usps_states.xsd'))/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=current()/State]/@value"/>
        </xsl:variable>
        <xsl:variable name="vCanadianState">
            <xsl:value-of select="document(concat($gEnvPath,'\Schemas\NiemExchanges\DvsDriverLicenseNotification\niem\codes\canada_post\3.0\post-canada.xsd'))/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=current()/State]/@value"/>
        </xsl:variable>
        <nc:Address>
            <xsl:choose>
                <xsl:when test="Block and string-length($vUsState or $vCanadianState)>0">
    <!--Standard-->
                    <nc:LocationStreet>
                        <nc:StreetNumberText>
                            <xsl:value-of select="Block"/>
                        </nc:StreetNumberText>
                        <nc:StreetPredirectionalText>
                            <xsl:value-of select="PreDir"/>
                        </nc:StreetPredirectionalText>
                        <nc:StreetName>
                            <xsl:value-of select="Street"/>
                        </nc:StreetName>
                        <nc:StreetCategoryText>
                            <xsl:value-of select="AddrSfxKy"/>
                        </nc:StreetCategoryText>
                        <nc:StreetPostdirectionalText>
                            <xsl:value-of select="PostDir"/>
                        </nc:StreetPostdirectionalText>
                        <nc:StreetExtensionText>
                            <xsl:value-of select="normalize-space(concat(UnitKy, ' ' , UnitNum))"/>
                        </nc:StreetExtensionText>
                    </nc:LocationStreet>
                    <nc:LocationCityName>
                        <xsl:value-of select="City"/>
                    </nc:LocationCityName>
                    <nc:LocationStateUSPostalServiceCode>
                        <xsl:value-of select="State"/>
                    </nc:LocationStateUSPostalServiceCode>
                    <nc:LocationPostalCode>
                        <xsl:value-of select="Zip"/>
                    </nc:LocationPostalCode>
                </xsl:when>
                    <xsl:when test="Foreign ='false'">
    <!--Non-Standard-->
                        <nc:LocationStreet>
                            <nc:StreetFullText>
                                <xsl:value-of select="AddressLine1"/>
                            </nc:StreetFullText>
                            <nc:StreetFullText>
                                <xsl:value-of select="AddressLine2"/>
                            </nc:StreetFullText>
                            <nc:StreetFullText>
                                <xsl:value-of select="AddressLine3"/>
                            </nc:StreetFullText>
                        </nc:LocationStreet>
                        <nc:LocationCityName>
                            <xsl:value-of select="City"/>
                        </nc:LocationCityName>
                        <nc:LocationStateUSPostalServiceCode>
                            <xsl:value-of select="State"/>
                        </nc:LocationStateUSPostalServiceCode>
                        <nc:LocationPostalCode>
                            <xsl:value-of select="Zip"/>
                        </nc:LocationPostalCode>
                    </xsl:when>
                    <xsl:otherwise>
    <!--Foreign-->
                        <nc:AddressFullText>
                            <xsl:value-of select="concat(AddressLine1, '&#xa;')"/>
                            <xsl:value-of select="concat(AddressLine2, '&#xa;')"/>
                            <xsl:value-of select="concat(AddressLine3, '&#xa;')"/>
                            <xsl:value-of select="concat(AddressLine4, '&#xa;')"/>
                        </nc:AddressFullText>
                    </xsl:otherwise>
                </xsl:choose>
        </nc:Address>
    </xsl:template>

我认为您的主要问题是这一行

<xsl:when test="Block and string-length($vUsState or $vCanadianState)>0">
但是,您可能会发现这仍然没有像预期的那样起作用。本例中的问题是如何定义这两个变量

    <xsl:variable name="vUsState">
        <xsl:value-of select="document(concat($gEnvPath,'\Schemas\NiemExchanges\DvsDriverLicenseNotification\niem\codes\usps_states\3.0\1\usps_states.xsd'))/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=current()/State]/@value"/>
    </xsl:variable>
这意味着如果属性不存在,它将为空,因此表达式
$vUsState或$vCanadianState
如果两者都不包含状态,则将返回false

首先尝试一下这个(简略的)XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nc="nc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:template match="/">
    <xsl:for-each select="/Integration/Party/Address[@PartyCurrent='true']">
      <xsl:call-template name="Address"/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="Address">
    <xsl:variable name="vUsState" select="document('C:\inetpub\wwwroot\Test\usps_states.xsd')/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=current()/State]/@value"/>
    <xsl:variable name="vCanadianState" select="document('C:\inetpub\wwwroot\Test\post-canada.xsd')/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=current()/State]/@value"/>
    <nc:Address>
      <xsl:choose>
        <xsl:when test="Block and ($vUsState or $vCanadianState)">
          <Standard />
        </xsl:when>
        <xsl:when test="Foreign ='false'">
          <NonStandard />
        </xsl:when>
        <xsl:otherwise>
          <Foreign />
        </xsl:otherwise>
      </xsl:choose>
    </nc:Address>
  </xsl:template>
</xsl:stylesheet>


请注意,对于给定的输入XML,
Foreign
元素被设置为
false
,这意味着第二个
xsl:when
子句将应用,因此在这种情况下,您将获得第二个地址表单。

。我只是喜欢你详细解释为什么或者为什么我的代码不能工作的方式。你的答案总是最好的。您总是解释您建议的代码问题解决方案背后的原因。你不仅要当专家,还要当老师。再次感谢。
  <xsl:variable name="vUsState" select="document('C:\inetpub\wwwroot\Test\usps_states.xsd')/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=current()/State]/@value"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nc="nc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:template match="/">
    <xsl:for-each select="/Integration/Party/Address[@PartyCurrent='true']">
      <xsl:call-template name="Address"/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="Address">
    <xsl:variable name="vUsState" select="document('C:\inetpub\wwwroot\Test\usps_states.xsd')/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=current()/State]/@value"/>
    <xsl:variable name="vCanadianState" select="document('C:\inetpub\wwwroot\Test\post-canada.xsd')/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=current()/State]/@value"/>
    <nc:Address>
      <xsl:choose>
        <xsl:when test="Block and ($vUsState or $vCanadianState)">
          <Standard />
        </xsl:when>
        <xsl:when test="Foreign ='false'">
          <NonStandard />
        </xsl:when>
        <xsl:otherwise>
          <Foreign />
        </xsl:otherwise>
      </xsl:choose>
    </nc:Address>
  </xsl:template>
</xsl:stylesheet>