I am trying to create a xsl file which has data in xml file. The expected output should only have states of vehicles towed from Arizona,Florida and New York (The xml file has state codes in output it needs to be full name of the state in the output example(FL->Florida). Link to the color codes(https://www.sarasotataxcollector.com/dealers/color-codes) which should be an abbreviation again in the output in xml it is just the color code. The most recently towed vehicle must be listed first. I have attached until where I was able to progress, I am stuck on the following scenarios.
- Structuring of the output and the corresponding naming structure.
- I'm not sure how abbreviation works in XSl for state name and color codes to get the below output. I am a beginner in xsl, xml attaching what I've tried until now. I would really appreciate it if anyone can guide me on how to resolve the problem statement. This is first time reaching out to the stack overflow community. :) Expected output should be as follows, This is a sample but with my xml data it should be as the same structure as below.
<?xml version="1.0" encoding="UTF-8"?>
<summary>
<state name="Indiana">
<vehicle date="2020-02-27" plate="AE78117" color="White"/>
<vehicle date="2020-02-27" plate="AJ6869" color="White"/>
<vehicle date="2020-02-24" plate="305VWS" color="Red"/>
</state>
<state name="Florida">
<vehicle date="2020-02-27" plate="2879YS" color="Aluminum"/>
<vehicle date="2020-02-24" plate="KQRK15" color="Burgundy"/>
<vehicle date="2020-02-22" plate="DFHW62" color="White"/>
<vehicle date="2020-02-18" plate="JJZU83" color="White"/>
</state>
<state name="California">
<vehicle date="2020-02-26" plate="JGC22988" color="Black"/>
<vehicle date="2020-02-20" plate="JHK5166" color="Gray"/>
<vehicle date="2020-02-13" plate="HKJ8739" color="Aluminum"/>
<vehicle date="2020-02-07" plate="FMA1068" color="Red"/>
</state>
</summary>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
The XML file which has data Cars.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Towing.xsl"?>
<response>
<tow>
<tow_date>2021-09-27</tow_date>
<make>BMW</make>
<style>4D</style>
<color>GRY</color>
<state>CA</state>
<towed_to_address>10300 S. Doty</towed_to_address>
<tow_facility_phone>(773) 568-8495</tow_facility_phone>
<inventory_number>2921620</inventory_number>
</tow>
<tow>
<tow_date>2021-11-12</tow_date>
<make>NISS</make>
<style>LL</style>
<color>BLK</color>
<plate>JJA5163</plate>
<state>NY</state>
<towed_to_address>701 N. Sacramento</towed_to_address>
<tow_facility_phone>(773) 265-7605</tow_facility_phone>
<inventory_number>7016434</inventory_number>
</tow>
<tow>
<tow_date>2021-09-02</tow_date>
<make>CHEV</make>
<style>LL</style>
<color>GRY</color>
<plate>BRB257</plate>
<state>IN</state>
<towed_to_address>10300 S. Doty</towed_to_address>
<tow_facility_phone>(773) 568-8495</tow_facility_phone>
<inventory_number>2920773</inventory_number>
</tow>
<tow>
<tow_date>2021-11-03</tow_date>
<make>NISS</make>
<style>VN</style>
<color>BLU</color>
<plate>FBY402</plate>
<state>IN</state>
<towed_to_address>701 N. Sacramento</towed_to_address>
<tow_facility_phone>(773) 265-1846</tow_facility_phone>
<inventory_number>1542941</inventory_number>
</tow>
<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-11-07</tow_date>
<make>CHEV</make>
<style>4D</style>
<color>BLU</color>
<plate>282DLC</plate>
<state>IN</state>
<towed_to_address>701 N. Sacramento</towed_to_address>
<tow_facility_phone>(773) 265-7605</tow_facility_phone>
<inventory_number>7016277</inventory_number>
</tow>
<tow>
<tow_date>2021-10-23</tow_date>
<make>CHEV</make>
<style>VN</style>
<color>WHI</color>
<plate>AL33956</plate>
<state>AZ</state>
<towed_to_address>10300 S. Doty</towed_to_address>
<tow_facility_phone>(773) 568-8495</tow_facility_phone>
<inventory_number>2922721</inventory_number>
</tow>
<tow>
<tow_date>2021-11-11</tow_date>
<make>FORD</make>
<style>VN</style>
<color>WHI</color>
<plate>AJ82932</plate>
<state>AZ</state>
<towed_to_address>10300 S. Doty</towed_to_address>
<tow_facility_phone>(773) 568-8495</tow_facility_phone>
<inventory_number>2923515</inventory_number>
</tow>
<tow>
<tow_date>2021-09-21</tow_date>
<make>NISS</make>
<style>LL</style>
<color>BLK</color>
<plate>ZZ90397</plate>
<state>IL</state>
<towed_to_address>400 E. Lower Wacker</towed_to_address>
<tow_facility_phone>(312) 744-7550</tow_facility_phone>
<inventory_number>0245476</inventory_number>
</tow>
<tow>
<tow_date>2021-08-21</tow_date>
<make>LEXS</make>
<style>4D</style>
<color>GRY</color>
<plate>58AETN</plate>
<state>FL</state>
<towed_to_address>701 N. Sacramento</towed_to_address>
<tow_facility_phone>(773) 265-7605</tow_facility_phone>
<inventory_number>7011627</inventory_number>
</tow>
<tow>
<tow_date>2021-10-30</tow_date>
<make>HOND</make>
<style>4D</style>
<color>BLK</color>
<plate>8PED621</plate>
<state>CA</state>
<towed_to_address>400 E. Lower Wacker</towed_to_address>
<tow_facility_phone>(312) 744-7550</tow_facility_phone>
<inventory_number>0247537</inventory_number>
</tow>
<tow>
<tow_date>2021-11-07</tow_date>
<make>HYUN</make>
<style>4D</style>
<color>RED</color>
<plate>KNA3803</plate>
<state>NY</state>
<towed_to_address>701 N. Sacramento</towed_to_address>
<tow_facility_phone>(773) 265-7605</tow_facility_phone>
<inventory_number>7016231</inventory_number>
</tow>
<tow>
<tow_date>2021-11-07</tow_date>
<make>CHEV</make>
<style>4D</style>
<color>GRY</color>
<plate>HN8118</plate>
<state>IN</state>
<towed_to_address>701 N. Sacramento</towed_to_address>
<tow_facility_phone>(773) 265-7605</tow_facility_phone>
<inventory_number>7016177</inventory_number>
</tow>
<tow>
<tow_date>2021-08-22</tow_date>
<make>HOND</make>
<style>LL</style>
<color>BLK</color>
<plate>QTJF49</plate>
<state>FL</state>
<towed_to_address>701 N. Sacramento</towed_to_address>
<tow_facility_phone>(773) 265-7605</tow_facility_phone>
<inventory_number>7011737</inventory_number>
</tow>
<tow>
<tow_date>2021-11-11</tow_date>
<make>CHEV</make>
<style>LL</style>
<color>WHI</color>
<plate>AS16111</plate>
<state>IL</state>
<towed_to_address>10300 S. Doty</towed_to_address>
<tow_facility_phone>(773) 568-8495</tow_facility_phone>
<inventory_number>2923484</inventory_number>
</tow>
<tow>
<tow_date>2021-11-11</tow_date>
<make>NISS</make>
<style>4D</style>
<color>TAN</color>
<plate>Q286702</plate>
<state>IL</state>
<towed_to_address>10300 S. Doty</towed_to_address>
<tow_facility_phone>(773) 568-8495</tow_facility_phone>
<inventory_number>2923487</inventory_number>
</tow>
<tow>
<tow_date>2021-09-18</tow_date>
<make>CHEV</make>
<style>4D</style>
<color>GRY</color>
<plate>QGBU61</plate>
<state>FL</state>
<towed_to_address>400 E. Lower Wacker</towed_to_address>
<tow_facility_phone>(312) 744-7550</tow_facility_phone>
<inventory_number>0245317</inventory_number>
</tow>
<tow>
<tow_date>2021-10-30</tow_date>
<make>DODG</make>
<style>4D</style>
<color>BLK</color>
<plate>8UXV304</plate>
<state>CA</state>
<towed_to_address>10300 S. Doty</towed_to_address>
<tow_facility_phone>(773) 568-8495</tow_facility_phone>
<inventory_number>2922994</inventory_number>
</tow>
<tow>
<tow_date>2021-10-25</tow_date>
<make>FORD</make>
<style>LL</style>
<model>TK</model>
<color>WHI</color>
<plate>NYVX68</plate>
<state>FL</state>
<towed_to_address>701 N. Sacramento</towed_to_address>
<tow_facility_phone>(773) 265-7605</tow_facility_phone>
<inventory_number>7015485</inventory_number>
</tow>
<tow>
<tow_date>2021-10-11</tow_date>
<make>JEEP</make>
<style>LL</style>
<color>WHI</color>
<plate>8TIN875</plate>
<state>CA</state>
<towed_to_address>10300 S. Doty</towed_to_address>
<tow_facility_phone>(773) 568-8495</tow_facility_phone>
<inventory_number>2922224</inventory_number>
</tow>
</response>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Towing.xsl This is what I've tried any suggestions to make it better are welcome.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:element name="state">
<xsl:for-each select="response/tow[state='AZ']">
<xsl:sort select="tow_date" order="descending" />
<state name ="AZ">
<xsl:element name = "vehicle">
<xsl:value-of select="tow_date" />
,
<xsl:value-of select="plate" />
,
<xsl:value-of select="color" />
</xsl:element>
</state>
</xsl:for-each>
<xsl:for-each select="response/tow[state='FL']">
<state name="FL">
<xsl:value-of select="tow_date" />
,
<xsl:value-of select="plate" />
,
<xsl:value-of select="color" />
</state>
</xsl:for-each>
<xsl:for-each select="response/tow[state='NY']">
<state name="NY">
<xsl:value-of select="tow_date" />
,
<xsl:value-of select="plate" />
,
<xsl:value-of select="color" />
</state>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
First, you don't want to write the same code three times, so use a single template to process tows from all three states:
XSLT 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:key name="tow-by-state" match="tow" use="state" />
<xsl:template match="/response">
<summary>
<state name="Arizona">
<xsl:apply-templates select="key('tow-by-state', 'AZ')">
<xsl:sort select="tow_date" data-type="text" order="descending"/>
</xsl:apply-templates>
</state>
<state name="Florida">
<xsl:apply-templates select="key('tow-by-state', 'FL')">
<xsl:sort select="tow_date" data-type="text" order="descending"/>
</xsl:apply-templates>
</state>
<state name="New York">
<xsl:apply-templates select="key('tow-by-state', 'NY')">
<xsl:sort select="tow_date" data-type="text" order="descending"/>
</xsl:apply-templates>
</state>
</summary>
</xsl:template>
<xsl:template match="tow">
<vehicle date="{tow_date}" plate="{plate}" color="{color}"/>
</xsl:template>
</xsl:stylesheet>
Now comes the problem of looking up the color by its color code. This too could be rather simple, except that your reference states that there can be two color codes - which makes it a bit more complex, esp. if you are limited to XSLT 1.0:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:codes="https://www.sarasotataxcollector.com/dealers/color-codes"
exclude-result-prefixes="codes">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="tow-by-state" match="tow" use="state" />
<xsl:template match="/response">
<summary>
<state name="Arizona">
<xsl:apply-templates select="key('tow-by-state', 'AZ')">
<xsl:sort select="tow_date" data-type="text" order="descending"/>
</xsl:apply-templates>
</state>
<state name="Florida">
<xsl:apply-templates select="key('tow-by-state', 'FL')">
<xsl:sort select="tow_date" data-type="text" order="descending"/>
</xsl:apply-templates>
</state>
<state name="New York">
<xsl:apply-templates select="key('tow-by-state', 'NY')">
<xsl:sort select="tow_date" data-type="text" order="descending"/>
</xsl:apply-templates>
</state>
</summary>
</xsl:template>
<xsl:template match="tow">
<vehicle date="{tow_date}" plate="{plate}">
<xsl:attribute name="color">
<xsl:variable name="colors" select="document('')/xsl:stylesheet/codes:colors/color" />
<xsl:choose>
<xsl:when test="contains(color, '/')">
<xsl:variable name="code1" select="substring-before(color, '/')" />
<xsl:variable name="code2" select="substring-after(color, '/')" />
<xsl:value-of select="$colors[@code=$code1]"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$colors[@code=$code2]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$colors[@code=current()/color]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</vehicle>
</xsl:template>
<codes:colors>
<color code="SIL">Aluminum</color>
<color code="AME">Amethyst</color>
<color code="BGE">Beige</color>
<color code="BLK">Black</color>
<color code="BLU">Blue</color>
<color code="DBL">Blue (Dark)</color>
<color code="LBL">Blue (Light)</color>
<color code="BRZ">Bronze</color>
<color code="BRN">Brown</color>
<color code="MAR">Burgundy</color>
<color code="CAM">Camouflage</color>
<color code="COM">Chrome</color>
<color code="CPR">Copper</color>
<color code="CRM">Cream</color>
<color code="Gold">Gold</color>
<color code="GRY">Gray</color>
<color code="GRN">Green</color>
<color code="DGR">Green (Dark)</color>
<color code="LGR">Green (Light)</color>
<color code="CRM">Ivory</color>
<color code="LAV">Lavender</color>
<color code="MAR">Maroon</color>
<color code="MVE">Mauve</color>
<color code="ONG">Orange</color>
<color code="PNK">Pink</color>
<color code="PLE">Purple</color>
<color code="RED">Red</color>
<color code="SIL">Silver</color>
<color code="COM">Stainless Steel</color>
<color code="TAN">Tan</color>
<color code="TPE">Taupe</color>
<color code="TEA">Teal</color>
<color code="TRQ">Turquoise</color>
<color code="WHI">White</color>
<color code="YEL">Yellow</color>
<color code="MUL/COL">Multicolor</color>
</codes:colors>
</xsl:stylesheet>
Note that I have left two additional issues unsolved:
The code
SIL
has two entries: one for Aluminum and one for Silver;The code for Multicolor is
MUL/COL
- which clashes with the stated principle:If a vehicle is more than one color, the two color codes would be separated by a slash.