XSL: iterate over static values fails to behave as expected

123 Views Asked by At

(Edited and successfully resolved in response to a comment; TL;DR: forgot 'xsl' namespace of the for-each tag--truly embarrassing. SOmetimes you're only purpose in life is to serve as a warning for others, so I'm patching up my question with the answer)

trying to iterate over static values. best similar question i can find in searches is xslt-1.0 iterate over fixed list of values. but i just cannot get the same behaviour.

using either Firefox 82.0.3 (in conjunction with an nginx server) or Saxon 9.9.1.5 on my machine i get the same results. i've isolated the for-each code that isn't behaving as expected. the current document never seems to get set to the new temporary document the for-each's select indicates.

i checked back on the usage of for-each on a couple of sites, and i am missing something simple i'm sure. i'm just not seeing it. others can clearly get this working and for some reason i can't. i didn't think i explicitly needed to use node-set in XSL 3, though trying that didn't get any better results. i tried the XSL 1.0 method indicated in the linked Q&A and a few other adjacent techniques.

XML

<?xml-stylesheet type="text/xsl" href="for-each-test.xsl" ?>
<Character xmlns:math="http://exslt.org/math" xmlns:xi="http://www.w3.org/2001/XInclude">
    <abc>xyz</abc>
</Character>

XSL

<xsl:stylesheet version="3.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                                xmlns:exsl="http://exslt.org/common"
                                xmlns:math="http://exslt.org/math"
                                extension-element-prefixes="exsl"  >
<xsl:output method="html" version="4.01" indent="yes"/>
<xsl:output doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3C//DTD HTML 4.01//EN"/>

    <xsl:variable name='var'>
            <type>enhancement</type>
            <type>inherent</type>
            <type>enlargement</type>
    </xsl:variable>

    <xsl:template match='/'>
        <html><head></head><body>   
        
            <!-- this was my original technique, which omitted 'xsl:'
            <for-each select="document('')//xsl:variable[@name='var']/*">
                name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
            </for-each>
            -->

            <!-- these techniques work -->
            <xsl:for-each select="document('')//xsl:variable[@name='var']/*">
                name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
            </xsl:for-each>

            <xsl:for-each select='exsl:node-set($var)//type'>
                name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
            </xsl:for-each>

            <!-- other incorrect techniques i tried;
            they don't work even with tags corrected; 
            didn't really expect these to work; 
            was grasping at straws with these, really -->
            <!--
            <xsl:for-each select='exsl:node-set($var)'>
                name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
            </xsl:for-each>

            <xsl:for-each select='$var//type'>
                name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
            </xsl:for-each>

            <xsl:for-each select='$var/*'>
                name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
            </xsl:for-each>

            <xsl:for-each select='$var'>
                name[<xsl:value-of select='name(.)'/>] = [<xsl:value-of select='.'/>]<br/>
            </xsl:for-each>
            -->

        </body></html>
    </xsl:template>
</xsl:stylesheet>

Sample Output of For-Each Loop Before Fix

name[] = [ xyz ]

Desired Output and Sample Output of For-Each Loop After Fix

name[type] = [enhancement]
name[type] = [inherent]
name[type] = [enlargement]
0

There are 0 best solutions below