[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: returning nodes which have a specific child

Subject: RE: returning nodes which have a specific child
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 2 Jul 2009 10:02:09 +0100
RE:  returning nodes which have a specific child
> I'm relatively new to xsl and to be honest a lot of it seems
> like a black art.

I suspect you're looking at XSLT code and trying to reverse engineer the
design principles of the language from that. Since you're the kind of person
who feels a need to understand the concepts rather than learning by
cut-and-paste, I think you'd benefit from reading a good book on the
language.

> Can you briefly describe what your
> solution does?

It describes three rules:

Rule 1: By default, when you find a node, do a shallow copy of this node and
process its attributes and children by applying these rules recursively.

Rule 2: But if you find an element, don't copy it; just process its child
elements by applying these rules recursively.

Rule 3: But if you find an element that has a child element that has an
attribute @name whose value is 'CONTENT', do the same as in Rule 1.

The tricky bit here is the priorities of the rules. I think Michael's been a
bit naughty here by giving rule 2 a priority of -0.4 and leaving the
priority of the other rules unspecified. In fact, the rules for defaulting
priority mean that rule 1 has a priority of -0.5, and rule 3 has a priority
of +0.5. (Higher number means higher priority.) I would have given the rules
explicit priorities of 1, 2, and 3 respectively to make this more readable.

Michael has also taken a short-cut in that Rule 1 is a very commonly used
"default rule" called the identity template, so he has re-used it; but in
your case it's never used for element nodes, so it could be replaced by the
simpler rule

  <xsl:template match="@*">
    <xsl:copy/>
  </xsl:template>

(I'm not sure why rule 3 is processing the attributes of an element and rule
2 isn't, but I didn't study your original problem.)

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay


> >
> > <xsl:stylesheet version="1.0"
> >  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> >
> >  <xsl:template match="@*|node()"><!-- identity template -->
> >    <xsl:copy>
> >      <xsl:apply-templates select="@*|node()"/>
> >    </xsl:copy>
> >  </xsl:template>
> >
> >  <xsl:template match="*" priority="-0.4" ><!-- skip unwanted -->
> >    <xsl:apply-templates select="*"/>
> >  </xsl:template>
> >
> >  <xsl:template match="*[*[@name='CONTENT']]" ><!-- keep wanted -->
> >    <xsl:copy>
> >      <xsl:apply-templates select="@*|node()"/>
> >    </xsl:copy>
> >  </xsl:template>
> >
> > </xsl:stylesheet>
> >
> > Michael Ludwig

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.