Re: [xsl] compact code using loop

Subject: Re: [xsl] compact code using loop
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 19 Mar 2008 12:52:58 -0400
Hi,

In addition to what Andrew suggests, an external document might be useful. Move the elements you need to process out of the XSLT and into a separate document, which you call with the document() function.

Or, document('') will call and parse the source file of the XSLT itself.

Or, use the exslt:node-set() function to convert a variable given as a RTF in the stylesheet into a node set, and process that.

Or, as Andrew says, use XSLT 2.0.

The best solution depends not only on the various architectural considerations that recommend them, but also on how regular the repetition is. If it can be generated entirely algorithmically, you can use recursion as Andrew suggests. If it has to consume some input (that is, if there's "information" there), you need to decide where to put it. Often, an external (subordinate) input file is better than the stylesheet, especially if users ever need to configure it.

Cheers,
Wendell

At 05:01 AM 3/19/2008, you wrote:
On 19/03/2008, Mansour <mansour77@xxxxxxxxx> wrote:
> I have repetitive code in my xslt. This code produces a list and does
>  not depend on the input xml. I am wondering if there is a way to
>  minimize this code. I can do it through for-each, but the element are in
>  the xslt file and not in the input xml, so I can not use the 'select='.
>  For example, I need to generate this code regardless of the input xml:
>
>  <item>
>     <type>type1</type>
>     <value>val1</value>
>  </item>
>  <item>
>     <type>type2</type>
>     <value>val2</value>
>  </item>
>  <item>
>     <type>type3</type>
>     <value>val3</value>
>  </item>
>
>
>  Of course I can use this directly in my xsl file, but there's more item.
>  So the code is too long. And there are more tags for the item.

If 2.0 you can do:

<xsl:for-each select="1 to 3">
  <item>
    <type>type<xsl:value-of select="."/>
     ...


In 1.0 you really need a recursive template:


<xsl:call-template name="generate">
  <xsl:with-param name="count" select="3"/>
</

<xsl:template name="generate">
  <xsl:param name="count"/>
  <xsl:if test="$count > 0">
    <item>
      <type>type<xsl:value-of select="$count"/>
      </
    </
    <xsl:call-template name="generate">
      <xsl:with-param name="count" select="$count - 1"/>
    </
  </xsl:if>
</xsl:template>


cheers -- Andrew Welch http://andrewjwelch.com Kernow: http://kernowforsaxon.sf.net/


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread