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

XSLT中substring-before处理含&节点失效问题求助

问题原因及解决方法

问题原因

  1. 条件判断逻辑错误:在<xsl:when>的test属性中,你用field[@MIDAS='#ob45[0].4600']是在当前已匹配的field节点下查找子节点,但当前节点本身就是目标节点,这个表达式永远找不到匹配项,导致contains返回false,代码始终走<xsl:otherwise>分支,不会执行截取操作。
  2. substring-before参数错误:你传入的第一个参数是字符串字面量'./',而非当前节点的文本值,这会让截取操作针对固定字符串'./',而非目标节点的实际内容。
  3. 匹配字符的误区:原XML中的&amp;经过解析后实际存储为&字符,但你在代码中判断的是是否包含&amp;字符串,和实际节点内容不匹配(不过结合第一条错误,这一步并未被触发)。

修正后的代码片段

<xsl:template match="field[@MIDAS='#ob45[0].4600']">
    <datafield tag="510" ind1=" " ind2=" ">
        <subfield code="a">
            <xsl:choose>
                <xsl:when test="contains(., '&amp;')">
                    <xsl:value-of select="substring-before(., '&amp;')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </subfield>
        <subfield code="4">rela</subfield>
        <subfield code="4">https://d-nb.info/standards/elementset/gnd#relatedCorporateBody</subfield>
        <subfield code="i">In Beziehung stehende Körperschaft</subfield>
        <subfield code="9">
            <xsl:text>v:</xsl:text>
            <xsl:value-of select="../field[@MIDAS='#ob45[0] Sozietaet']"/>
        </subfield>
    </datafield>
</xsl:template>

额外说明

  • .在XSLT中代表当前节点,用它可以直接获取当前匹配节点的文本值,无需再次通过属性查找。
  • 因为XSLT本身是XML格式,代码中需要用&amp;来表示&字符,这样处理器才能正确解析并匹配节点文本中的&。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 22:01:16