pennypacker 0 Newbie Poster

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="$nodes">

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

XML:

<?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:

<?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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.