0

(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]
Luke
  • 1
  • 1
  • No sure what exactly the question is. However, I am certain that `` is a *literal result element*, not an XSLT instruction. To process a set of nodes, you need to use ``. -- P.S. A browser is not a good testing environment. Also keep in mind that browsers support XSLT 1.0 only. – michael.hor257k Nov 17 '20 at 19:13
  • Thanks, that was exactly it! 'xsl:for-each' instead of 'for-each'. I patched up my original description to show the answer. Thanks for the note about browsers. Perhaps I have been under the impression that because I declare XSL 3.0 at the start i was using it. I don't clearly understand a lot about XSL outside the contents of my stylesheets and documents, and find most resources I find on the subject difficult to digest. As far as a testing environment, Firefox is a suitable testing environment for me because that's where the output is ultimately viewed. – Luke Nov 19 '20 at 01:43
  • A good testing environment is one that provides error messages. – michael.hor257k Nov 19 '20 at 04:36

0 Answers0