Re: [xsl] select only some elements and attributes

Subject: Re: [xsl] select only some elements and attributes
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 15 Feb 2001 09:18:18 +0000
Hi Meili,

> Hi, What's the best way to only select some elements and attributes
> using xslt?

Evan's shown you one way (a push method), which involves selecting all
the elements and attributes, copying most of them and not copying the
ones that you don't want. That's the easiest way to do it if you want
to copy the majority of the nodes, and just want to leave out a few
(or if the nodes that you want to leave out are easily identified).

If you want to only keep a smaller proportion of the nodes, then it's
best to select the ones that you *do* want to keep and just copy those
(a pull method). In your case, this alternative gives something like:

<xsl:template match="product">
   <xsl:copy>
      <xsl:copy-of select="@sku" />
      <xsl:copy-of select="title" />
      <xsl:copy-of select="price" />
      <xsl:copy-of select="weight" />
      <xsl:apply-templates select="vendor" />
   </xsl:copy>
</xsl:template>

<xsl:template match="vendor">
   <xsl:copy>
      <xsl:copy-of select="company" />
      <xsl:copy-of select="phone" />
   </xsl:copy>
</xsl:template>

If the ordering in the source and the result is the same, then you can
combine the xsl:copy-of instructions together to make it shorter:

<xsl:template match="product">
   <xsl:copy>
      <xsl:copy-of select="@sku | title | price | weight" />
      <xsl:apply-templates select="vendor" />
   </xsl:copy>
</xsl:template>

<xsl:template match="vendor">
   <xsl:copy>
      <xsl:copy-of select="company | phone" />
   </xsl:copy>
</xsl:template>

I hope that helps,

Jeni

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



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


Current Thread