Re: [xsl] repetition using for-each (and generating "attributes")

Subject: Re: [xsl] repetition using for-each (and generating "attributes")
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 23 Jul 2001 17:43:31 +0100
Hi Kris,

> OK! Thanks to everyone - I modified your suggestion and it worked.
> This is what I used:
>
> <RING>
>         <xsl:for-each select="//Polygon/Point">
>                 <POINT>
>                 <xsl:attribute name="x">
>                         <xsl:value-of select="@x"/>
>                 </xsl:attribute>
>                 <xsl:attribute name="y">
>                         <xsl:value-of select="@y"/>
>                 </xsl:attribute>
>                </POINT>
>         </xsl:for-each>
>     </RING>

>From the looks of your source XML as quoted, you have only one Polygon
element within it, which is the document element. In that case,
there's no need to use // at the beginning of the path - using // will
make the XSLT processor search all the way through the entire XML
document to find Polygon elements, while you know exactly where it is.
So you could simplify to:

<RING>
  <xsl:for-each select="/Polygon/Point">
    <POINT>
      <xsl:attribute name="x">
        <xsl:value-of select="@x"/>
      </xsl:attribute>
      <xsl:attribute name="y">
        <xsl:value-of select="@y"/>
      </xsl:attribute>
    </POINT>
  </xsl:for-each>
</RING>

The other simplification that you could make is to either use
attribute value templates to create the attributes:

<RING>
  <xsl:for-each select="/Polygon/Point">
    <POINT x="{@x}" y="{@y}" />
  </xsl:for-each>
</RING>

Or, since you're keeping the same names and values for the attributes,
you could simply copy them with:

<RING>
  <xsl:for-each select="/Polygon/Point">
    <POINT>
      <xsl:copy-of select="@x | @y" />
    </POINT>
  </xsl:for-each>
</RING>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread