INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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

CODE --> xml

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

Wrong output

CODE --> 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

CODE --> 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> 

Sample referring usps_states.xsd document

CODE --> xml

<?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> 

Sample referring post-canada.xsd document

CODE --> xml

<?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> 

My xsl code

CODE --> 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, '
')"/>
                            <xsl:value-of select="concat(AddressLine2, '
')"/>
                            <xsl:value-of select="concat(AddressLine3, '
')"/>
                            <xsl:value-of select="concat(AddressLine4, '
')"/>
                        </nc:AddressFullText>
                    </xsl:otherwise>
                </xsl:choose>
        </nc:Address>
    </xsl:template> 

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

CODE --> XSLT_fragment

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

<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 --> XSLT_fragment

<xsl:when test="Block and ($vUsState + $vCanadianState) &gt; 0"> 

Tom Morrison
Hill Country Software

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

I want to be a bit less terse on the following:

CODE --> XPath

Block and string-length($vUsState or $vCanadianState) &gt; 0 

The string-length() function takes a string datatype. Let's look at the datatype conversions going on.
  • $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
  • the boolean expression is evaluated
  • the result boolean value is converted to a string, which will be either 'true' or 'false'
  • the string-length function will evaluate to either 4 or 5, i.e. always greater than 0, so the term will always be true
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close