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

Status
Not open for further replies.

momo2000

Programmer
Jan 2, 2015
63
US
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
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="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:
<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>
 
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:
 <xsl:for-each select="/Integration/Party/DriversLicense[@current='true']/DriversLicenseState">
[indent]<xsl:if test="@string-length(@Word) = 4 and substring(@Word,1,2) = 'CD'">
[indent]<!-- Do what you want to do when it is Canadian -->[/indent]
</xsl:if>[/indent]
</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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top