Rod
Rod

Reputation: 15455

root node slash vs root node name

Why would the slash cause a file io error (cannot open xml file) vs if I specifically use name in match it works? Aren't they synonymous?

snippet of code below:

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

Sample 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>

Sample 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>

Upvotes: 0

Views: 207

Answers (1)

NMGod
NMGod

Reputation: 2027

When you use / you are at the document level.

The only element that exists on the document level is the <root> element. But you are using the select attribute to say, specifically apply templates to the element named greetings, but this does not exist on the document level, it exists in your <root> element.

You have three options.

  1. Change it to <xsl:template match="/root">
  2. Remove the select <xsl:apply-templates />
  3. Change your select to <xsl:apply-templates select="root/greeting"/>

Upvotes: 2

Related Questions