【问题标题】:Ignore child nodes in XSLT template using mode使用模式忽略 XSLT 模板中的子节点
【发布时间】:2013-04-25 15:10:24
【问题描述】:

我正在将 XML 文档转换为 HTML 文件以供在线显示(作为电子书)。

XML 文件中的每一章都包含在<div> 中,并有一个标题 (<head>)。我需要将每个标题显示两次——一次作为目录的一部分在开头,第二次在每章的顶部。我在<xsl:template> 中使用了mode="toc" 来执行此操作。

我的问题是我的一些<head> 标题有一个子元素<note>,其中包含编辑脚注。当标题出现在章节顶部时,我需要处理这些 <note> 标签,但我不希望它们显示在目录中(即当 mode="toc" 时。

我的问题是如何告诉样式表处理目录的<head> 元素,但忽略任何子元素(应该出现)?

这是一个没有注释的示例标题,在目录模式下可以正常显示:

<div xml:id="d1.c1" type="chapter">
  <head>Pursuit of pleasure. Limits set to it by Virtue—
  Asceticism is Vice</head>
  <p>Contents of chapter 1 go here</p>
</div>

这里有一个注释,我想在生成目录时去掉它:

<div xml:id="d1.c6" type="chapter">
  <head>Happiness and Virtue, how diminished by Asceticism in an indirect
  way.—Useful and genuine obligations elbowed out by spurious ones<note
  xml:id="d1.c6fn1" type="editor">In the text, the author has noted at this 
  point: 'These topics must have been handled elsewhere: perhaps gone through 
  with. Yet what follows may serve for an Introduction.'</note></head>
  <p>Contents of chapter 6 go here</p>
</div>

我的 XSL 目前看起来像这样:

<xsl:template match="tei:head" mode="toc">
    <xsl:if test="../@type = 'chapter'">
        <h3><a href="#{../@xml:id}"><xsl:apply-templates/></a></h3>
    </xsl:if>
</xsl:template>

我尝试在toc 模式下添加一个新的空白模板匹配以供备注,但无济于事。例如:

<xsl:template match="tei:note" mode="toc"/>

我也试过tei:head/tei:note\\tei:head\tei:note

在匹配整个文档 (/) 的模板中,我使用以下内容显示目录:

<xsl:apply-templates select="//tei:head" mode="toc"/>

我尝试添加以下内容,但无济于事:

<xsl:apply-templates select="//tei:head/tei:note[@type = 'editorial']"
mode="toc"/>

任何帮助将不胜感激!

附言这是我第一次在 SE 上发帖,所以如果我错过了重要的细节,请告诉我,我会澄清的。谢谢。

【问题讨论】:

  • 您能否向我们展示您的完整 XSLT(如果它不是太大的话)以及示例输入和输出 XML?

标签: xml xslt


【解决方案1】:

在进行特定于 toc 的处理时,您通常需要传递模式:

<xsl:template match="tei:head" mode="toc" />
<xsl:template match="tei:head[../@type = 'chapter']" mode="toc">
  <h3><a href="#{../@xml:id}"><xsl:apply-templates mode="toc" /></a></h3>
</xsl:template>

注意xsl:apply-templates 上的mode 属性。如果你这样做了,那么你的 tei:note 模板应该被遵守。

如果您使用身份模板,这可能意味着您需要一个用于toc 模式的模板。

或者,如果您在到达tei:head 后真的不需要特定模式的处理,您可以这样做:

<xsl:template match="tei:head" mode="toc" />
<xsl:template match="tei:head[../@type = 'chapter']" mode="toc">
  <h3><a href="#{../@xml:id}">
    <xsl:apply-templates select="node()[not(self::tei:note)]" />
  </a></h3>
</xsl:template>

【讨论】:

  • 太好了,谢谢。一旦我将mode="toc" 添加到xsl:apply-templates,如您的示例所述,稍后为空的&lt;xsl:template match="tei:note" mode="toc"/&gt; 使 元素静音。太棒了!
猜你喜欢
  • 1970-01-01
  • 2017-12-07
  • 2016-07-18
  • 2017-02-09
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
相关资源
最近更新 更多