Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: "Michael Kay" <mike@--------.--->
To: <xsl-list@-----.------------.--->
Date: 7/2/2009 9:03:00 AM
> 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
> >
> > 
> --~------------------------------------------------------------------
> > 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@l...>
> > --~--
> >
> >
> 
> --~------------------------------------------------------------------
> 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@l...>
> --~--
> 


--~------------------------------------------------------------------
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@l...>
--~--



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent