子节点之间的XSLT父内容分割

时间:2014-12-16 11:52:06

标签: xml xslt

XML:

<text> Day light saving normally starts on 
<date>29 March 2015 </date> 
in some countries     
<footnote> <text> According to wikipedia </text> </footnote> 
in Europe </text>

预期产出:

Day light saving normally starts on 29 March 2015 in some
countries<sup>1</sup> in Europe

<sup>1</sup> According to wikipedia

什么是XSLT?

在我的XSLT中,我试图使用node()来捕获所有元素和内容,但是徒劳无功。

<xsl:template match="text">
<xsl:if test= "./footnote">
   <xsl:for-each select="node()">
     <xsl:if test= "not(name() = footnote">
           <xsl:value-of select="text()" />
     </xsl:if>
     <xsl:if test= "name() = footnote">
       <xsl:apply-templates select="text()" mode="footnote"/>
     </xsl:if>
    </xsl:for-each>
</xsl:if>

<xsl:template match="text/footnote" mode="footnote">
<xsl:element name="FootNote">
  <xsl:value-of select="." />
</xsl:element>
</xsl:template>

1 个答案:

答案 0 :(得分:2)

您可以使用<xsl:number>

执行此操作
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <div>
      <div>
        <xsl:apply-templates />
      </div>
      <xsl:apply-templates select="//footnote" mode="footnotes" />
    </div>
  </xsl:template>

  <xsl:template match="footnote" name="Index">
    <sup>
      <xsl:number level="multiple" />
    </sup>
  </xsl:template>

  <xsl:template match="footnote" mode="footnotes">
    <div>
      <xsl:call-template name="Index" />
      <xsl:text> </xsl:text>
      <xsl:apply-templates select=".//text()" />
    </div>
  </xsl:template>

</xsl:stylesheet>

在此输入上运行时:

<text>
  Day light saving normally starts on
  <date>29 March 2015 </date>
    <footnote>
    <text> A very marvelous date </text>
  </footnote>

  in some countries
  <footnote>
    <text> According to wikipedia </text>
  </footnote>
  in Europe
</text>

结果是:

<div>
  <div>
  Day light saving normally starts on
  29 March 2015 <sup>1</sup>

  in some countries
  <sup>2</sup>
  in Europe
</div>
  <div><sup>1</sup>  A very marvelous date </div>
  <div><sup>2</sup>  According to wikipedia </div>
</div>