我正在创建一个 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。我怎样才能做到这一点?
请尝试以下 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)