Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xml中基于位置的头标记_Xml_Xslt_Xpath_Xslt 1.0 - Fatal编程技术网

xml中基于位置的头标记

xml中基于位置的头标记,xml,xslt,xpath,xslt-1.0,Xml,Xslt,Xpath,Xslt 1.0,我想根据标题的节位置为标题生成一个h1到h3标签 xml的格式是 <sections> <section> <header>section 1 header</header> <image alt="section 1 image alt">imagename.filetype</image> <content>section

我想根据标题的节位置为标题生成一个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>

第1节标题
imagename.filetype
第一节内容
第2节标题
imagename.filetype
第2节内容
第3节标题
imagename.filetype
第3节内容
我的输出应该是

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

第1节标题
第一节内容
第2节标题
第2节内容
第3节标题
第3节内容
有没有一个简单的方法可以做到这一点?任何想法都值得赞赏

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

H
0

使用上面的xslt,它是khachik post的稍微更新版本,这不是答案,因为它看起来很难看,但是太长了,无法发表评论。我不确定这是最好的方法,但是:

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

0

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

这里是一个完全采用推式风格的完整解决方案:

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

在提供的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>

第1节标题
imagename.filetype
第一节内容
第2节标题
imagename.filetype
第2节内容
第3节标题
imagename.filetype
第3节内容
生成所需的正确结果

<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>
    <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节标题
imagename.filetype
第一节内容
第2节标题
imagename.filetype
第2节内容
第3节标题
imagename.filetype
第3节内容

这与我最初的想法类似,可以稍加修改:)好问题,+1。请参阅我的答案,以获得完全符合XSLT(推送样式)精神的完整解决方案:)谢谢你,这正是我想要的!,我现在不明白,但我会解决的。啊,所以在一个加1之前,它先计算头数。明白了谢谢Dimitre+1个好答案。它将精确匹配所需的输出,为
图像添加一个规则,甚至删除标识规则和覆盖元素内置规则的规则。