我想根据其部分位置为标题生成一个 h1 到 h3 标记
xml 格式为
<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>
我的输出会像
<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>
有没有一种简单的方法可以做到这一点?任何想法表示赞赏!
谢谢树猴
更新:
<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>
使用上面的 xslt,它是 khachik 帖子的略微更新版本