Subject: Re: [xsl] collapsing consecutive elements From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Fri, 18 Jan 2002 13:45:20 -0500 |
Does anyone know of a quick and dirty way to collapse a series of elements which each have only one child? ... The following stylesheet is close, but not quite there yet.
T:\ftemp>type saverio.xml <db> <a> <b> <c> <d/> <e/> </c> </b> </a>
<f> <g> <h> <i/> <j/> </h> </g> </f> </db>
T:\ftemp>type saverio.xsl <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="*"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template>
<xsl:template match="*[count(*)=1]"><!--first with one child--> <xsl:apply-templates mode="one-child"/> </xsl:template>
<!--child of last one with one child--> <xsl:template match="*[count(*)!=1]" mode="one-child"> <xsl:variable name="container-name"> <xsl:for-each select=".|ancestor::*[count(*)=1]"> <xsl:if test="position()>1">_</xsl:if> <xsl:value-of select="name(.)"/> </xsl:for-each> </xsl:variable> <xsl:element name="{$container-name}"> <xsl:apply-templates/> </xsl:element> </xsl:template>
T:\ftemp>saxon saverio.xml saverio.xsl <?xml version="1.0" encoding="utf-8"?> <db>
-- Training Blitz: 3-days XSLT/XPath, 2-days XSLFO - Feb 18-22, 2002 - (Early-bird date for discounts is today!)
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 (Fax:-0995) ISBN 0-13-065196-6 Definitive XSLT & XPath ISBN 1-894049-08-X Practical Transformation Using XSLT and XPath ISBN 1-894049-07-1 Practical Formatting Using XSLFO XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed), articles, training(instructor-live,Internet-live,web/CD,licensed) Next public training: 02-02-11,12,14,15,18,21,03-04,05,06,08,11, - 04-08,09,10,12,05-14,15,06-04,07
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] collapsing consecutive el, G. Ken Holman | Thread | RE: [xsl] adding unique values to a, Katie McNally |
Re: [xsl] collapsing consecutive el, David Carlisle | Date | Re: [xsl] Error code 6, david_n_bertoni |
Month |