|
Subject: Re: [xsl] copying attributes to all child nodes From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Mon, 26 Jan 2004 07:18:56 -0500 |
I'm trying to figure out a way to select a node based on an attribute,
then copy that attribute to all children of the node i.e.
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>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
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] copying attributes to all, George Cristian Bina | Thread | [xsl] disable-output-escaping, Márcio Ferreira |
| Re: [xsl] copying attributes to all, George Cristian Bina | Date | [xsl] disable-output-escaping, Márcio Ferreira |
| Month |