1

我想根据其部分位置为标题生成一个 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 帖子的略微更新版本

4

2 回答 2

2

这是一个完全采用推送方式的完整解决方案

<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>

当此转换应用于提供的 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>
       <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>
于 2010-12-12T15:20:39.197 回答
0

这不是一个答案,因为它看起来很丑,但是对于评论来说太长了。我不确定这是最好的方法,但是:

<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>

注意:我不认为取决于位置的标题是一个好主意。DocBook 和其他语言使用节级别来放置适当的标题标签。

于 2010-12-12T14:07:59.190 回答