XSLT 2.0 - 合并相邻的OL元素

时间:2017-04-24 08:34:41

标签: xslt-2.0

我尝试合并<ol>元素中出现的相邻<ul> | <li>元素但未成功。

因此转向:

 <ol>
 <li>Text node <p>Child node</p>
  <ol>
   <li>1</li>
  </ol>  
  <ol>
   <li>2</li>
   <li>3</li>
   <li>4</li>
  </ol>
  <p>Some text</p>
  <ol>
   <li>1</li>
  </ol>
 </li>
</ol>

<ol>
 <li>Text node <p>Child node</p>
  <ol>
   <li>1</li>
   <li>2</li>
   <li>3</li>
   <li>4</li>
  </ol>
  <p>Some text</p>
  <ol>
   <li>1</li>
  </ol>
 </li>
</ol>

现在,我已经想出了这个:

  <xsl:template match="li[ol]" priority="5">   
    <xsl:copy copy-namespaces="no">          
      <ol>
        <xsl:for-each-group select="node()" group-adjacent="boolean(self::ol)">
          <xsl:choose>            
            <xsl:when test="current-grouping-key() and current-group()[self::*]">
              <xsl:apply-templates select="current-group()/node()"/>            
            </xsl:when>
            <xsl:otherwise>              
              <xsl:apply-templates select="current-group()"/>              
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each-group>   
      </ol>
    </xsl:copy>
  </xsl:template>

这给了我:

  <ol>
    <li>
      <ol>Text node <p>Child node</p>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <p>Some text</p>
        <li>1</li>
      </ol>
    </li>
  </ol>

所以我很难不将父<li>的文字移到合并的<ol>中,我该如何解决?

1 个答案:

答案 0 :(得分:0)

使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="li[ol]">
        <xsl:copy>
            <xsl:for-each-group select="node()"
                group-adjacent="boolean(self::ol | self::text()[not(normalize-space())])">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <ol>
                            <xsl:copy-of select="current-group()/node()"/>
                        </ol>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

你可以改变

<ol>
    <li>Text node <p>Child node</p>
        <ol>
            <li>1</li>
        </ol>  
        <ol>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ol>
        <p>Some text</p>
        <ol>
            <li>1</li>
        </ol>
    </li>
</ol>

<ol>
   <li>Text node <p>Child node</p>
      <ol>
         <li>1</li>
         <li>2</li>
         <li>3</li>
         <li>4</li>
      </ol>
      <p>Some text</p>
      <ol>
         <li>1</li>
      </ol>
   </li>
</ol>