[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] banging my head against the wall (key, actually)


Subject: Re: [xsl] banging my head against the wall (key, actually)
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 24 Apr 2015 02:29:59 -0000

(slight rewording noted below)

At 2015-04-24 02:26 +0000, I wrote:
At 2015-04-24 01:30 +0000, Syd Bauman s.bauman@xxxxxxx wrote:
I fully expected method 1 and method 2 in the following stylesheet
(which reads itself as input) to produce the same result.

Method 1 is to access nodes from a key all at once by handing key()
a single sequence of four key-values.

Method 2 is to access nodes from a key one at a time by handing key()
a single key-value four times.

Obviously I'm missing something, but I'll be darned if I can figure
out what. I have, of course, tried a bunch of variations (like using
"(string())" instead of "string(.)" as the 2nd arg to 2nd key()), but
to no avail. Any thoughts?

The problem is context.


In your first loop, the item being examined is a string and so has no node context and so the current node context is used and so the key table is found in the tree of the input document since the current node is in the input document.

In your second loop, the item being examined is a node in the tree created for the variable $set

Sorry ... I should have said that the item being examined is again a string, but the current node is in the tree for the variable $set ...


and so sets the context for the key table accordingly, but there are no <xd:p> elements in the tree of the context of the $set variable and so nothing is returned.

When I use a third argument in your second loop in order to explicitly set the context to the input document, the values are returned as you would expect:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
  exclude-result-prefixes="xs xd"
  version="2.0">

  <xd:doc scope="stylesheet">
    <xd:desc>
      <xd:p n="1">one</xd:p>
      <xd:p n="2">two dos</xd:p>
      <xd:p n="3">three tres III</xd:p>
      <xd:p n="4">f o u r</xd:p>
    </xd:desc>
  </xd:doc>

  <xsl:output method="text"/>
  <xsl:key name="tst" match="xd:desc/xd:p" use="."/>
  <xsl:variable name="home" select="/"/>

<xsl:template match="/">
<xsl:variable name="set">
<xsl:apply-templates select="//xd:p"/>
</xsl:variable>
<!-- method 1: -->
<xsl:for-each select="key('tst', $set/duck/string(.) )">
<xsl:message>debug1: <xsl:value-of select="@n"/></xsl:message>
</xsl:for-each>
<!-- method 2: -->
<xsl:for-each select="$set/duck">
<xsl:message>debug2: <xsl:value-of select="key('tst', string(.), $home )/@n"/></xsl:message>
</xsl:for-each>
</xsl:template>


<xsl:template match="xd:p">
<duck alpha="{substring('abcd',xs:integer(@n),1)}"><xsl:value-of select="."/></duck>
</xsl:template>


</xsl:stylesheet>

T:\>xslt2 syd.xsl syd.xsl
debug1: 1
debug1: 2
debug1: 3
debug1: 4
debug2: 1
debug2: 2
debug2: 3
debug2: 4

T:\>

I hope this helps.

. . . . . . . Ken


--
Check our site for free XML, XSLT, XSL-FO and UBL developer resources |
Free 5-hour lecture:  http://www.CraneSoftwrights.com/links/video.htm |
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/ |
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx |
Google+ profile:       http://plus.google.com/+GKenHolman-Crane/about |
Legal business disclaimers:     http://www.CraneSoftwrights.com/legal |


--- This email has been checked for viruses by Avast antivirus software. http://www.avast.com


Current Thread
Keywords