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

Subject: Re: [xsl] variable outside a for-each loop
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 20 Sep 2007 09:27:36 -0400
At 2007-09-20 14:52 +0200, Mathieu Malaterre wrote:
I have the following XML (docbook):
...
My goal is to produce:

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

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}"/>
                </xsl:for-each>

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

Solution or syntax?


An abbreviated syntax for what you have would be:

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

But whether or not this is a correct solution depends on whether or not the function calls you chose have met your requirements.

You were almost there when using the braces for the attribute value template ... but *any* expression can go in an attribute value template, not just a variable reference.

I hope this helps.

. . . . . . . . . . Ken


-- Upcoming public training: UBL and code lists Oct 1/5; Madrid Spain World-wide corporate, govt. & user group XML, XSL and UBL training RSS feeds: publicly-available developer resources and training G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Jul'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread