Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing xsl:variable in a xsl:foreach select statement
    text
    copied!<p>I'm trying to iterate through an xml document using xsl:foreach but I need the select=" " to be dynamic so I'm using a variable as the source. Here's what I've tried:</p> <p>...</p> <pre><code>&lt;xsl:template name="SetDataPath"&gt; &lt;xsl:param name="Type" /&gt; &lt;xsl:variable name="Path_1"&gt;/Rating/Path1/*&lt;/xsl:variable&gt; &lt;xsl:variable name="Path_2"&gt;/Rating/Path2/*&lt;/xsl:variable&gt; &lt;xsl:if test="$Type='1'"&gt; &lt;xsl:value-of select="$Path_1"/&gt; &lt;/xsl:if&gt; &lt;xsl:if test="$Type='2'"&gt; &lt;xsl:value-of select="$Path_2"/&gt; &lt;/xsl:if&gt; &lt;xsl:template&gt; </code></pre> <p>...</p> <pre><code> &lt;!-- Set Data Path according to Type --&gt; &lt;xsl:variable name="DataPath"&gt; &lt;xsl:call-template name="SetDataPath"&gt; &lt;xsl:with-param name="Type" select="/Rating/Type" /&gt; &lt;/xsl:call-template&gt; &lt;/xsl:variable&gt; </code></pre> <p>...</p> <pre><code>&lt;xsl:for-each select="$DataPath"&gt; </code></pre> <p>...</p> <p>The foreach threw an error stating: "XslTransformException - To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function."</p> <p>When I use the msxsl:node-set() function though, my results are blank.</p> <p>I'm aware that I'm setting $DataPath to a string, but shouldn't the node-set() function be creating a node set from it? Am I missing something? When I don't use a variable:</p> <pre><code>&lt;xsl:for-each select="/Rating/Path1/*"&gt; </code></pre> <p>I get the proper results.</p> <p>Here's the XML data file I'm using:</p> <pre><code>&lt;Rating&gt; &lt;Type&gt;1&lt;/Type&gt; &lt;Path1&gt; &lt;sarah&gt; &lt;dob&gt;1-3-86&lt;/dob&gt; &lt;user&gt;Sarah&lt;/user&gt; &lt;/sarah&gt; &lt;joe&gt; &lt;dob&gt;11-12-85&lt;/dob&gt; &lt;user&gt;Joe&lt;/user&gt; &lt;/joe&gt; &lt;/Path1&gt; &lt;Path2&gt; &lt;jeff&gt; &lt;dob&gt;11-3-84&lt;/dob&gt; &lt;user&gt;Jeff&lt;/user&gt; &lt;/jeff&gt; &lt;shawn&gt; &lt;dob&gt;3-5-81&lt;/dob&gt; &lt;user&gt;Shawn&lt;/user&gt; &lt;/shawn&gt; &lt;/Path2&gt; &lt;/Rating&gt; </code></pre> <p>My question is simple, how do you run a foreach on 2 different paths?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload