Reputation: 2163
I would like to produce a h1 to h3 tag for a header based on its section position
the xml is in the format
<sections>
<section>
<header>section 1 header</header>
<image alt="section 1 image alt">imagename.filetype</image>
<content>section 1 content</content>
</section>
<section>
<header>section 2 header</header>
<image alt="section 2 image alt">imagename.filetype</image>
<content>section 2 content</content>
</section>
<section>
<header>section 3 header</header>
<image alt="section 3 image alt">imagename.filetype</image>
<content>section 3 content</content>
</section>
</sections>
my output would want to be like
<div>
<h1> section 1 header</h1>
<img alt="section 1 image alt" src="imagename.filename"/>
section 1 content
</div>
<div>
<h2> section 2 header</h2>
<img alt="section 2 image alt" src="imagename.filename"/>
section 2 content
</div>
<div>
<h3> section 3 header</h3>
<img alt="section 3 image alt" src="imagename.filename"/>
section 3 content
</div>
is there a simple way to do this? any ideas appreciated!
kind thanks Treemonkey
update:
<xsl:template mode="section" match="section">
<xsl:apply-templates mode="header" select="header">
<xsl:with-param name="position">h<xsl:value-of select="position()"/></xsl:with-param>
</xsl:apply-templates>
<img alt="{image/@alt}" src="{image}" />
<xsl:value-of select="content"/>
</xsl:template>
<xsl:template mode="header" match="header">
<xsl:param name="position">0</xsl:param>
<xsl:element name="{$position}"><xsl:value-of select="."/></xsl:element>
</xsl:template>
using the above xslt which is slightly updated version of khachik post
Upvotes: 1
Views: 2456
Reputation: 243529
Here is a complete solution which is entirely in push style:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="section">
<div>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="header">
<xsl:element name="h{count(../preceding-sibling::section)+1}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="sections|content">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<sections>
<section>
<header>section 1 header</header>
<image alt="section 1 image alt">imagename.filetype</image>
<content>section 1 content</content>
</section>
<section>
<header>section 2 header</header>
<image alt="section 2 image alt">imagename.filetype</image>
<content>section 2 content</content>
</section>
<section>
<header>section 3 header</header>
<image alt="section 3 image alt">imagename.filetype</image>
<content>section 3 content</content>
</section>
</sections>
the wanted, correct result is produced:
<div>
<h1>section 1 header</h1>
<image alt="section 1 image alt">imagename.filetype</image>
section 1 content
</div>
<div>
<h2>section 2 header</h2>
<image alt="section 2 image alt">imagename.filetype</image>
section 2 content
</div>
<div>
<h3>section 3 header</h3>
<image alt="section 3 image alt">imagename.filetype</image>
section 3 content
</div>
Upvotes: 2
Reputation: 28703
This not an answer since it looks ugly, but is too long for a comment. I'm not sure this is the best way to do it, but:
<xsl:template match="section">
<xsl:apply-templates select="./header">
<xsl:with-param name="position"><xsl:value-of select="position()"/>
</xsl:with-param>
</xsl:apply-templates>
<!-- section stuff here -->
</xsl:template>
<xsl:template match="header">
<xsl:param name="position">0</xsl:param>
<xsl:variable name="tagname"><xsl:value-of select="concat('h', $position)"></xsl:value-of></xsl:variable>
<xsl:element name="{$tagname}"><xsl:value-of select="."/></xsl:element>
</xsl:template>
A note: I don't think heading depending on the position is a good idea. DocBook and another languages use the section level to put the appropriate header tag.
Upvotes: 0