Home > Enterprise >  How can I replace an abbreviation with a string?
How can I replace an abbreviation with a string?

Time:12-01

I am creating an XSL file that will pull information from an XML file about cars that have been towed in my area and sort it in ascending order by the date the car was towed. I need to display the towed date, license plate, and car color in my transformed file. My problem is that the color for each car is abbreviated and I want the full name of the color instead of the three letter abbreviation.

Here is my XML file:

<?xml version="1.0"?>
<response>
    <tow>
        <tow_date>2021-10-10</tow_date>
        <make>CHRI</make>
        <style>4D</style>
        <color>WHI</color>
        <plate>549XIB</plate>
        <state>AZ</state>
        <towed_to_address>10300 S. Doty</towed_to_address>
        <tow_facility_phone>(773) 568-8495</tow_facility_phone>
        <inventory_number>2922125</inventory_number>
    </tow>
    <tow>
        <tow_date>2021-10-24</tow_date>
        <make>TOYT</make>
        <style>4T</style>
        <color>GRY</color>
        <plate>LDNE06</plate>
        <state>FL</state>
        <towed_to_address>701 N. Sacramento</towed_to_address>
        <tow_facility_phone>(773) 265-7605</tow_facility_phone>
        <inventory_number>7015429</inventory_number>
    </tow>
    <tow>
        <tow_date>2021-11-06</tow_date>
        <make>JEEP</make>
        <style>LL</style>
        <color>BLK</color>
        <plate>HDU4518</plate>
        <state>NY</state>
        <towed_to_address>701 N. Sacramento</towed_to_address>
        <tow_facility_phone>(773) 265-7605</tow_facility_phone>
        <inventory_number>7016130</inventory_number>
    </tow>
</response>

Here is my XSL file:

<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <xsl:element name="summary">
            <state name="Arizona">
                <xsl:apply-templates select="response/tow[state = 'AZ']">
                    <xsl:sort select="tow_date" order="ascending" />
                </xsl:apply-templates>
            </state>
            <state name="Florida">
                <xsl:apply-templates select="response/tow[state = 'FL']">
                    <xsl:sort select="tow_date" order="ascending" />
                </xsl:apply-templates>
            </state>
            <state name="New York">
                <xsl:apply-templates select="response/tow[state = 'NY']">
                    <xsl:sort select="tow_date" order="ascending" />
                </xsl:apply-templates>
            </state>
        </xsl:element>
    </xsl:template>

    <xsl:template match="tow" >
        <vehicle date="{tow_date}" plate="{plate}" color="{color}" />
    </xsl:template>

</xsl:stylesheet>

My transformed document comes out like this:

<?xml version="1.0" encoding="UTF-8"?>
<summary>
    <state name="Arizona">
        <vehicle date="2021-10-10" plate="549XIB" color="WHI"/>
    </state>
    <state name="Florida">
        <vehicle date="2021-10-24" plate="LDNE06" color="GRY"/>
    </state>
    <state name="New York">
        <vehicle date="2021-11-06" plate="HDU4518" color="BLK"/>
    </state>
</summary>

In my transformed file, I would like the values for WHI, GRY, and BLK to become WHITE, GRAY, and BLACK. How can I do that?

CodePudding user response:

Please try the following XSLT.

The color selection is based on use of the <xsl:choose> branching element.

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <xsl:element name="summary">
            <state name="Arizona">
                <xsl:apply-templates select="response/tow[state = 'AZ']">
                    <xsl:sort select="tow_date" order="ascending"/>
                </xsl:apply-templates>
            </state>
            <state name="Florida">
                <xsl:apply-templates select="response/tow[state = 'FL']">
                    <xsl:sort select="tow_date" order="ascending"/>
                </xsl:apply-templates>
            </state>
            <state name="New York">
                <xsl:apply-templates select="response/tow[state = 'NY']">
                    <xsl:sort select="tow_date" order="ascending"/>
                </xsl:apply-templates>
            </state>
        </xsl:element>
    </xsl:template>

    <xsl:template match="tow">
        <vehicle date="{tow_date}" plate="{plate}">
            <xsl:attribute name="color">
                <xsl:choose>
                    <xsl:when test="color='WHI'">WHITE</xsl:when>
                    <xsl:when test="color='GRY'">GRAY</xsl:when>
                    <xsl:when test="color='BLK'">BLACK</xsl:when>
                    <xsl:otherwise>... some default color ....</xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
        </vehicle>
    </xsl:template>
</xsl:stylesheet>
  • Related