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

Re: [xsl] apply-templates repeats inside xsl:for-each, why


Subject: Re: [xsl] apply-templates repeats inside xsl:for-each, why
From: "Mark Giffin m1879@xxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 16 Jun 2014 01:22:01 -0000

I think I've narrowed it down a bit. This code calls no templates:

<xsl:variable name="num" select="count($platforms/platform)"/>

        <xsl:for-each select="1 to $num">
          <xsl:value-of select="$platforms/platform[position()]/@id"/>
          <xsl:text>&#10;</xsl:text>
        </xsl:for-each>

This outputs the following:
unique_2 unique_35 unique_37
unique_2 unique_35 unique_37
unique_2 unique_35 unique_37

Why does it repeat this 3 times instead of just once? If I remove one of the nodes in $platforms so there are only 2 nodes, then I get this:
unique_2 unique_35
unique_2 unique_35


By the way, the @id's are repeated in the same file, but they are in separate namespaces. It's not a structure of my making, it's a temp file produced by the DITA Open Toolkit, if you happen to be familiar with that.


On 6/15/14 6:03 PM, Syd Bauman s.bauman@xxxxxxx wrote:
Well, without seeing at least the code that matches <concept> and
the portion of the input that has <concept>, it's hard to impossible
to say. But my first guess would be that the code that matches
<concept id="unique_2"> generates one line of output for each
<platform>.

But a few odd things jump to mind:
* It would seem there is both a <platform> and a <concept> with
id=unique_2. While not necessarily invalid, most of us think of
the id= attribute as an ID attribute, and thus necessarily
unique. But even if your schema (unwisely, I daresay) permits
values of id= to be non-unique, it seems terribly misleading to
say in the value it self that it is unique, when it's not.


  * The predicate "[@id = @id]" will never return false, and thus
    seems a bit silly. (Perhaps you meant "[@id = $id]"? I've made
    that mistake :-)

I'm running xsl:for-each on a variable $platforms that holds this:

        <platform name="windows_xp" id="unique_2"/>
        <platform name="windows_vista" id="unique_35"/>
        <platform name="windows_7" id="unique_37"/>

The following code produces the same output 3 times for each node in
$platforms:

          <xsl:for-each select="$platforms/platform">
            <xsl:apply-templates select="$root-of-input-file/map/concept/concept[@id = @id]"/>
          </xsl:for-each>

So in the output I get this kind of thing:

Windows XP
Windows Vista
Windows 7
Windows XP
Windows Vista
Windows 7
Windows XP
Windows Vista
Windows 7

I was expecting to get one output for each element in
$platforms. If I use the following outside of xsl:for-each, I get
one output:

<xsl:apply-templates select="$root/map/concept/concept[@id = 'unique_2']"/>

What am I missing?


Current Thread
Keywords