Subject: Re: [xsl] Find elements whose ancestors are the same From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> Date: Mon, 26 Sep 2005 17:00:37 -0400 |
<xsl:template match="folder"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:attribute name="ancestry"> <xsl:for-each select="ancestor-or-self::*/@name"> <xsl:value-of select="."/> <xsl:text>#</xsl:text> </xsl:for-each> </xsl:attribute> </xsl:copy> </xsl:template>
<xsl:template match="folder"> <xsl:variable name="ancestry"> <xsl:for-each select="ancestor-or-self::*/@name"> <xsl:value-of select="."/> </xsl:for-each> </xsl:variable> <xsl:copy-of select="key('folder-by-ancestry',$ancestry)"/> </xsl:template>
Cheers, Wendell
Thanks again to everyone who has replied to these messages. I had no idea how hard this would be. It seemed so simple when I explained it to the client ;-)
I'm looking for an XPath expression that will find all elements whose ancestors are the same. By "same" I mean their @name is the same at each level of ancestry (and they appear in the same order).
For example, this expression would return a count of 2 for the following XML when filtered on the "daughter" folder (or "mother" or "grandfather" for that matter).
<root> <folder name="grandfather"> <folder name="mother"> <folder name="daughter" /> </folder> </folder> <folder name="grandfather"> <folder name="mother"> <folder name="daughter" /> </folder> </folder> <folder name="grandmother"> <folder name="father"> <folder name="son" /> </folder> </folder> </root>
I know it's going to be something like this, but I just can't get my head around it!
<xsl:copy-of select="/root//folder[@name = following-sibling::*/@name and ancestor-or-self::*/@name = following-sibling::*/@name/ancestor-or-self::*/@name" />
====================================================================== 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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Find elements whose ancestors, Gustave Stresen-Reut | Thread | Re: [xsl] Find elements whose ances, JBryant |
Re: [xsl] Xsl:variable question, JBryant | Date | Re: [xsl] Find elements whose ances, JBryant |
Month |