如何调整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>
逻辑说明
- 原有逻辑兼容:完全保留
h4[@class='artikel']的模板,确保旧场景不受影响 - 目标div定位:通过
starts-with(@id, 'ln')和后代元素匹配,精准定位需要添加历史链接的div - 插入规则适配:
- 若div内有h4,通过通用h4模板自动将历史链接嵌入
title-bar - 若无h4,直接在div末尾追加历史链接模块
- 若div内有h4,通过通用h4模板自动将历史链接嵌入
- 通用h4处理:统一非
artikel类h4的渲染格式,同时满足历史链接的插入要求
内容的提问来源于stack exchange,提问作者red guardgen
相关产品推荐
相关产品推荐

