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

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

Subject: [xsl] banging my head against the wall (key, actually)
From: "Syd Bauman s.bauman@xxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 24 Apr 2015 01:30:26 -0000

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?

Note that the @alpha bit is, at the moment, entirely unused.

XSLT and input
---- --- -----
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  exclude-result-prefixes="xs xd"
  <xd:doc scope="stylesheet">
      <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>
  <xsl:output method="text"/>
  <xsl:key name="tst" match="xd:desc/xd:p" use="."/>
  <xsl:template match="/">
    <xsl:variable name="set">
      <xsl:apply-templates select="//xd:p"/>
    <!-- method 1: -->
    <xsl:for-each select="key('tst', $set/duck/string(.) )">
      <xsl:message>debug1: <xsl:value-of select="@n"/></xsl:message>
    <!-- method 2: -->
    <xsl:for-each select="$set/duck">
      <xsl:message>debug2: <xsl:value-of select="key('tst', string(.) )/@n"/></xsl:message>
  <xsl:template match="xd:p">
    <duck alpha="{substring('abcd',xs:integer(@n),1)}"><xsl:value-of select="."/></duck>

expected output
-------- ------
debug1: 1
debug1: 2
debug1: 3
debug1: 4
debug2: 1
debug2: 2
debug2: 3
debug2: 4

actual output
------ ------
debug1: 1
debug1: 2
debug1: 3
debug1: 4

"Saxon-HE from Saxonica" running in "GNU bash, version
4.2.25(1)-release" on an Ubuntu system (12.04, I think).[1]

[1] Linux albus 3.5.0-18-generic #29-Ubuntu SMP Thu Oct 25 07:26:14
    UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

 Syd Bauman, EMT-Paramedic
 Senior XML Programmer/Analyst
 Northeastern University Women Writers Project
 s.bauman@xxxxxxx or

Current Thread