[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] loop without switching context


Subject: [xsl] loop without switching context
From: Fred Christian <fchristian@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 22 Mar 2012 16:36:23 -0700

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


Current Thread
Keywords