从DocBook XSL覆盖match =“*”模板

时间:2009-10-12 18:46:17

标签: xslt docbook

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>

copy.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源的情况下覆盖此模板。我试图弄乱优先事项但是没有用。

1 个答案:

答案 0 :(得分:1)

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

copy.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部分应用的节点集(基于命名空间),并且可能会混乱应用模板流程确保它可预测地处理它。希望这对你有用..