Re: [xsl] Iterate through variable in XSLT?

Subject: Re: [xsl] Iterate through variable in XSLT?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 20 Aug 2007 11:19:09 -0400
Hi Yun Wu,

At 04:07 AM 8/18/2007, you wrote:
basically i like to generic produce the following code rather then write it by myself

<xsl:template match="/">
<classes>
 <class>
   <xsl:attribute name="name">
   <xsl:text>classA</xsl:text>
 </class>
 <class>
   <xsl:attribute name="name">
   <xsl:text>classB</xsl:text>
 </class>
 <class>
   <xsl:attribute name="name">
   <xsl:text>classC</xsl:text>
 </class>
 <class>
   <xsl:attribute name="name">
   <xsl:text>classD</xsl:text>
 </class>
</classes>
</xsl:template>

It isn't exactly clear what you actually want. For one, your example isn't well-formed. I am going to assume you want:


<class>
  <xsl:attribute name="name">
    <xsl:text>classA</xsl:text>
  </xsl:attribute>
</class>

... that is, you want to attach a series of class elements each with an attribute named "name" and a value "classX".

Then the next question is will it be simply

<class name="classX"/>

? if so, this is just generating a series of elements matching a series of values.

Given a series of values X:

<x>
  <value>classA</value>
  <value>classB</value>
  ...
  </value>classX</value>
</x>

then generating the source is pretty easy stuff, as you can undoubtedly see: you simply select the set of nodes x/* and generate 'class' elements, populating their @name attributes accordingly:

(Having bound $values to x/value, depending on the context)

<xsl:for-each select="$values">
  <class name="{.}"/>
</xsl:for-each>

The only question remaining is how you want to make x/value available at runtime. There are many ways to do this; which is most convenient depends on your processing architecture.

So you probably need to pose a more specific question, correcting any mistaken assumptions in the above. My guess is you want to do something more complex, and/or you're actually asking about ways to make an arbitrary series of values available at runtime (i.e., how to bind a $values variable).

An external document called in with the document() function may be the simplest and most versatile way to do this.

Cheers,
Wendell


====================================================================== 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