XSLT:我的转换添加了一个未选中的元素。我错过了什么?

时间:2012-10-29 13:32:01

标签: xslt

好的,我正在从这里开始学习一些简单的教程:

http://www.cch.kcl.ac.uk/legacy/teaching/7aavdh06/xslt/html/module_06.html

第一个练习涉及创建产生特定输出的转换。不幸的是,虽然我很接近,但我在一开始就得到了一个不需要的元素。即。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0">

  <xsl:output method="xhtml"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />

  <xsl:template match="/div/placeName">
    <html>
      <head />
      <body>
        <Table>
          <tr>
            <td>Place Name</td>
            <td>
              <xsl:value-of select="name" />
            </td>
          </tr>
          <tr>
            <td>Place Name (regularised)</td>
            <td>
              <xsl:value-of select="@reg" />
            </td>
          </tr>
          <tr>
            <td>National Grid Reference</td>
            <td>
              <xsl:value-of select="@key" />
            </td>
          </tr>
          <tr>
            <td>Type of building/monument</td>
            <td>
              <xsl:value-of select="settlement/@type" />
            </td>
          </tr>
        </Table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

但我得到的输出是:

Location
Place Name  Old Warden
Place Name (regularised)    Old Warden, St Leonard
National Grid Reference TL 137 443
Type of building/monument   Parish church

剩下的很好,但'位置'是不需要的。源XML位于上面的链接中。知道如何阻止不需要的文字出现吗?或者,更好的是,告诉我哪里出错了! :)

编辑:这是输出

<?xml version="1.0" encoding="utf-8" ?>
Location
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
    <table>
        <tr>
            <td>Place Name</td>
            <td>Old Warden</td>
        </tr>
        <tr>
            <td>Place Name (regularised)</td>
            <td>Old Warden,  St Leonard</td>
        </tr>
        <tr>
            <td>National Grid Reference</td>
            <td>TL 137 443</td>
        </tr>
        <tr>
            <td>Type of building/monument</td>
            <td>Parish church</td>
        </tr>
    </table>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

正如Stivel所提到的,“位置”文本确实来自XML中的 head 元素。

<div type="location">
    <head n="I">Location</head>
    <placeName reg="Old Warden,  St Leonard" key="TL 137 443">

它出现的原因是因为XSTL的内置模板在你没有为它在XSLT中寻找的元素指定匹配时使用。

您可以在W3C page上阅读内置模板,但简而言之,如果XSLT找不到匹配项,它将继续处理元素的子项(不复制元素),或者在案例中文本或属性,输出值。

XSLT将首先查找文档元素的匹配项,如果您还没有提供模板,它将继续为根元素,然后是子元素等寻找模板。

在您的情况下,在 / div / placeName 之前,您尚未提供匹配任何内容的模板,这意味着XSLT将使用 div 元素的内置模板。这有两个孩子; placeName 。您有一个模板可用于 placeName ,但不能 head ,因此内置模板最终会输出 head 的文本,因为您没有说出任何其他的话。

解决方案是简单地添加模板以忽略 head 元素

<xsl:template match="/div/head" />

这是完整的XSLT

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

  <xsl:output method="xhtml"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" indent="yes" />

  <xsl:template match="/div/head" />

  <xsl:template match="/div/placeName">
    <html>
      <head />
      <body>
        <Table>
          <tr>
            <td>Place Name</td>
            <td>
              <xsl:value-of select="name" />
            </td>
          </tr>
          <tr>
            <td>Place Name (regularised)</td>
            <td>
              <xsl:value-of select="@reg" />
            </td>
          </tr>
          <tr>
            <td>National Grid Reference</td>
            <td>
              <xsl:value-of select="@key" />
            </td>
          </tr>
          <tr>
            <td>Type of building/monument</td>
            <td>
              <xsl:value-of select="settlement/@type" />
            </td>
          </tr>
        </Table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

当你使用它时,这应该提供你需要的输出。

答案 1 :(得分:0)

您的<head/>可能会提及

<head n="I">Location</head>

删除xsl中的<head/>并检查。

答案 2 :(得分:0)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xhtml" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="div">
<xsl:apply-templates select="placeName"/>
</xsl:template>
  <xsl:template match="placeName">
    <html>
      <head />
      <body>
        <Table>
          <tr>
            <td>Place Name</td>
            <td>
              <xsl:value-of select="name" />
            </td>
          </tr>
          <tr>
            <td>Place Name (regularised)</td>
            <td>
              <xsl:value-of select="@reg" />
            </td>
          </tr>
          <tr>
            <td>National Grid Reference</td>
            <td>
              <xsl:value-of select="@key" />
            </td>
          </tr>
          <tr>
            <td>Type of building/monument</td>
            <td>
              <xsl:value-of select="settlement/@type" />
            </td>
          </tr>
        </Table>
      </body>
    </html>
  </xsl:template>