html - XSLT - Dynamic nesting of elements -
i'm working on stylesheet outputs html xml inputs. have generate different nesting levels in output file according position of list of elements in input. instance, news[1] , news[2] should nested under same element news[3] , news[4].
this example of xml input:
<news_list> <news> <title>title of notice #1</tile> <img_url>http://media/image_1.png</img_url> </news> <news> <title>title of notice #2</tile> <img_url>http://media/image_2.png</img_url> </news> <news> <title>title of notice #3</tile> <img_url>http://media/image_3.png</img_url> </news> <news> <title>title of notice #4</tile> <img_url>http://media/image_4.png</img_url> </news> </news_list>
desired html output:
<div class="middle> <div class="unit 1"> <div class="unit 2"> <img src="http://media/image_1.png"/> <p>title of notice #1</p> </div> <div class="unit 2"> <img src="http://media/image_2.png"/> <p>title of notice #2</p> </div> </div> <div class="unit 1"> <div class="unit 2"> <img src="http://media/image_3.png"/> <p>title of notice #3</p> </div> </div class="unit 2"> <img src="http://media/image_4.png"/> <p>title of notice #4</p> </div> </div> </div>
i using xslt position() function select elements , output files don't know how nest childs. appreciated.
i suggest browse news
nodes in 2 phases:
- first phase: match odd position nodes:
<xsl:apply-templates select="news[(position() mod 2)=1]" mode="unit1"/>
...and each one, apply second phase , next one:
<xsl:template match="news" mode="unit1"> <div class="unit 1"> <xsl:apply-templates select="." mode="unit2"></xsl:apply-templates> <xsl:apply-templates select="following-sibling::news[1]" mode="unit2"/> </ div> </xsl:template>
- first phase: match odd position nodes:
- second phase: print each node processed.
<xsl:template match="news" mode="unit2"> <div class="unit 2"> <img src="{img_url}" /> <p><xsl:value-of select="title"/></p> </ div> </xsl:template>
- second phase: print each node processed.
note there 2 templates matching news
, differ in mode
(which proper each phase).
Comments
Post a Comment