我正在創建一個 XSL 檔案,該檔案將從一個 XML 檔案中提取有關在我所在地區被拖走的汽車的資訊,并按汽車被拖走的日期按升序對其進行排序。我需要在轉換后的檔案中顯示拖車日期、車牌和汽車顏色。我的問題是每輛車的顏色都是縮寫的,我想要顏色的全名而不是三個字母的縮寫。
這是我的 XML 檔案:
<?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>
這是我的 XSL 檔案:
<?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>
我轉換后的檔案是這樣的:
<?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>
在我的轉換檔案中,我希望 WHI、GRY 和 BLK 的值變為 WHITE、GRAY 和 BLACK。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
請嘗試以下 XSLT。
顏色選擇基于<xsl:choose>分支元素的使用。
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>
uj5u.com熱心網友回復:
一個比 Yitzhak 更優雅/更易于維護的解決方案,如果有幾十個顏色代碼,性能更好,是將顏色詞匯表放在一個單獨的檔案中 colors.xml
<colors>
<color code="WHI">WHITE</color>
...
</colors>
定義索引:
<xsl:key name="color-codes" match="color" use="@code"/>
然后(在 XSLT 2.0 中)
<xsl:template match="tow">
<vehicle date="{tow_date}" plate="{plate}"
color="{key('color-codes', color, doc('colors.xml'))"}/>
</xsl:template>
或在 XSLT 1.0 中
<xsl:template match="tow">
<xsl:variable name="short-color" select="color"/>
<xsl:variable name="full-color">
<xsl:for-each select="document('colors.xml')">
<xsl:value-of select="key('color-codes', $short-color)"/>
</xsl:for-each>
</xsl:variable>
<vehicle date="{tow_date}" plate="{plate}"
color="{$full-color)"}/>
</xsl:template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/373593.html
上一篇:XPath不支持Let
