Subject: [xsl] grouping xhtml title with first sibling From: Matthieu Ricaud-Dussarget <matthieu.ricaud@xxxxxxxxx> Date: Tue, 18 Jan 2011 12:55:21 +0100 |
This is my unit-test sample <?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>unit-test</title> </head> <body> <h1>title1</h1> <p>para1</p> <hr/> <div><img src="img1.jpg" alt=""/></div> <table><tr><td>table1</td></tr></table> <h2>title2</h2> <p>para2</p> <h3>title3</h3> <p>para3</p> <p>para4</p> <h2>title4</h2> <h3>title5</h3> <p>para5</p> <table><tr><td>table2</td></tr></table> <h2>title6</h2> <h1>title7</h1> <p>para6</p> <p>para7</p> <h2>title8</h2> <p class="foo">para8</p> <p>para9</p> <h1>title9</h1> <h2>title10</h2> <h3>title11</h3> <p>para10</p> </body> </html>
Desired ouput is : <?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>unit-test</title> </head> <body> <div class="group"> <h1>title1</h1> <p>para1</p> </div> <hr/> <div><img src="img1.jpg" alt=""/></div> <table><tr><td>table1</td></tr></table> <div class="group"> <h2>title2</h2> <p>para2</p> </div> <div class="group"> <h3>title3</h3> <p>para3</p> </div> <p>para4</p> <div class="group"> <h2>title4</h2> <h3>title5</h3> <p>para5</p> </div> <table><tr><td>table2</td></tr></table> <h2>title6</h2> <div class="group"> <h1>title7</h1> <p>para6</p> </div> <p>para7</p> <h2>title8</h2> <p class="foo">para8</p> <p>para9</p> <div class="group"> <h1>title9</h1> <h2>title10</h2> <h3>title11</h3> <p>para10</p> </div> </body> </html>
I actually like this to work with any h1, h2, ..., h6. In this purpose I gave a param to my xslt : <xsl:param name="elements" select="'h1,h2,h3'"/>
<xsl:param name="debug" select="'no'"/> <xsl:param name="verbose" select="'yes'"/> <xsl:param name="elements" select="'h1,h2,h3'"/>
<xsl:variable name="direct-concerned"> <xsl:for-each select="tokenize($elements,',')"> <xsl:text>self::</xsl:text> <xsl:value-of select="."/> <xsl:if test="not(position()=last())"> <xsl:text> or </xsl:text> </xsl:if> </xsl:for-each> </xsl:variable>
<xsl:variable name="not-following-sibling"> <xsl:text>not(</xsl:text> <xsl:for-each select="tokenize($elements,',')"> <xsl:text>name(following-sibling::*[1])='</xsl:text> <xsl:value-of select="."/> <xsl:text>'</xsl:text> <xsl:if test="not(position()=last())"> <xsl:text> or </xsl:text> </xsl:if> </xsl:for-each> <xsl:text>)</xsl:text> </xsl:variable>
<xsl:variable name="uncopy"> <xsl:for-each select="tokenize($elements,',')"> <xsl:text>preceding-sibling::*[1][self::</xsl:text> <xsl:value-of select="."/> <xsl:text>]</xsl:text> <xsl:if test="not(position()=last())"> <xsl:text> or </xsl:text> </xsl:if> </xsl:for-each> </xsl:variable>
<xsl:template match="* | node() | @*" > <xsl:param name="copy" select="false()"/> <xsl:choose> <xsl:when test="$copy"> <xsl:copy> <xsl:apply-templates select="* | node() | @*" /> </xsl:copy> </xsl:when> <xsl:when test="saxon:evaluate($uncopy)"/> <xsl:when test="saxon:evaluate($direct-concerned)"> <xsl:element name="div" namespace="http://www.w3.org/1999/xhtml"> <xsl:attribute name="class">group</xsl:attribute> <xsl:apply-templates select="self::* | following-sibling::*[1]"> <xsl:with-param name="copy" select="true()"/> </xsl:apply-templates> </xsl:element> </xsl:when> <xsl:otherwise> <xsl:copy> <xsl:apply-templates select="* | node() | @*" /> </xsl:copy> </xsl:otherwise> </xsl:choose> </xsl:template>
-- Matthieu Ricaud IGS-CP Service Livre numirique
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] saxon and maven: quid the, Michael Kay | Thread | Re: [xsl] grouping xhtml title with, Michael Kay |
Re: [xsl] White space treatment iss, David Carlisle | Date | Re: [xsl] grouping xhtml title with, Michael Kay |
Month |