You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于指定条件使用XSL将XML拖车数据转换为目标格式的求助

XSLT 处理拖车数据实现方案

核心逻辑说明

  • 过滤:仅保留州字段为AZ、FL、NY的记录
  • 缩写转译:通过内置映射表实现州缩写、颜色缩写到全称的转换
  • 排序:按拖移时间倒序排列,最新记录优先展示

完整实现代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 州缩写与全称映射表 -->
    <xsl:variable name="stateMapping">
        <item abbr="AZ" full="Arizona"/>
        <item abbr="FL" full="Florida"/>
        <item abbr="NY" full="New York"/>
    </xsl:variable>

    <!-- 颜色缩写与全称映射表(匹配标准机动车颜色编码规则) -->
    <xsl:variable name="colorMapping">
        <item abbr="BK" full="Black"/>
        <item abbr="WH" full="White"/>
        <item abbr="RD" full="Red"/>
        <item abbr="BL" full="Blue"/>
        <item abbr="GR" full="Green"/>
        <item abbr="GY" full="Gray"/>
        <item abbr="YL" full="Yellow"/>
        <item abbr="OR" full="Orange"/>
        <item abbr="PR" full="Purple"/>
        <item abbr="BN" full="Brown"/>
        <item abbr="SI" full="Silver"/>
        <item abbr="GD" full="Gold"/>
    </xsl:variable>

    <xsl:template match="/">
        <!-- 输出根节点,可根据你的示例结构调整 -->
        <TowingVehicleList>
            <!-- 筛选目标州记录,按拖移时间倒序排列 -->
            <xsl:for-each select="Cars/TowingRecord[state=('AZ','FL','NY')]">
                <xsl:sort select="towingTimestamp" order="descending" data-type="text"/>
                <Vehicle>
                    <!-- 车牌信息 -->
                    <licenseNumber><xsl:value-of select="licenseNumber"/></licenseNumber>
                    <!-- 转译州全称 -->
                    <registerState>
                        <xsl:value-of select="document('')/*/xsl:variable[@name='stateMapping']/item[@abbr=current()/state]/@full"/>
                    </registerState>
                    <!-- 转译颜色全称 -->
                    <vehicleColor>
                        <xsl:value-of select="document('')/*/xsl:variable[@name='colorMapping']/item[@abbr=current()/colorCode]/@full"/>
                    </vehicleColor>
                    <brand><xsl:value-of select="brand"/></brand>
                    <model><xsl:value-of select="model"/></model>
                    <towingTime><xsl:value-of select="towingTimestamp"/></towingTime>
                    <towingLocation><xsl:value-of select="towingLocation"/></towingLocation>
                </Vehicle>
            </xsl:for-each>
        </TowingVehicleList>
    </xsl:template>
</xsl:stylesheet>

适配调整说明

  • 代码中的节点路径(如Cars/TowingRecord、licenseNumber等)需要和你实际的Cars.xml节点结构保持一致
  • 若使用XSLT 2.0+版本,可替换为map结构实现更高性能的缩写查询,无需调用document('')
  • 若拖移时间为时间戳格式,可将xsl:sort的data-type属性改为number,避免排序错误
  • 颜色映射表可根据你的实际数据源中的缩写类型补充条目

内容的提问来源于stack exchange,提问作者user17547677

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 23:45:04