【问题标题】:Overriding match="*" template from DocBook XSL从 DocBook XSL 覆盖 match="*" 模板
【发布时间】:2009-10-12 18:46:17
【问题描述】:

DocBook XSL 包含一个匹配所有元素的模板

<xsl:template match="*">
  <xsl:message> ....  </xsl:message>
</xsl:template>

我需要用另一个模板覆盖它,因为我的源 XML 树包含的不仅仅是 DoocBook XML。如果我在文件中指定这样的模板,它将覆盖 DocBook XSL 中的所有模板。似乎所有导入的模板都仅按导入顺序排列优先级,而不是根据模板的具体程度。

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:db="http://docbook.org/ns/docbook" version="1.0">

  <xsl:import href="docbook-xsl-ns/xhtml/docbook.xsl" />
  <xsl:import href="copy.xsl"/>

  <xsl:template match="/">
    <xsl:apply-templates select="//db:book"/>
  </xsl:template>
</xsl:stylesheet>

复制.xsl

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform>

  <xsl:template match="*">
    <xsl:element name="{local-name()}">
        <!-- go process attributes and children -->
        <xsl:apply-templates select="@*|node()" />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

示例 XML 源代码

<?xml version="1.0" encoding="UTF-8"?>
<root>
<http-host>localhost</http-host>
<book xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"  xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:svg="http://www.w3.org/2000/svg" xmlns:m="http://www.w3.org/1998/Math/MathML" xml:id="course.528" xml:lang="en" version="5.0">
  <info>
   <title>Postoperative Complications</title>    
  </info>
  <chapter xml:id="chapter.1">
   <title>INTRODUCTION</title>
   <para>Postoperative complications are a constant threat to the millions  ....</para>
  </chapter>
</book>
<errors></errors>
</root>

对于 Xalan 和 xsltproc 处理器都是如此。如何在不更改 DocBook XSL 源的情况下覆盖此模板。我尝试弄乱优先级,但没有奏效。

【问题讨论】:

    标签: xslt docbook


    【解决方案1】:

    据我了解,您只想将 copy.xsl 的模板应用于非 docbook 元素。尝试在您的 copy.xsl 中更具体 - 通过在您的 copy.xsl 中更具体,该模板将被选择用于所有非 docbook 元素。

    复制.xsl

    <xsl:stylesheet version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform>
    
      <xsl:template match="*[not(namespace-uri() = 'http://docbook.org/ns/docbook')]">
        <xsl:element name="{local-name()}">
            <!-- go process attributes and children -->
            <xsl:apply-templates select="@*|node()" />
        </xsl:element>
      </xsl:template>
    
    </xsl:stylesheet>
    

    根据非 Docbook 节点中是否存在 DocBook 元素,您可能还需要限制您在 apply-templates 部分应用的节点集(基于命名空间),并且可能会弄乱 apply-templates 流程以确保它以可预测的方式处理它。希望这对你有用..

    【讨论】:

    • 这更像是一种解决方法,但我认为这对我来说是可行的;所有 DocBook 元素都在一起,其余的是来自本地命名空间的自定义元素。尽管如此,为什么在通用模板之前导入的更具体的模板没有得到应用。稍后导入的通用模板将全部覆盖。有没有办法覆盖这种行为?如果我要按优先级来做,有没有办法在导入时查看优先级是什么?
    猜你喜欢
    • 2017-11-06
    • 2014-01-25
    • 1970-01-01
    • 2019-03-27
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多