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

如何用XSLT转换含重复Job_Classification的Workday XML为指定行列格式

完善XSLT实现XML到行列格式的转换

你需要将Workday的SOAP响应XML转换为竖线分隔的行列格式,每个Job_Classification对应一行,每行以Job_Classification_Group_ID开头,包含组的相关信息和对应分类的ID。我来帮你完善不完整的XSLT:

需求输出格式示例

Job_Classification_Group_ID|wd:ID|wd:Effective_Date|wd:Job_Classification_Group_Name|wd:Location_Reference[2]|wd:Location_Reference[3]|wd:Location_Reference[4]|Job_Classification_Reference_ID
KldB2010_Germany|KldB2010_Germany|2018-04-18-07:00|Klassifikation der Berufe 2010 (KldB 2010)|DE|DEU|276|KldB2010_Germany_1_Land-,_Forst-_und_Tierwirtschaft_und_Gartenbau
KldB2010_Germany|KldB2010_Germany|2018-04-18-07:00|Klassifikation der Berufe 2010 (KldB 2010)|DE|DEU|276|KldB2010_Germany_11_Land-,_Tier-_und_Forstwirtschaftsberufe

完整的XSLT代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:wd="urn:com.workday/bsvc"
                xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
                exclude-result-prefixes="env wd">

    <!-- 匹配根节点,开始转换 -->
    <xsl:template match="/">
        <!-- 输出表头行 -->
        <xsl:text>Job_Classification_Group_ID|wd:ID|wd:Effective_Date|wd:Job_Classification_Group_Name|wd:Location_Reference[2]|wd:Location_Reference[3]|wd:Location_Reference[4]|Job_Classification_Reference_ID&#10;</xsl:text>
        
        <!-- 遍历每个Job_Classification_Group -->
        <xsl:for-each select="env:Envelope/env:Body/wd:Get_Job_Classification_Groups_Response/wd:Response_Data/wd:Job_Classification_Group">
            <!-- 存储组的公共信息,避免重复查询 -->
            <xsl:variable name="groupID" select="wd:Job_Classification_Group_Reference/wd:ID[@wd:type='Job_Classification_Group_ID']"/>
            <xsl:variable name="groupData" select="wd:Job_Classification_Group_Data"/>
            <xsl:variable name="locationAlpha2" select="$groupData/wd:Location_Reference/wd:ID[@wd:type='ISO_3166-1_Alpha-2_Code']"/>
            <xsl:variable name="locationAlpha3" select="$groupData/wd:Location_Reference/wd:ID[@wd:type='ISO_3166-1_Alpha-3_Code']"/>
            <xsl:variable name="locationNumeric3" select="$groupData/wd:Location_Reference/wd:ID[@wd:type='ISO_3166-1_Numeric-3_Code']"/>
            
            <!-- 遍历组内的每个Job_Classification,生成一行 -->
            <xsl:for-each select="wd:Job_Classification_Group_Data/wd:Job_Classification">
                <xsl:value-of select="$groupID"/>
                <xsl:text>|</xsl:text>
                <xsl:value-of select="$groupData/wd:ID"/>
                <xsl:text>|</xsl:text>
                <xsl:value-of select="$groupData/wd:Effective_Date"/>
                <xsl:text>|</xsl:text>
                <xsl:value-of select="$groupData/wd:Job_Classification_Group_Name"/>
                <xsl:text>|</xsl:text>
                <xsl:value-of select="$locationAlpha2"/>
                <xsl:text>|</xsl:text>
                <xsl:value-of select="$locationAlpha3"/>
                <xsl:text>|</xsl:text>
                <xsl:value-of select="$locationNumeric3"/>
                <xsl:text>|</xsl:text>
                <xsl:value-of select="wd:Job_Classification_Reference/wd:ID[@wd:type='Job_Classification_Reference_ID']"/>
                <xsl:text>&#10;</xsl:text> <!-- 换行符 -->
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

代码关键点说明

  • 命名空间处理:正确声明了env和wd命名空间,并通过exclude-result-prefixes避免它们出现在输出中。
  • 公共信息复用:使用变量存储组的公共字段(比如组ID、位置信息),减少重复的XPath查询,提升效率。
  • 多分类遍历:通过嵌套的xsl:for-each,先遍历组,再遍历组内的每个分类,确保每个分类对应一行数据。
  • 精准字段提取:通过@wd:type属性筛选特定类型的ID(比如Job_Classification_Group_ID、ISO编码类型),保证提取的是你需要的字段。
  • 格式控制:使用|分隔字段,&#10;添加换行符,生成符合要求的行列格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:41:43