RE: [xsl] Joining children attributes with parent

Subject: RE: [xsl] Joining children attributes with parent
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 03 Dec 2004 18:32:54 -0500
And in XSLT 1.0:

<xsl:for-each select="//*">
  <xsl:for-each select="ancestor-or-self::*/@*">
    <xsl:if test="not(position=1)">
      <xsl:text>,</xsl:text>
    </xsl:if>
    <xsl:value-of select=".">

... don't worry about the performance: this is the kind of performance XSLT engines are built to do.

If your trees really are so big this is ever a problem, it could be helped by doing a recursive descent and passing parameters along the way:

<xsl:template match="*"/>
  <xsl:param name="ancestor-attrs" select="/.."/>
  <xsl:variable name="all-attrs" select="@* | $ancestor-attrs"/>
  <xsl:for-each select="$all-attrs">
    <xsl:if test="not(position=1)">
      <xsl:text>,</xsl:text>
    </xsl:if>
    <xsl:value-of select=".">
  </xsl:for-each>
  <xsl:apply-templates>
    <xsl:with-param name="ancestor-attrs" select="$all-attrs"/>
  </xsl:apply-templates>
</xsl:template>

But I wouldn't bother with that unless I had a real reason to. (Obsessive-compulsive disorder in itself is no good reason.)

Cheers,
Wendell

At 06:00 PM 12/3/2004, you wrote:
No problem. In XSLT 2.0:

xsl:for-each select="//*"
  xsl:value-of select="string-join(ancestor-or-self::*/@att, ',')"

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

> -----Original Message-----
> From: Ross Niemi [mailto:ross.niemi@xxxxxxxxx]
> Sent: 03 December 2004 22:48
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Joining children attributes with parent
>
> I'm not too sure if this is even possible in XSLT or if it may be too
> slow when implemented (since a parent node may need to be revisited
> multiple times), but this is what I'm trying to do:  I'm trying to
> join a set of parent's attributes with its children's attributes and
> their children's attributes for an arbitrary number of descendants
> (this will be displayed in CSV format).  Would be nice if there is a
> way to do this bottom up.
>
> input:
>
> parent1
>   child1
>   child2
> parent2
>   child3
>   child4
>     grandchild1
>     grandchild2
>
> output (assuming each element is a set of attributes for the
> element above):
>
> parent1,child1
> parent1,child2
> parent2,child3
> parent2,child4,grandchild1
> parent2,child4,grandchild2
>
> Cheers!
>
> -- Ross
>

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