0

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 源的情况下覆盖此模板。我尝试弄乱优先级,但没有奏效。

4

1 回答 1

1

据我了解,您只想将 copy.xsl 的模板应用于非文档元素。尝试在您的 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 流程以确保它可以预见地处理它。希望这对你有点用..

于 2009-10-14T11:32:56.727 回答