Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Trouble with XSL [Thread Next] Re: Trouble with XSLTo: NULL Date: 1/3/2007 2:13:00 AM Hi Andy, This is known as positional grouping. In XSLT 2.0 you can have for instance something like <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output indent="yes"/> <xsl:template match="/test"> <xsl:for-each-group select="*" group-starting-with="heading1|heading2"> <xsl:copy> <xsl:for-each select="current-group()[position()>1]"> <xsl:copy-of select="."/> </xsl:for-each> </xsl:copy> </xsl:for-each-group> </xsl:template> </xsl:stylesheet> Applied on <test> <heading1>fdfdfdfdf</heading1> <para>text1</para> <para>text2</para> <table>Table structure</table> <para>text3</para> <heading2>gfhgjg</heading2> <para>text4</para> </test> will give you <?xml version="1.0" encoding="UTF-8"?> <heading1> <para>text1</para> <para>text2</para> <table>Table structure</table> <para>text3</para> </heading1> <heading2> <para>text4</para> </heading2> An XSLT 1.0 solution is to walk along following sibling axes until the next node, like below: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output indent="yes"/> <xsl:template match="/test"> <xsl:for-each select="heading1|heading2"> <xsl:variable name="headerId" select="generate-id(.)"/> <xsl:copy> <xsl:for-each select="following-sibling::*[not(self::heading2 or self::heading1) and generate-id(preceding-sibling::*[self::heading1 or self::heading2][1])=$headerId]"> <xsl:copy-of select="."/> </xsl:for-each> </xsl:copy> </xsl:for-each> </xsl:template> </xsl:stylesheet> Best Regards, George --------------------------------------------------------------------- George Cristian Bina <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger www.---.com carra@u... wrote: > Hi > If I have some XML that looks like this... > > <heading1>fdfdfdfdf</heading> > <para>text1</para> > <para>text2</para> > <table>Table structure</table> > <para>text3</para> > <heading2>gfhgjg</heading> > <para>text4</para> > > How do I transform it with XSL so that all the para's and tables under > the headings are wrapped within the heading so that it looks like > this.... > > <heading1> > <para>text1</para> > <para>text2</para> > <table>Table structure</table> > <para>text3</para> > </heading> > <heading2> > <para>text4</para> > </heading2> > > Any help would be appreciated > > Regards > Andy | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
