基于 xml 中位置的标头标记
我想根据其部分位置为标头生成 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>
格式,有没有一种简单的方法可以做到这一点?任何想法表示赞赏!
非常感谢 Treemonkey
更新:
<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 帖子的稍微更新版本
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个完全采用推送风格的完整解决方案:
当此转换应用于提供的 XML 文档时:
生成所需的正确结果:
Here is a complete solution which is entirely in push style:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
这不是一个答案,因为它看起来很丑,但对于评论来说太长了。我不确定这是最好的方法,但是:
注意:我认为根据位置进行前进不是一个好主意。 DocBook 和其他语言使用节级别来放置适当的标题标记。
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:
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.