xsl-list
[Top] [All Lists]

[xsl] Error: XPTY0020: Leading '/' cannot select the root node of the tree containing the context item: the context item is an atomic value

2011-01-10 05:35:42
Hi,

I try to retrieve elements from a <xsl:key> using a <xsl:for-each> loop
over atomic values but get the following error:

Error on line 12 of example-b.xsl:
  XPTY0020: Leading '/' cannot select the root node of the tree
containing the context item:
  the context item is an atomic value
Failed to compile stylesheet. 1 error detected.

Any idea how I can make this work?

Here everything in more detail:

I have the following source document:

--- example.xml ---
<?xml version="1.0" encoding="UTF-8"?>
<example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
  <foo id="a">one</foo>
  <foo id="b">two</foo>
  <foo id="c">three</foo>
  <bar ids="a,b,c"/>
</example>
---

Using a key, I can generate an index as follows:

--- example-a.xsl ---
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/xhtml";
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:xs="http://www.w3.org/2001/XMLSchema";>

  <xsl:key name="index" match="//*[@id]" use="./@id" />

  <xsl:template match="/">
    <body>
      a: <xsl:value-of select="key('index', 'a')" />
      b: <xsl:value-of select="key('index', 'b')" />
      c: <xsl:value-of select="key('index', 'c')" />
    </body>
  </xsl:template>

</xsl:stylesheet>
---

here it is:

---
$ saxon -s:example.xml -xsl:example-a.xsl

<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns="http://www.w3.org/1999/xhtml";>
      a: one
      b: two
      c: three
</body>
---

but when trying the same with a loop using the tokenized value of @ids
of element <bar ids="a,b,c"/> using the following transformations:

--- example-b.xsl ---
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
    xmlns="http://www.w3.org/1999/xhtml";
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:xs="http://www.w3.org/2001/XMLSchema";>

  <xsl:key name="index" match="//*[@id]" use="./@id" />

  <xsl:template match="/">
    <body>
      <xsl:for-each select="tokenize(example/bar/@ids, ',')">
        <xsl:value-of select="."/>: <xsl:value-of
select="key('index', .)" />
      </xsl:for-each>
    </body>
  </xsl:template>

</xsl:stylesheet>
---

I get the following error:

---
$ saxon -s:example.xml -xsl:example-b.xsl

Error on line 12 of example-b.xsl:
  XPTY0020: Leading '/' cannot select the root node of the tree
containing the context item:
  the context item is an atomic value
Failed to compile stylesheet. 1 error detected.
---

Any idea how to do this?

Thanks for your help!

Dietrich




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--