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 check State Code if it has four characters and then transform it to 2 character code?

How do I check State Code if it has four characters and then transform it to 2 character code?

How do I check State Code if it has four characters and then transform it to 2 character code?

(OP)
What I am trying to do is check in the XML document for the element

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

If State word found has four characters and begins with ‘CD’ only display the last two characters. In this case (example) I would display <nc:JurisdictionCanadianProvinceCode>ON</nc:JurisdictionCanadianProvinceCode> otherwise display the State/@Word

I am not sure how to add logic to do this.

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="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>
</Integration> 

XSLT code

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

RE: How do I check State Code if it has four characters and then transform it to 2 character code?

You are ignoring the useful fact that inside the context of <xsl:for-each> the context node becomes the selected node for that iteration. This will greatly simplify your XPath expressions within the for-each:

CODE --> XSLT

<xsl:for-each select="/Integration/Party/DriversLicense[@current='true']/DriversLicenseState">
<xsl:if test="@string-length(@Word) = 4 and substring(@Word,1,2) = 'CD'">
<!-- Do what you want to do when it is Canadian -->
</xsl:if>
</xsl:for-each>

I often use xsl:for-each to shift the context node simply to make my XPath expressions for a cluster of statements easier to read (and less error-prone to code).

Tom Morrison
Hill Country Software

RE: How do I check State Code if it has four characters and then transform it to 2 character code?

(OP)
Thanks Tom. This solution worked.

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