【问题标题】:Header tag based on position in xml基于xml中位置的标题标签
【发布时间】:2010-12-12 13:32:19
【问题描述】:

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

【问题讨论】:

  • 好问题,+1。请参阅我的答案,以获得完全符合 XSLT(推式)精神的完整解决方案。 :)

标签: xml xslt xpath xslt-1.0


【解决方案1】:

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

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

【讨论】:

  • 感谢这正是我所追求的!,目前不明白,但会解决的。啊,所以它在添加 1 之前计算标题。谢谢Dimitre
  • +1 好答案。它将完全匹配所需的输出,为 image 添加规则,甚至删除标识规则和覆盖元素内置规则的规则。
【解决方案2】:

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

<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 和其他语言使用节级别来放置适当的标题标签。

【讨论】:

  • 这与我最初的想法相似,可以稍微修改一下:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 2015-05-30
  • 2015-08-17
相关资源
最近更新 更多