XSLT 1.0提取TEI XML中surface的xml:id与对应graphic的url方案
解决方案
1. 顶层新增查找键配置
首先确认你的xsl:stylesheet节点已声明TEI命名空间,再新增key用于快速匹配surface节点:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tei="http://www.tei-c.org/ns/1.0" exclude-result-prefixes="tei"> <!-- 新增:通过xml:id匹配surface节点的查找键 --> <xsl:key name="surface-by-id" match="tei:surface" use="@xml:id"/> <!-- 其余原有顶层配置 --> </xsl:stylesheet>
2. 修改tei:pb匹配模板的取值逻辑
你之前的写法直接读取了根路径下第一个graphic节点的url,改成通过当前pb的facs属性查找对应节点即可:
<xsl:template match="tei:pb"> <!-- 处理facs属性的#前缀:TEI规范中facs属性通常为# + xml:id的格式 --> <xsl:variable name="targetSurfaceId" select="substring-after(@facs, '#')"/> <!-- 通过key查找对应surface下的graphic url --> <xsl:variable name="targetImgUrl" select="key('surface-by-id', $targetSurfaceId)/tei:graphic/@url"/> <!-- 生成链接,增加判空逻辑避免无效链接 --> <xsl:choose> <xsl:when test="$targetImgUrl"> <a href="{$targetImgUrl}"> <!-- 保留原有pb标签的渲染逻辑,比如输出页码 --> <xsl:value-of select="@n"/> </a> </xsl:when> <xsl:otherwise> <!-- 无匹配时的兜底展示 --> <span class="pb-no-img"> <xsl:value-of select="@n"/> </span> </xsl:otherwise> </xsl:choose> </xsl:template>
注意事项
- 如果你的
facs属性值没有#前缀,直接把substring-after(@facs, '#')替换为@facs即可 - 如果存在一个
surface下对应多个graphic的场景,可按需增加过滤条件,比如取指定分辨率的图:tei:graphic[@type='high-res']/@url
内容的提问来源于stack exchange,提问作者KWunsch
相关产品推荐
相关产品推荐

