Using a variable to tell xsl:for-each which nodes to select

Joined
Oct 4, 2008
Messages
1
Reaction score
0
Hello, I am trying to make a for-each loop that selects the nodes to run over using a variable.

So instead of:

<xsl:for-each select="item">

It's:

<xsl:for-each select="$node-test">

I've stripped what I have so far to the example below to illustrate what I've got:

XML:

Code:
<?xml version="1.0"?>
<test-root name="test document">
	<list group="big list">
		<item name="number one">
			<item-notes type="notes">blah.</item-notes>
		</item>
		<item name="number two">
			<item-tag name="test tag" />
			<item-notes type="notes">blah blah.</item-notes>
		</item>
		<item name="number three">
			<item-tag name="test tag" />
			<item-notes type="notes">blah blah blah.</item-notes>
		</item>
	</list>
</test-root>

And the XSL:

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="*">
    <xsl:for-each select="*[@group]">

      <xsl:variable name="node-test">
        <xsl:choose>
          <xsl:when test="1=1"><xsl:value-of select="*" /></xsl:when>
          <xsl:otherwise><xsl:value-of select="*[child::item-tag]" /></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:for-each select="$node-test">
        <h3><xsl:value-of select="@name" /></h3>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

(I'm not really using 1=1 as a test but this is just to show what I'm trying to do.)

I know that if I put either of the two values inside the choose elements (* or *[child::item-tag]) directly into the select in the for-each, then it works as intended.

I've also found that if I take out the choose elements entirely and set the variable like this:

<xsl:variable name="node-test" select="*" />

or:

<xsl:variable name="node-test" select="*[child::item-tag]" />

then the for-each will take the variable and work fine, but not if I set the variable inside choose elements.

Can anyone tell me what I'm doing wrong, and if this approach can work? Or, if not, suggest a different approach?

Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top