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

两种XSLT方式获取期望值失败,求排查模板匹配或条件问题

问题分析与解决方案

你的两种XSLT方案都未返回结果,核心问题集中在注释错误、路径匹配偏差和命名空间误用这几点,具体如下:

核心问题点

  • 注释未闭合:<!-- Solution 1>缺少结尾的-->,导致后续所有代码都被当作注释处理,根本不会执行。
  • 命名空间前缀滥用:你的XML元素没有使用命名空间,但XSLT中用了*:前缀,这会导致元素匹配失败(除非XSLT中声明了对应命名空间,但你未提供这部分配置)。
  • XPath路径错误:
    • Controlling是CPol的子元素,直接写*:Controlling是从当前Pol节点查找,找不到对应元素;
    • StateProvCd位于InsOrPrincipal/General/Addr层级下,直接写*:StateProvCd路径不完整,无法匹配目标节点。

修正后的代码

方案1(修复后)

假设XML无命名空间,去掉*:前缀并修正路径:

<xsl:template match="Pol">
    <!-- Solution 1 -->
    <xsl:value-of select="if(string-length(normalize-space(CPol/Controlling/@CodeListRef))>0) 
        then (CPol/Controlling/@CodeListRef, CPol/Controlling) 
        else (InsOrPrincipal/General/Addr/StateProvCd/@CodeListRef, InsOrPrincipal/General/Addr/StateProvCd)"/>
</xsl:template>

方案2(修复后)

修正路径和注释,同时确保fns:listValue函数已正确定义:

<xsl:template match="Pol">
    <!-- Solution 2 -->
    <xsl:choose>
        <xsl:when test="CPol/Controlling">
            <xsl:value-of select="fns:listValue(CPol/Controlling/@CodeListRef, CPol/Controlling)"/>
        </xsl:when>
        <xsl:when test="InsOrPrincipal/General/Addr">
            <xsl:value-of select="fns:listValue(InsOrPrincipal/General/Addr/StateProvCd/@CodeListRef, InsOrPrincipal/General/Addr/StateProvCd)"/>
        </xsl:when>
        <xsl:otherwise/>
    </xsl:choose>
</xsl:template>

额外说明

如果你的XML实际使用了命名空间,需要在XSLT头部声明对应命名空间前缀,例如:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://你的命名空间URI">
    <xsl:template match="ns:Pol">
        <!-- 用ns:前缀替换所有元素前的*: -->
        <xsl:value-of select="if(string-length(normalize-space(ns:CPol/ns:Controlling/@CodeListRef))>0) 
            then (ns:CPol/ns:Controlling/@CodeListRef, ns:CPol/ns:Controlling) 
            else (ns:InsOrPrincipal/ns:General/ns:Addr/ns:StateProvCd/@CodeListRef, ns:InsOrPrincipal/ns:General/ns:Addr/ns:StateProvCd)"/>
    </xsl:template>
</xsl:stylesheet>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 11:41:20