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

如何调整XSLT模板匹配规则,实现通用化历史法律链接渲染

XSLT模板通用化调整实现方案

需求回顾

  • 保留class="artikel"的h4元素原有渲染逻辑
  • 无上述h4时,匹配id以ln开头且包含class="historicalVersions"子div的div元素
  • 若目标元素内存在h4,将历史版本链接插入已有的title-bar类div中;否则放在目标div之后

调整后的XSLT代码

<!-- 保留原有h4[@class='artikel']的渲染逻辑 -->
<xsl:template match="xhtml:h4[@class='artikel']">
    <xsl:variable name="currentAnchor" select="../@id" />
    <div class="row article title-bar">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates />
        </xsl:copy>
        <xsl:call-template name="AddHistoricAndFutureLawLinks">
            <xsl:with-param name="currentAnchor" select="$currentAnchor"/>
        </xsl:call-template>
    </div>
</xsl:template>

<!-- 新增模板:匹配符合条件的div元素 -->
<xsl:template match="xhtml:div[starts-with(@id, 'ln') and .//xhtml:div[@class='historicalVersions']]">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <!-- 优先渲染内部h4,触发对应的模板逻辑 -->
        <xsl:apply-templates select="xhtml:h4"/>
        <!-- 渲染div内除h4外的其他子元素 -->
        <xsl:apply-templates select="node()[not(self::xhtml:h4)]"/>
        
        <!-- 若div内无h4,直接在div末尾添加历史链接 -->
        <xsl:if test="not(xhtml:h4)">
            <xsl:call-template name="AddHistoricAndFutureLawLinks">
                <xsl:with-param name="currentAnchor" select="@id"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:copy>
</xsl:template>

<!-- 通用h4模板:处理非artikel类的h4元素 -->
<xsl:template match="xhtml:h4[not(@class='artikel')]">
    <div class="row article title-bar">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates />
        </xsl:copy>
        <!-- 将历史链接插入title-bar容器内 -->
        <xsl:call-template name="AddHistoricAndFutureLawLinks">
            <xsl:with-param name="currentAnchor" select="../@id"/>
        </xsl:call-template>
    </div>
</xsl:template>

逻辑说明

  1. 原有逻辑兼容:完全保留h4[@class='artikel']的模板,确保旧场景不受影响
  2. 目标div定位:通过starts-with(@id, 'ln')和后代元素匹配,精准定位需要添加历史链接的div
  3. 插入规则适配:
    • 若div内有h4,通过通用h4模板自动将历史链接嵌入title-bar
    • 若无h4,直接在div末尾追加历史链接模块
  4. 通用h4处理:统一非artikel类h4的渲染格式,同时满足历史链接的插入要求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 02:55:16