Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Trouble with XSL >Thread Next - Re: Trouble with XSL Re: Trouble with XSLTo: NULL Date: 1/3/2007 2:24:00 AM carra@u... wrote: > If I have some XML that looks like this... No you don't. That's not XML. Where's the root element? > <heading1>fdfdfdfdf</heading> For that matter, this is not well-formed. > <para>text1</para> > <para>text2</para> > <table>Table structure</table> > <para>text3</para> > <heading2>gfhgjg</heading> Neither is this. > <para>text4</para> So you can't be bothered to spend fifteen minutes reading XSLT/XPath tutorial, you can't be bothered to spend fifteen *seconds* proof-reading your post, and you're asking for help. Ok then. > 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> Since you failed to mention what should happen to headings' children and attributes, I won't address that. Here's a little something for you to chew on. Have fun: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key name="heading" match="*[not(starts-with(local-name(),'heading'))]" use= " generate-id ( preceding-sibling::* [starts-with(local-name(),'heading')][1] ) "/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="*[starts-with(local-name(),'heading')]"> <xsl:copy> <xsl:apply-templates select="key('heading',generate-id())" mode="copy"/> </xsl:copy> </xsl:template> <xsl:template match= " * [not(starts-with(local-name(),'heading'))] [ preceding-sibling::* [starts-with(local-name(),'heading')] ] "/> <xsl:template match="@*|node()" mode="copy"> <xsl:copy> <xsl:apply-templates select="@*|node()" mode="copy"/> </xsl:copy> </xsl:template> </xsl:stylesheet> -- Pavel Lepin | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
