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

XSLT处理含转义HTML的Workday XML生成JSON结构遇阻

解决Workday XML转义HTML导致XSLT提取失败的问题

核心问题是<wd:response>里的转义HTML字符(&lt;p&gt;、&lt;span&gt;等)被当作纯文本处理,无法直接用XPath提取内部的MRQ1、DRQ1等字段值。需要先把转义内容还原为可解析的XML节点,再进行数据提取,最后生成目标JSON。

步骤1:还原转义的HTML内容

根据你使用的XSLT版本,选择对应的处理方式:

方式一:XSLT 2.0+(推荐,支持parse-xml函数)

直接用parse-xml把转义的HTML字符串解析成XML节点集,注意要给转义内容套一个根节点(否则HTML片段可能不合法):

<!-- 定义变量存储解析后的HTML节点 -->
<xsl:variable name="parsed-response">
  <root>
    <xsl:value-of select="wd:response" disable-output-escaping="yes"/>
  </root>
</xsl:variable>
<!-- 转换为可查询的节点集 -->
<xsl:variable name="response-nodes" select="parse-xml($parsed-response)/root"/>

方式二:XSLT 1.0(依赖扩展函数)

如果只能用XSLT 1.0,需要借助处理器的扩展函数(比如Saxon的exsl:node-set或MSXML的msxsl:node-set)来把结果树片段转为节点集:

<xsl:variable name="parsed-response">
  <root>
    <xsl:value-of select="wd:response" disable-output-escaping="yes"/>
  </root>
</xsl:variable>
<xsl:variable name="response-nodes" select="exsl:node-set($parsed-response)/root"/>
<!-- 注意:需要在XSLT头部声明exsl命名空间:xmlns:exsl="http://exslt.org/common" -->

步骤2:提取MRQ1、DRQ1等字段值

通过XPath从解析后的节点里提取目标字段,比如假设转义后的HTML结构是<p>MRQ1: 具体值</p>:

<xsl:variable name="MRQ1-value" select="$response-nodes/p[starts-with(text(), 'MRQ1:')]/substring-after(text(), 'MRQ1: ')"/>
<xsl:variable name="DRQ1-value" select="$response-nodes/p[starts-with(text(), 'DRQ1:')]/substring-after(text(), 'DRQ1: ')"/>
<xsl:variable name="PSQ1-value" select="$response-nodes/p[starts-with(text(), 'PSQ1:')]/substring-after(text(), 'PSQ1: ')"/>

步骤3:生成目标JSON结构

用XSLT的JSON生成逻辑(XSLT 3.0可直接用xsl:output method="json",低版本可手动拼接):

XSLT 3.0示例

<xsl:output method="json" indent="yes"/>
<xsl:template match="/">
  <xsl:map>
    <xsl:map-entry key="'MRQ1'" select="$MRQ1-value"/>
    <xsl:map-entry key="'DRQ1'" select="$DRQ1-value"/>
    <xsl:map-entry key="'PSQ1'" select="$PSQ1-value"/>
    <!-- 其他字段同理 -->
  </xsl:map>
</xsl:template>

低版本手动拼接示例

<xsl:output method="text"/>
<xsl:template match="/">
  {
    "MRQ1": "<xsl:value-of select="$MRQ1-value"/>",
    "DRQ1": "<xsl:value-of select="$DRQ1-value"/>",
    "PSQ1": "<xsl:value-of select="$PSQ1-value"/>"
  }
</xsl:template>

注意事项

  • 如果转义的HTML里有<span>等嵌套标签,需要调整XPath路径,比如$response-nodes/p/span[starts-with(text(), 'MRQ1:')]/substring-after(text(), 'MRQ1: ')
  • 确保XSLT处理器支持对应的扩展函数或版本特性(比如Saxon支持XSLT 2.0+,MSXML仅支持1.0)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 09:33:23