如何在CaseParty下显示地址?

时间:2015-06-05 15:50:48

标签: xml xslt

我认为发布一个新问题会更容易,而不是在上一篇文章中进行更多编辑。 我的xslt代码以正确的格式显示正确的地址,但当CaseParty下找不到地址时,我的代码不显示Party下的地址

我添加了地址模板xsl代码,因为在xml中找到的地址将根据地址模板格式化,即非美国标准,标准和外国。

我想显示CaseParty下的地址,但如果找不到地址,我想在Party下显示地址

我需要检查CaseParty xpath /Integration/Case/CaseParty/Connection[@Word="DFD"下是否有地址,然后<xsl:call-template name="Address"/>(调用地址模板进行格式化并显示)。

如果在CaseParty下找不到地址,我想查找Party xpath /Integration/Party/Address[@PartyCurrent="true"下的地址。如果在那里找到地址,则显示它。

示例xml文档,其中地址位于CaseParty下,地址位于Party

    <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="67084533" xmlns="">
<Case Op="E" InternalID="1617088326" ID="12120229" xmlns:user="http://tylertechnologies.com">
    <CaseParty ID="16731290" InternalCasePartyID="1634787102" InternalPartyID="1614631672">
        <Connection Word="DFD" BaseConnection="DF" ID="36370323" InternalCasePartyConnectionID="1636469444">
            <Description>Defendant</Description>
        </Connection>
        <Address CaseCorrespondence="true" ID="17875824" Type="Standard">
            <AddressLine2>3712 Testing RD</AddressLine2>
            <AddressLine4>St Paul, NY, 21457</AddressLine4>
            <Block>3712</Block>
            <Street>Testing</Street>
            <AddrSfxKy Word="RD">Road</AddrSfxKy>
            <City>St Paul</City>
            <State>NY</State>
            <Zip>21457</Zip>
            <Foreign>false</Foreign>
            <TimestampCreate>5/27/2015 10:34:08 AM</TimestampCreate>
        </Address>
        <TimestampCreate>1/29/2015 5:04:53 PM</TimestampCreate>
        <TimestampChange/>
    </CaseParty>
</Case>
<Party ID="16731290" InternalPartyID="1614631672">
    <Address PartyCorrespondence="true" PartyCurrent="true" ID="17867956" Type="Standard">
        <AddressLine2>1906 3RD AVE S #36</AddressLine2>
        <AddressLine4>Denver, CO, 55408</AddressLine4>
        <Block>1906</Block>
        <Street>3RD AVE S #36</Street>
        <City>Denver</City>
        <State>CO</State>
        <Zip>87459</Zip>
        <Foreign>false</Foreign>
    </Address>
</Party>
</Integration> 

所需地址输出

<nc:Address>
        <nc:AddressFullText>
            <nc:Address>
                <nc:LocationStreet>
                    <nc:StreetNumberText>3712</nc:StreetNumberText>
                    <nc:StreetPredirectionalText/>
                    <nc:StreetName>Testing</nc:StreetName>
                    <nc:StreetCategoryText>Road</nc:StreetCategoryText>
                    <nc:StreetPostdirectionalText/>
                    <nc:StreetExtensionText/>
                </nc:LocationStreet>
                <nc:LocationCityName>St Paul</nc:LocationCityName>
                <nc-3.0.1:LocationStateUSPostalServiceCode>NY</nc-3.0.1:LocationStateUSPostalServiceCode>
                <nc:LocationPostalCode>21457</nc:LocationPostalCode>
            </nc:Address>
    </nc:Address>

地址模板

    <!--Template Address-->
<xsl:template name="Address">
    <xsl:variable name="vUsState" 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 name="vCanadianState" 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"/>
    <nc:Address>
        <xsl:choose>
            <xsl:when test="Block and ($vUsState or $vCanadianState)">
<!--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-3.0.1:LocationStateUSPostalServiceCode>
                    <xsl:value-of select="State"/>
                </nc-3.0.1:LocationStateUSPostalServiceCode>
                <nc:LocationPostalCode>
                    <xsl:value-of select="Zip"/>
                </nc:LocationPostalCode>
            </xsl:when>
            <xsl:when test="Foreign ='false' and ($vUsState or $vCanadianState)">
<!--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-3.0.1:LocationStateUSPostalServiceCode>
                    <xsl:value-of select="State"/>
                </nc-3.0.1: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代码仅在CaseParty下有地址时才有效,如果CaseParty下没有地址则无效。它无法在Party

下显示地址
<nc:Address>
<nc:AddressFullText>
    <xsl:for-each select="/Integration/Case/CaseParty[Connection/@Word='DFD']/Address">
        <xsl:call-template name="Address"/>
    </xsl:for-each>
</nc:AddressFullText>

1 个答案:

答案 0 :(得分:0)

问题中提到了两个xpath规则,第一个是:

/Integration/Case/CaseParty[Connection/@Word='DFD']/Address 

,第二个是:

/Integration/Party/Address[@PartyCurrent="true"]

您可以使用xpath union(|)组合这两者。但由于第二条规则仅在第一条规则不满足时使用,我们还需要在第二条规则中添加一个否定第一条规则的谓词,如此(格式化为可读性):

/Integration[
    not(
        Case/CaseParty[Connection/@Word='DFD']/Address
    )
]
/Party
/Address[@PartyCurrent='true']

然后,您可以在此处使用XSL select部分中的组合规则:

<xsl:for-each select="/Integration/Case/CaseParty[Connection/@Word='DFD']/Address | /Integration[not(Case/CaseParty[Connection/@Word='DFD']/Address)]/Party/Address[@PartyCurrent='true']">
    <xsl:call-template name="Address"/>
</xsl:for-each>