XSLT 默认模板不会让一个元素消失

XSLT default template won't let one element disappear

我很困惑为什么 resource 标签仍​​然在 <body> 中,因为我认为 ltx:resource 的默认模板应该让它出现。

我有 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<?latexml RelaxNGSchema="LaTeXML"?>
<document xmlns="http://dlmf.nist.gov/LaTeXML" xmlns:stex="http://kwarc.info/ns/sTeX" xml:id="Document">
<resource src="LaTeXML.css" type="text/css"/>
<resource src="ltx-article.css" type="text/css"/>
<para class="theory" xml:id="p1" fragid="p1">
  <block class="definition" xml:id="foo" fragid="foo">
   <p xml:id="foo.p1" about="#foo.p1" stex:srcref="/Users/yuancalvin/lmh/localmh/ext/sTeX/htmltest/test.tex#textrange(from=9;0,to=11;16)" fragid="foo.p1"> 
  testing 
   </p>
  </block>
</para>
</document>

XSL中负责构造的部分如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version     = "1.0"
    xmlns:xsl   = "http://www.w3.org/1999/XSL/Transform"
    xmlns:ltx   = "http://dlmf.nist.gov/LaTeXML"
    xmlns:string= "http://exslt.org/strings"
    xmlns:f     = "http://dlmf.nist.gov/LaTeXML/functions"
    exclude-result-prefixes = "ltx f"
    extension-element-prefixes="string f">

 <xsl:template match="/" mode="head-resources">
    <xsl:apply-templates select="//ltx:resource[@type='text/css']
                                 | //ltx:resource[@type='text/javascript']" mode="inhead"/>
  </xsl:template>

  <!-- By default, these disappear -->
  <xsl:template match="ltx:resource"/>

  <xsl:template match="ltx:resource[@type='text/css' and @src]" mode="inhead">
    <xsl:text>&#x0A;</xsl:text>
    <xsl:element name="link" namespace="{$html_ns}">
      <xsl:attribute name="rel">stylesheet</xsl:attribute>
      <xsl:attribute name="href"><xsl:value-of select="f:url(@src)"/></xsl:attribute>
      <xsl:attribute name="type"><xsl:value-of select="@type"/></xsl:attribute>
      <xsl:if test="@media">
        <xsl:attribute name="media"><xsl:value-of select="@media"/></xsl:attribute>
      </xsl:if>
    </xsl:element>
  </xsl:template>


    <xsl:template match="/" mode="body-main-begin"/>
  <xsl:template match="/" mode="body-main-end"/>

  <xsl:template match="/" mode="body-content">
    <xsl:text>&#x0A;</xsl:text>
    <xsl:element name="div" namespace="{$html_ns}">
      <xsl:attribute name="class">ltx_page_content</xsl:attribute>
      <xsl:apply-templates select="." mode="body-content-begin"/>
      <xsl:apply-templates/>
      <xsl:apply-templates select="." mode="body-content-end"/>
      <xsl:text>&#x0A;</xsl:text>
    </xsl:element>
  </xsl:template>

  <xsl:template match="/" mode="body-content-begin"/>
  <xsl:template match="/" mode="body-content-end"/>
</xsl:stylesheet>

样式表仍然不完整,因为实际的样式表相当长,但应该足以进行错误检查。

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title/>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"/>
<link rel="stylesheet" href="LaTeXML.css" type="text/css"/>
<link rel="stylesheet" href="ltx-article.css" type="text/css"/>
</head>
<body>
<div class="ltx_page_main">
<div class="ltx_page_content">
  <resource xmlns="http://dlmf.nist.gov/LaTeXML" src="LaTeXML.css" type="text/css"/>
  <resource xmlns="http://dlmf.nist.gov/LaTeXML" src="ltx-article.css" type="text/css"/>
  <para xmlns="http://dlmf.nist.gov/LaTeXML" class="theory" xml:id="p1">

    <block class="definition" xml:id="foo">
      <p xmlns:stex="http://kwarc.info/ns/sTeX" xml:id="foo.p1" about="#foo.p1" stex:srcref="/Users/yuancalvin/lmh/localmh/ext/sTeX/htmltest/test.tex#textrange(from=9;0,to=11;16)">
testing </p>
    </block>
  </para>


</div>
<div class="ltx_page_footer">
<div class="ltx_page_logo">Generated  by <a href="http://dlmf.nist.gov/LaTeXML/">LaTeXML <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAOCAYAAAD5YeaVAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wKExQZLWTEaOUAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAdpJREFUKM9tkL+L2nAARz9fPZNCKFapUn8kyI0e4iRHSR1Kb8ng0lJw6FYHFwv2LwhOpcWxTjeUunYqOmqd6hEoRDhtDWdA8ApRYsSUCDHNt5ul13vz4w0vWCgUnnEc975arX6ORqN3VqtVZbfbTQC4uEHANM3jSqXymFI6yWazP2KxWAXAL9zCUa1Wy2tXVxheKA9YNoR8Pt+aTqe4FVVVvz05O6MBhqUIBGk8Hn8HAOVy+T+XLJfLS4ZhTiRJgqIoVBRFIoric47jPnmeB1mW/9rr9ZpSSn3Lsmir1fJZlqWlUonKsvwWwD8ymc/nXwVBeLjf7xEKhdBut9Hr9WgmkyGEkJwsy5eHG5vN5g0AKIoCAEgkEkin0wQAfN9/cXPdheu6P33fBwB4ngcAcByHJpPJl+fn54mD3Gg0NrquXxeLRQAAwzAYj8cwTZPwPH9/sVg8PXweDAauqqr2cDjEer1GJBLBZDJBs9mE4zjwfZ85lAGg2+06hmGgXq+j3+/DsixYlgVN03a9Xu8jgCNCyIegIAgx13Vfd7vdu+FweG8YRkjXdWy329+dTgeSJD3ieZ7RNO0VAXAPwDEAO5VKndi2fWrb9jWl9Esul6PZbDY9Go1OZ7PZ9z/lyuD3OozU2wAAAABJRU5ErkJggg==" alt="[LOGO]"/></a></div></div>
</div>
</body>
</html>

我真的不知道为什么资源标签仍然存在于正文中。任何帮助将不胜感激:)

你没有显示复制节点的部分,所以按照目前的写法,如果匹配失败,输出将是该节点的文本值。

您的代码是这样的:

<!-- By default, these disappear -->
<xsl:template match="ltx:resource"/>

您显示的部分在 inhead 模式下显示 xsl:apply-templates,但 "delete template" 在该模式下不存在。因此它永远不会被匹配。

您需要在您希望应用它的每个模式中添加删除模板。如果您使用的是 XSLT 2.0(目前它被标记为 XSLT 1.0),您可以使用 <xsl:template match="X" mode="#all" /> 语法,但不幸的是,在 XSLT 1.0 中,您必须分别为每种模式指定它。

规格says it so:

There is also a built-in template rule for each mode, which allows recursive processing to continue in the same mode in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule for mode m.

<xsl:template match="*|/" mode="m">
  <xsl:apply-templates mode="m"/>
</xsl:template>