|
Subject: Re: [xsl] creating container elements From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Tue, 06 Apr 2004 08:54:15 -0400 |
This is the first time I have looked at XSLT in anger and have what I thought was a simple question.
Given an input document containing amongst other things a recurring element I would like to create a container element in the output document.
To my mind this was a case of output an opening container tag prior to the first occurrent of the recurrent element and a closing tag after the last occurrence.
When I first approached this I tried using something such as
<xsl:if test="postion() = 1"> <container> </xsl:if>
but this complained as it was not valid XML.
Upon seeking advice from a colleague it was suggested I use modes to control the output and output the container element in the template matching the first element.
Given the following test document
<foo> <bar id="bar-1"> <barfoo id="barfoo-1"/> </bar> <bar id="bar-2"> <barfoo id="barfoo-2"/> </bar> <bar id="bar-3"> <barfoo id="barfoo-3"/> </bar> </foo>
Which seems to have spurious <barfoo> elements following the closing </container> element.
From my continued efforts I seem to have this down to the behaviour when using the mode and I can resolve this be added the mode attribute to my <barfoo> template match. I am suprised I need this as I would have thought it would work without this.
I would appreciate any help anyone can give regarding this. How should this be
done
and any explanation as to why this occurs.
FYI - the following is the required information regarding the xslt engine I am
using which for this was Xalan-J 2.5.2
<?xml version="1.0" encoding="UTF-8"?> xmlns:msxsl="http://www.w3.org/TR/WD-xsl">
T:\ftemp>type exley.xml
<foo>
<bar id="bar-1">
<barfoo id="barfoo-1"/>
</bar>
<bar id="bar-2">
<barfoo id="barfoo-2"/>
</bar>
<bar id="bar-3">
<barfoo id="barfoo-3"/>
</bar>
</foo>T:\ftemp>type exley.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt" exclude-result-prefixes="xalan"> <xsl:output method="xml" indent="yes" xalan:indent-amount="2" />
<xsl:template match="foo">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template> <xsl:template match="bar[1]">
<container>
<xsl:apply-templates select="../bar" mode="standard"/>
</container>
</xsl:template> <xsl:template match="bar" mode="standard">
<foobar id="{@id}">
<xsl:apply-templates />
</foobar>
</xsl:template> <xsl:template match="barfoo">
<barfoo>
<xsl:value-of select="@id"/>
</barfoo>
</xsl:template>T:\ftemp>saxon exley.xml exley.xsl <?xml version="1.0" encoding="utf-8"?> <foo>
<container>
<foobar id="bar-1"> </foobar>
<foobar id="bar-2"> </foobar>
<foobar id="bar-3"></foobar> </container>
</foo> T:\ftemp>
-- Public courses: Spring 2004 world tour of hands-on XSL instruction Each week: Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO Hong Kong May 17-21; Bremen Germany May 24-28; Helsinki June 14-18
World-wide on-site corporate, govt. & user group XML/XSL 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 -> |
|---|---|---|
| [xsl] creating container elements, Rob Exley | Thread | RE: [xsl] creating container elemen, Michael Kay |
| RE: [xsl] creating container elemen, Andrew Welch | Date | RE: [xsl] creating container elemen, Michael Kay |
| Month |