如何在xsl中对标记集进行分组?

时间:2015-05-07 10:39:19

标签: xslt

我有以下过程的HTML。

<p class="para">First Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>
<p class="pagebreak"/>
<p class="para">Second Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>
<p class="pagebreak"/>
<p class="para">Third Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>

输出应如下所示。

<div class="right">
   <p class="para">First Set of paragraph.</p>
   ......
   <ul>
      <li>List first</li>
      <li>List last</li>
   </ul>
</div>
<div class="left">
   <p class="para">Second Set of paragraph.</p>
   ......
   <ul>
      <li>List first</li>
      <li>List last</li>
   </ul>
</div>
<div class="right">
   <p class="para">Third Set of paragraph.</p>
   ......
   <ul>
     <li>List first</li>
     <li>List last</li>
   </ul>
</div>

将标记分组到 class =&#34; pagebreak&#34; 。第一组正在修复 class =&#34; right&#34; ,第二组正在修复 class =&#34; left&#34; ,或者去上。

提前致谢。

1 个答案:

答案 0 :(得分:2)

以下是使用XSLT 2.0和Saxon 9,XmlPrime或Altova Raptor等XSLT 2.0处理器的建议:

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="body">
  <xsl:copy>
    <xsl:for-each-group select="node()" group-ending-with="p[@class = 'pagebreak']">
      <div class="{if (position() mod 2 eq 0) then 'left' else 'right'}">
        <xsl:copy-of select="current-group()[not(self::p[@class = 'pagebreak'])]"/>
      </div>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

该示例转换

<body>
<p class="para">First Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>
<p class="pagebreak"/>
<p class="para">Second Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>
<p class="pagebreak"/>
<p class="para">Third Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>
</body>

<body><div class="right">
<p class="para">First Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>
</div><div class="left">
<p class="para">Second Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>
</div><div class="right">
<p class="para">Third Set of paragraph.</p>
......
<ul>
   <li>List first</li>
   <li>List last</li>
</ul>
</div></body>