如何在XSLT中排除<altIdentifier>渲染<msIdentifier>子元素
TEI XML中用XSLT排除msIdentifier下的altIdentifier节点
你的问题出在XPath写法上,原代码里的[not(/altIdentifier)]是检查根节点下是否存在altIdentifier,而非当前msIdentifier节点的子元素;而且你选择的是msIdentifier本身,它的所有子元素仍会被默认模板处理,包括altIdentifier。
正确解决方法
方法1:处理msIdentifier时,仅遍历除altIdentifier外的子元素
在msIdentifier的模板里,明确指定要处理的子元素,排除altIdentifier:
<xsl:template match="t:msIdentifier"> <!-- 选择当前msIdentifier下所有非altIdentifier的子元素 --> <xsl:apply-templates select="*[not(self::t:altIdentifier)]"/> </xsl:template>
方法2:直接在apply-templates中指定目标子元素
如果不需要单独编写msIdentifier的模板,也可以直接在调用时筛选:
<xsl:apply-templates select="//t:msDesc/t:msIdentifier/*[not(self::t:altIdentifier)]"/>
关键注意事项
- 命名空间匹配:确保你的XSLT已正确声明TEI命名空间并绑定到
t前缀(和XML中的命名空间一致),示例:
<xsl:stylesheet xmlns:t="http://www.tei-c.org/ns/1.0" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
命名空间不匹配的话,XPath会找不到任何节点。
- 默认模板影响:XSLT默认模板会输出所有文本节点,哪怕你没写专门的模板,
altIdentifier里的文本也会被输出。通过上述筛选,就能跳过altIdentifier及其子元素的处理。
示例效果
假设你的XML结构如下:
<TEI xmlns="http://www.tei-c.org/ns/1.0"> <msDesc> <msIdentifier> <settlement>Città del Vaticano</settlement> <repository>Biblioteca Apostolica Vaticana</repository> <idno>Pal. gr. 44</idno> <altIdentifier> <idno>65777</idno> </altIdentifier> </msIdentifier> </msDesc> </TEI>
使用上述XSLT后,会输出你期望的内容:Città del Vaticano Biblioteca Apostolica Vaticana Pal. gr. 44
内容的提问来源于stack exchange,提问作者fischiola
相关产品推荐
相关产品推荐

