声明为DTD的DOCTYPE如何影响XSLT转换?

时间:2015-06-16 14:20:35

标签: xml xslt doctype

我正在使用XSLT 1和2以及各种处理器转换TEI编码的文本文档(TEI - 文本编码计划,文本文档编码中的标准)。

我遇到了一个非常特殊的问题。根据我在XML文件头中提供的DTD,我会得到不同的结果。输入文件示例:     

<!DOCTYPE TEI SYSTEM "tei_lite.dtd">
<TEI>
  <teiHeader>  
    <fileDesc>
      <titleStmt>
        <title>Przyjaciel szczery</title>
        <author>Jan Daniecki</author>
        <respStmt>
          <resp>wyd.</resp> 
          <name>Maciej Eder</name>
        </respStmt>
      </titleStmt>
    </fileDesc>
   </teiHeader>
</TEI>

以下xslt应该删除作者节点:

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="author"/>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

但是,如果我用空的替换dtd(这里发布的时间太长),样式表只会删除该节点。

我找到了原因:这是因为使用DTD引入了命名空间。为了使样式表起作用,事实证明,我需要声明命名空间 - 然后一切正常,请参阅

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:tei="http://www.tei-c.org/ns/1.0" version="2.0">
  <xsl:template match="tei:author"/>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

我认为在XSLT转换期间只有默认属性节点受DTD定义的影响。有人可以总结一下DTD如何以及何时添加需要考虑的名称空间?

谢谢!

1 个答案:

答案 0 :(得分:3)

  

我认为在XSLT转换期间只有默认属性节点受DTD定义的影响。

嗯,它也会影响实体,但默认属性节点正是这里发生的事情。

  

有人可以总结一下DTD如何以及何时添加需要考虑的名称空间?

假设您使用的DTD类似于this,请注意:

<!ATTLIST TEI xmlns CDATA "http://www.tei-c.org/ns/1.0">

因此,DTD表示<TEI>元素具有xmlns http://www.tei-c.org/ns/1.0的默认属性值。除非元素在源中具有明确的xmlns属性,否则应将其视为具有xlmns="http://www.tei-c.org/ns/1.0"

现在,对于像XSLT转换这样的名称空间感知XML过程,名称空间声明和属性之间存在差异,而对于非命名空间感知的XML过程(如DTD验证),名称空间声明就像其他属性一样。 (这就是如何以向后兼容的方式将命名空间添加到XML中。)

事实上,大多数(所有?)元素都是以这种方式定义的,所以在DTD验证后你的文档:

<TEI>
  <teiHeader>  
    <fileDesc>
      <titleStmt>
        <title>Przyjaciel szczery</title>
        <author>Jan Daniecki</author>
        <respStmt>
          <resp>wyd.</resp> 
          <name>Maciej Eder</name>
        </respStmt>
      </titleStmt>
    </fileDesc>
   </teiHeader>
</TEI>

变为:

<TEI xmlns="http://www.tei-c.org/ns/1.0">
  <teiHeader xmlns="http://www.tei-c.org/ns/1.0">
    <fileDesc xmlns="http://www.tei-c.org/ns/1.0">
      <titleStmt xmlns="http://www.tei-c.org/ns/1.0">
        <title xmlns="http://www.tei-c.org/ns/1.0">Przyjaciel szczery</title>
        <author xmlns="http://www.tei-c.org/ns/1.0">Jan Daniecki</author>
        <respStmt xmlns="http://www.tei-c.org/ns/1.0">
          <resp xmlns="http://www.tei-c.org/ns/1.0">wyd.</resp> 
          <name xmlns="http://www.tei-c.org/ns/1.0">Maciej Eder</name>
        </respStmt>
      </titleStmt>
    </fileDesc>
   </teiHeader>
</TEI>

然后处理XSLT转换。