Re: [xsl] Define internal xsl variable

Subject: Re: [xsl] Define internal xsl variable
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Wed, 05 Oct 2011 09:49:43 +0100
Try sibling recursion:

<xsl:template match="t:cell">
<xsl:param name="start" as="xs:integer" select="1"/>
<t:data><xsl:value-of select="$start to ($start + ((@span, 1)[1]) - 1)"/></t:data>
<xsl:apply-templates select="following-sibling::t:cell[1]"/>
</xsl:template>


then

<xsl:apply-templates select="$origin/t:cell[1]"/>

To make this work the t:cell elements have to be siblings, so change the variable to:

    <xsl:variable name="origin" as="element(t:cells)">
      <t:cells>
        <t:cell>foo</t:cell>
       <t:cell span="2">bar</t:cell>
       <t:cell span="3">baz</t:cell>
       <t:cell>test</t:cell>
      </t:cells>
    </xsl:variable>



Not tested.

Michael Kay
Saxonica

On 05/10/2011 09:01, Susanne Wunsch wrote:
Hello,

I'm Susanne and did already some xsl transformations before. For now I try
to define an internal xsl variable but the xpath expression doesn't work
so far.

(The current xsl approach is shown below.)

The 'origin' variable should be mapped into the following node set (in the
'spannedCells' variable):

<t:data>1</t:data>
<t:data>2 3</t:data>
<t:data>4 5 6</t:data>
<t:data>7</t:data>

It shows occupied cells including the "spanned" ones. I think that some
recursive design should be possible but I just didn't find how to start in
this case.

I would be very glad about any hint.

Full xsl file follows:
-------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   xmlns:xs="http://www.w3.org/2001/XMLSchema";
   xmlns:t="http://www.example.org/test";
   version="2.0">

   <xsl:template name="main">
     <xsl:variable name="origin" as="node()*">
        <t:cell>foo</t:cell>
        <t:cell span="2">bar</t:cell>
        <t:cell span="3">baz</t:cell>
        <t:cell>test</t:cell>
     </xsl:variable>

     <xsl:variable name="SpannedCells" as="node()*">
        <t:data>1</t:data>
        <xsl:for-each select="$origin[position()>  1]">
          <xsl:variable name="PreviousValue"
            select="xs:integer(max(preceding::t:data/text()))"/>
          <xsl:variable name="MaxValue"
            select="if (./@span) then ($PreviousValue + 1 + @span)
            else ($PreviousValue + 1)"/>
          <t:data>
            <xsl:value-of select="($PreviousValue + 1) to $MaxValue"/>
          </t:data>
        </xsl:for-each>
      </xsl:variable>

      <xsl:message select="$SpannedCells"/>
   </xsl:template>
</xsl:stylesheet>
------------------------------------------------------------------------

Thanks and kind regards...
Susanne

Current Thread