This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: select only some elements and attributes
- To: "Meili Zhong" <mzhong at interwoven dot com>
- Subject: Re: [xsl] select only some elements and attributes
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Thu, 15 Feb 2001 09:18:18 +0000
- CC: XSL-List at lists dot mulberrytech dot com
- Organization: Jeni Tennison Consulting Ltd
- References: <013d01c096e9$f02be9d0$a129000a@mzhongbng.interwoven.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
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