RE: [xsl] variable outside a for-each loop

Subject: RE: [xsl] variable outside a for-each loop
From: "Bjorndahl, Brad" <brad.bjorndahl@xxxxxxxxxxxxxxxx>
Date: Thu, 20 Sep 2007 10:26:01 -0400

Something more along the lines of the following will work. I assume you want
to carry the last non-empty para content to any empty attribute in the same
relative position. Your sample data is too meagre to be sure.

Learn XPath to get to nodes in your XML.

If you change your attribute names to something like "col1', 'col2', etc.,
this can be generalised much more easily.
I'm not sure that the string-join does much in this transform.

<?xml version='1.0'?>
<xsl:transform version="2.0" xmlns:xsl="";

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/informaltable" >
    <xsl:apply-templates />

<xsl:template match="row">
    <xsl:for-each select="entry" >
    <xsl:variable name="position" select="position()" />
    <xsl:variable name="attName"><xsl:value-of select="('one',
'two')[$position]" /></xsl:variable>

      <xsl:attribute name="{$attName}" >
          <xsl:when test="para eq ''" >
            <xsl:value-of select="../preceding-sibling::row/entry[position()
eq $position][1]/normalize-space(string-join(para,' '))" />
            <xsl:value-of select="normalize-space(string-join(para,' '))" />



-----Original Message-----
From: Mathieu Malaterre [mailto:mathieu.malaterre@xxxxxxxxx]
Sent: September 20, 2007 8:52 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] variable outside a for-each loop


  this is clearly a question that has been asked many times on this mailing
list. But I simply cannot find anything smart to do for my

I have the following XML (docbook):

      <para>DEF 2</para>

My goal is to produce:

    <entry one="ABC" two="DEF"/>
    <entry one="ABC" two="DEF 2"/>

In C/C++ this is trivial at each step whenever row/entry is empty I would use
some local variable instead. But in XSL I am stuck with:

                <xsl:for-each select="row">
                  <xsl:variable name="one"
select="normalize-space(string-join(entry[1]/para,' '))"/>
                  <xsl:variable name="two"
select="normalize-space(string-join(entry[2]/para,' '))"/>
                  <entry one="{$one}" two="{$two}"/>

Could someone please let me know what the correct solution is when using XSL

Thanks so much;


Current Thread