根节点斜线与根节点名称

竿

为什么斜杠会导致文件 io 错误(无法打开 xml 文件),而如果我专门在匹配中使用名称,它会起作用吗?它们不是同义词吗?

下面的代码片段:

  <xsl:template match="/"> <!-- In question, different results / vs root -->
    <xsl:apply-templates select="greeting"/>
  </xsl:template>

示例 xslt

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

  <xsl:output method="html"/>

  <xsl:template match="/"> <!-- In question, different results / vs root -->
    <xsl:apply-templates select="greeting"/>
  </xsl:template>

  <xsl:template match="greeting">
    <html>
      <body>
        <h1>
          <xsl:value-of select="."/>
        </h1>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

示例 xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="helloworld.xslt"?>
<root>
  <greeting>
    Hello, World!
  </greeting>
  <greeting>
    Hello, World Too!
  </greeting>
</root>
上帝

当您使用时,/您处于文档级别。

存在于文档级别的唯一元素是<root>元素。但是你是用select属性说的,专门把模板应用到名为greetings的元素上,但是这在文档层面并不存在,它存在于你的<root>元素中。

你有三个选择。

  1. 将其更改为 <xsl:template match="/root">
  2. 删除选择 <xsl:apply-templates />
  3. 将您的选择更改为 <xsl:apply-templates select="root/greeting"/>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章