Re: [xsl] Thought i knew this but i guess not

Subject: Re: [xsl] Thought i knew this but i guess not
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 22 Feb 2011 14:35:35 -0500
Russ,

On 2/22/2011 2:19 PM, russurquhart1@xxxxxxxxxxx wrote:
I am trying to extract only elements having a filter attribute value of 'filter10', 'filter1' or has no filter attributes at all. I would have thought this would do it:

But other filter values are making it into the result file.

Naturally. Using the templates you've given, there are two things you could get in your result:


If the document element (/*) has a @filter containing 'filter10' or 'filter1', or no @filter at all, you'll get the entire document.

Otherwise you'll get nothing.

This is because xsl:copy-of simply copies a branch of the tree to the result. Since either one or the other of your templates will match the element at the top of the document, either it will be copied with all its attributes and descendants, or it won't.

From your question it appears you expect the templates to match recursively down through a full traversal of the tree. But the copy-of instruction doesn't do that. Instead, you want the identity template, which uses xsl:copy (not copy-of) and xsl:apply-templates, to effect a traversal in the normal way.

So basically what you want is a copy of the identity template, plus another template that overrides it for elements matching elements you don't want to copy, which does something special for them. (But we can't write that template until you say whether the contents of these elements might still be copied, or you expect the tree simply to be truncated at those points.)

If you have trouble working it out based on this hint, I'm sure another helpful list member can be more explicit.

Cheers,
Wendell

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:template match="*[@filter]">
     <xsl:if test="contains(@filter, 'filter10') or contains(@filter, 'filter1')">

       <xsl:copy-of select="." />
     </xsl:if>
   </xsl:template>
   <xsl:template match="*[not(@filter)]">
     <xsl:copy-of select="." />
   </xsl:template>
</xsl:stylesheet>


Can someone provide some help on this!


Thanks so much!

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