找到连续性的节点,然后向它们添加父标记 xslt 1.0

Find the nodes which are coming in continuety and then add a parent tag to them xslt1.0

我有一个 xml 如下所示:

<root>
    <p>some text</p>
    <ol>
      <li>
         <a href="some-link" rel="nofollow">link</a>
      </li>
    </ol>
    <li>tag with parent as root</li>
    <li>another li tag with parent as root</li>
    <li>another li tag with parent as root</li>
    <p>some more text</p>
    <li>some text in li.</li>
    <li>another li tag with parent as root</li>
    <p>some more text</p>
    <li>some text in li.</li>
    <a href="http://cowherd.com" rel="nofollow">http://cowherd.com</a>
</root>

期望的输出:我想为没有任何父级的所有 li 标签添加一个父级,如果有多个 li 标签连续出现,我还想将 li 标签添加到一个共同的父级中.

 <root>
        <p>some text</p>
        <ol>
          <li>
             <a href="some-link" rel="nofollow">link</a>
          </li>
        </ol>
        <ul>
          <li>tag with parent as root</li>
          <li>another li tag with parent as root</li>
          <li>another li tag with parent as root</li>
        </ul>
        <p>some more text</p>
        <ul>
          <li>some text in li.</li>
          <li>another li tag with parent as root</li>
        </ul>
        <p>some more text</p>
        <ul>
          <li>some text in li.</li>
        </ul>
    </root>

我尝试了各种方法但没有用。提前致谢

  <xsl:template match="li[not(parent::ol) and not(parent::ul)]">
  <ul>
     <xsl:copy-of select="."/>
   </ul>
  </xsl:template>


<xsl:template match="li[not(parent::ol) and not(parent::ul)]">
  <xsl:for-each select="root/li">
        <ul><xsl:value-of select="."/></ul>
  </xsl:for-each>
</xsl:template>


<xsl:template match="li[not(parent::ol) and not(parent::ul)]">
        <ul><xsl:apply-templates/></ul>
    </xsl:template>

这在 XSLT 1 中是可行的,使用一个键,一个相当复杂的键,但完成工作:

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

  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*"/>
  
  <xsl:key name="li-group" match="*[not(self::ol | self::ul)]/li[preceding-sibling::*[1][self::li]]" use="generate-id(preceding-sibling::li[not(preceding-sibling::*[1][self::li])][1])"/>

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

  <xsl:template match="*[not(self::ol | self::ul)]/li[not(preceding-sibling::*[1][self::li])]">
    <ul>
      <xsl:copy-of select=". | key('li-group', generate-id())"/>
    </ul>
  </xsl:template>
  
  <xsl:template match="*[not(self::ol | self::ul)]/li[preceding-sibling::*[1][self::li]]"/>

</xsl:stylesheet>

其他选项是兄弟递归。