Re: [xsl] copying attributes to all child nodes

Subject: Re: [xsl] copying attributes to all child nodes
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 26 Jan 2004 07:18:56 -0500
At 2004-01-26 11:04 +0000, Terence Mac Goff wrote:
I'm trying to figure out a way to select a node based on an attribute,

A predicate is used to qualify the node test of a location path expression, but you don't say which nodes you are addressing before filtering. Perhaps you need to address all the children of your context looking for such a node?


select="*[@attributeX='123456']"

Then again, you say "select", but do you mean "match"? Do you want all of the nodes copied verbatim *except* those nodes that happen to have an attribute, and process those differently? I'll assume that, which means you need:

match="*[@attributeX='123456']"

then copy that attribute to all children of the node i.e.

You've phrased your requirement in a way that doesn't mesh with the processing model of XSLT. It sounds more like a DOM process the way you've worded it. Also, your example seems to have all of the descendants of the result with the attribute, not just the children, so I'll assume you meant to say descendants.


If you worded your requirement as "copy all of the descendants of the node including a copy of the given attribute for each", that would be more in line with the processing model.

Remember that an XSLT stylesheet is just a set of construction rules for the result tree, and that you must express your solution as just that: constructing a result tree from the source tree.

An example is below that you can work from. Upon detection of an element that is special, the remaining construction of that element and its descendants is different than just a simple copy: each element adds to its list of specified attributes a copy of the closest ancestral specification of the special attribute. Those elements that are not special are just copied to the result tree.

I hope this helps.

......................... Ken

t:\ftemp>type terence.xml
<top>
<top.level.node attributeX="123456">
  <nextelement attributeXYZ="654321">
     <next2element> this is some text</next2element>
     <next2element> this is some more text </next2element>
  </nextelement>
</top.level.node>
<top.level.node attributeX="123457">
  <nextelement attributeXYZ="654321">
     <next2element> this is some text</next2element>
     <next2element> this is some more text </next2element>
  </nextelement>
</top.level.node>
</top>

t:\ftemp>type terence.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<!--copy all nodes that aren't special-->
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<!--detect an element that is special-->
<xsl:template match="*[@attributeX='123456']">
  <xsl:apply-templates select="." mode="addattr"/>
</xsl:template>

<!--when constructing the tree for all descendants,
    add closest ancestral attribute-->
<xsl:template match="*" mode="addattr">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:copy-of select="ancestor-or-self::*[@attributeX][1]/@attributeX"/>
    <xsl:apply-templates select="node()" mode="addattr"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>saxon terence.xml terence.xsl
<?xml version="1.0" encoding="utf-8"?><top>
<top.level.node attributeX="123456">
  <nextelement attributeXYZ="654321" attributeX="123456">
     <next2element attributeX="123456"> this is some text</next2element>
     <next2element attributeX="123456"> this is some more text </next2element>
  </nextelement>
</top.level.node>
<top.level.node attributeX="123457">
  <nextelement attributeXYZ="654321">
     <next2element> this is some text</next2element>
     <next2element> this is some more text </next2element>
  </nextelement>
</top.level.node>
</top>
t:\ftemp>


-- Public courses: sign up for one or both soon to reserve your seat! Each week: Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO Washington, DC: 2004-03-15 San Francisco, CA: 2004-03-22 Hong Kong, China: 2004-05-17 Bremen, Germany: 2004-05-24 World-wide on-site corporate, government & user group XML training

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread