xsl-list
[Top] [All Lists]

[xsl] loop without switching context

2012-03-22 18:36:56
I ran into a challenge today that I ended up working around.
I wonder though if there is a direct solution.

Is there a way to write a loop without switching context?
I am using XSLT 2.0 and Saxon.

If I loop through a set of values using <xsl:for-each> then my context
is set to the xml that contains that data. But I want my context to be
set to the xml before I entered the for-each.

Some example code.
I had this.
<xsl:call-template name="a"><xsl:with-param
name="x">01</xsl:with-param></xsl:call-templates>
<xsl:call-template name="a"><xsl:with-param
name="x">02</xsl:with-param></xsl:call-templates>
{duplicated about 10 times}

It works okay, but it is hard-coded (bad).
I will have cases where my list of x values will vary, but they will
always be short strings like {01,02,A1,B3}. And the number of values
will always be less than 20.

So I thought of using xsl:for-each on a data file for the x values.
Like this
<xsl:variable name="datafile">../data.xml</xsl:variable>
<xsl:for-each select="document($datafile)/data/xvals/x">
  <xsl:call-template name="a"><xsl:with-param
name="x">01</xsl:with-param></xsl:call-templates>
</xsl:for-each>

The loop works, but it switches my context so that template "a" no
longer has the xml it needs.
I realized I could pass in the context node as a param, but it seems
that is only giving me access to children and not ancestors of the
context node.

Maybe I have hit a case where I am just modeling the project wrong and
need to rethink it some.
Or, is there a way to write a loop without switching context?

Thanks,
Fred

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


<Prev in Thread] Current Thread [Next in Thread>