Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I display correct address type based on state code found in xml? 1

Status
Not open for further replies.

momo2000

Programmer
Jan 2, 2015
63
0
0
US
I would like to display Party address as non-standard or standard or Foreign address based on the CaseParty/State or Party/State found in the in the xml document. In my xsl I have already checked for the State in the referring xsd document. If the State is not found in the two referring documents, I would like to display the party address as foreign address. My code is not displaying foreign address as foreign which has a different format as I have shown in the desired output.

Desired output

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

Wrong output
XML:
<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>

My xml document

XML:
<Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="[URL unfurl="true"]http://tsgweb.com"[/URL] xmlns:IXML="[URL unfurl="true"]http://tsgweb.com"[/URL] 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>

Sample referring usps_states.xsd document
XML:
<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:structures="[URL unfurl="true"]http://release.niem.gov/niem/structures/3.0/"[/URL] xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] xmlns:appinfo="[URL unfurl="true"]http://release.niem.gov/niem/appinfo/3.0/"[/URL] xmlns:usps-3.0.1="[URL unfurl="true"]http://publication.niem.gov/niem/codes/usps_states/3.0/1/"[/URL] xmlns:ct="[URL unfurl="true"]http://release.niem.gov/niem/conformanceTargets/3.0/"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] targetNamespace="[URL unfurl="true"]http://publication.niem.gov/niem/codes/usps_states/3.0/1/"[/URL] version="1" xsi:schemaLocation="[URL unfurl="true"]http://release.niem.gov/niem/appinfo/3.0/[/URL] ../../../../appinfo/3.0/appinfo.xsd [URL unfurl="true"]http://release.niem.gov/niem/conformanceTargets/3.0/[/URL] ../../../../conformanceTargets/3.0/conformanceTargets.xsd" ct:conformanceTargets="[URL unfurl="true"]http://reference.niem.gov/niem/specification/naming-and-design-rules/3.0/#ReferenceSchemaDocument">[/URL]
    <xs:annotation>
        <xs:documentation>U.S. Postal Service Supplement</xs:documentation>
    </xs:annotation>
    <xs:import namespace="[URL unfurl="true"]http://release.niem.gov/niem/structures/3.0/"[/URL] 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>

Sample referring post-canada.xsd document

XML:
<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:structures="[URL unfurl="true"]http://release.niem.gov/niem/structures/3.0/"[/URL] xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] xmlns:appinfo="[URL unfurl="true"]http://release.niem.gov/niem/appinfo/3.0/"[/URL] xmlns:can="[URL unfurl="true"]http://release.niem.gov/niem/codes/canada_post/3.0/"[/URL] xmlns:ct="[URL unfurl="true"]http://release.niem.gov/niem/conformanceTargets/3.0/"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] targetNamespace="[URL unfurl="true"]http://release.niem.gov/niem/codes/canada_post/3.0/"[/URL] version="1" xsi:schemaLocation="[URL unfurl="true"]http://release.niem.gov/niem/appinfo/3.0/[/URL] ../../../appinfo/3.0/appinfo.xsd [URL unfurl="true"]http://release.niem.gov/niem/conformanceTargets/3.0/[/URL] ../../../conformanceTargets/3.0/conformanceTargets.xsd" ct:conformanceTargets="[URL unfurl="true"]http://reference.niem.gov/niem/specification/naming-and-design-rules/3.0/#ReferenceSchemaDocument">[/URL]
    <xs:import namespace="[URL unfurl="true"]http://release.niem.gov/niem/structures/3.0/"[/URL] 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>

My xsl code

Code:
 <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>
 
Code:
<xsl:when test="Block and string-length($vUsState or $vCanadianState) &amp;gt; 0">

In the XML instance under scrutiny, Block will be true. So then it boils down to then value of the second term, string-length() &gt; 0.

Since I can only presume from your code that this term will also always be true (the variable will most likely have whitespace, but this can depend on the processor), the first <xsl:when> is selected.

Since you are attempting to validate the state/province, it would seems that something like this is more appropriate (Canada left as an exercise):
Code:
<xsl:variable name="vUsState" select="string-length(normalize-space(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))"/>

You could then change then when test to:
Code:
<xsl:when test="Block and ($vUsState + $vCanadianState) &amp;gt; 0">

Tom Morrison
Hill Country Software
 
I want to be a bit less terse on the following:
Code:
Block and string-length($vUsState or $vCanadianState) &amp;gt; 0

The string-length() function takes a string datatype. Let's look at the datatype conversions going on.[ul]
[li]$vUsState and $vCanadianState are strings. Since they are used in a boolean expression, they will be converted to booleans. A zero-length string converts to a false value, and a nonzero-length string converts to a true value[/li]
[li]the boolean expression is evaluated[/li]
[li]the result boolean value is converted to a string, which will be either 'true' or 'false'[/li]
[li]the string-length function will evaluate to either 4 or 5, i.e. always greater than 0, so the term will always be true[/li]
[/ul]

I hope this provides a bit more understanding.

ALSO:

I might over-use normalize-string(); however, it makes explicit that I want a string containing only whitespace to be reduced to zero-length. When it comes to whitespace, I have had differences of opinion, shall we say, among XSL processors. I have found this most often in recursive templates that are processing strings, which often use a zero-length input string to terminate recursion.

Tom Morrison
Hill Country Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top