xsl-list
[Top] [All Lists]

RE: [xsl] returning nodes which have a specific child

2009-07-02 05:02:47
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="*[*[(_at_)name='CONTENT']]" ><!-- keep wanted -->
   <xsl:copy>
     <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

Michael Ludwig


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--