Subject: Re: [xsl] Adding structure to a docment based on attributes From: Jörg Heinicke <joerg.heinicke@xxxxxx> Date: Mon, 24 Sep 2001 23:23:37 +0200 |
Hello Nicholas, for your simplified XML the following stylesheet will work: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <xsl:template match="root"> <main> <xsl:apply-templates select="p[@style='Heading1']"/> </main> </xsl:template> <xsl:template match="p[@style='Heading1']"> <section> <heading> <xsl:value-of select="text()"/> </heading> <xsl:apply-templates select="following-sibling::p[1][@style='Body']"/> <xsl:variable name="pos"> <xsl:for-each select="following-sibling::p"> <xsl:if test="@style='Heading1'"> <xsl:value-of select="position()"/> </xsl:if> </xsl:for-each> </xsl:variable> <xsl:apply-templates select="following-sibling::p[@style='Heading2'][position() < $pos]"/> </section> </xsl:template> <xsl:template match="p[@style='Heading2']"> <subsection> <heading> <xsl:value-of select="text()"/> </heading> <xsl:apply-templates select="following-sibling::p[1][@style='Body']"/> </subsection> </xsl:template> <xsl:template match="p[@style='Body']"> <text> <xsl:value-of select="text()"/> </text> </xsl:template> </xsl:stylesheet> But with more complex XML it will be very difficult to get a correct and generally working stylesheet. I hope the code is understandable. Regards, Joerg ----- Original Message ----- From: "Nicholas Waltham" <info@xxxxxxxxxxxx> To: "xsl-list" <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> Sent: Monday, September 24, 2001 1:49 PM Subject: [xsl] Adding structure to a docment based on attributes > Adding sections/subjections and levels depending on formatting type > information > > I have an XML document which contains formatting type information like this > (actually this is a bit simplified) > > > <p style="Heading1">Top Level Heading</p> > <p stlye=Body>Some text at this level</p> > <p style="Heading2">Sub Heading 1</p> > <p stlye=Body>Some text here</p> > <p style="Heading2">Sub heading 2</p> > <p stlye=Body>Some text here</p> > <p style="Heading1">Another Heading</p> > <p stlye=Body>Some more text here</p> > > > I would like to add some logical structure to the document, to have > something like this > <MAIN><HEADING>Top Level Heading</HEADING><TEXT>Some text at this > level</TEXT> > <SUBSECTION><HEADING>Sub Heading 1<TEXT>Some text here</TEXT></SUBSECTION> > <SUBSECTION><HEADING>Sub Heading 2<TEXT>Some text here</TEXT></SUBSECTION> > <HEADING>Top Level Heading</HEADING><TEXT>Some more text here</TEXT> > </MAIN> > > > Can I do this with XSLT, some pointers would be great. > > I know for example I can match the different styles with a template > > match="p[@style='Heading1']" > > but the problem is closing the logical structure at the right place. > > Hope you can help, > Regards, > Nicholas Waltham XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Adding structure to a doc, Wendell Piez | Thread | [xsl] Converting CALS Tables to HTM, Nicholas Waltham |
Re: [xsl] Re: RE: Namespace incompa, Evan Lenz | Date | [xsl] Java extension functions, Matthew L. Avizinis |
Month |