根节点斜杠vs根节点名称

问题描述:

为什么斜线会导致文件io错误(无法打开xml文件)vs如果我特别使用名称匹配它的作品?他们不是同义词吗?根节点斜杠vs根节点名称

下面的代码片段:

<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> 
+2

了'root'元素是'/' – Pawel

+0

可以请你提供多一点的孩子洞察力?为什么我在使用斜杠时会抛出异常? – Rod

当您使用/你在documen t水平。

文档级别上存在的唯一元素是<root>元素。但是您使用select属性来说,特别是将模板应用于名为greetings的元素,但这在文档级别上不存在,它存在于您的<root>元素中。

您有三种选择。

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