Node List Group in the XSL Node Tree

I am parsing a huge word file with test descriptions and am having a problem with nodes. Word basically creates a list of paragraphs, and I want to group them into a parent node. Therefore, for each node 'A', I want to group all the following nodes until the next node 'A' in 'A'.

How can this be done using XSL?

Example: I received:

<A/>
<ab/>
<ac/>
<A/>
<ab/>
<ac/>

But you need:

<A>
<ab/>
<ac/>
</A>
<A>
<ab/>
<ac/>
</A>

Thanks!   

+3
source share
3 answers

If you want to combine all the nodes following <A>, but come before the next <A>, I think you can use something like this:

<xsl:template match="A">
  <xsl:copy>
    <!-- start of range -->
    <xsl:variable name="start" select="count(preceding-sibling::*) + 1" />
    <!-- end of range -->
    <xsl:variable name="stop">
      <xsl:choose>
        <!-- either just before the next A node -->
        <xsl:when test="following-sibling::A">
          <xsl:value-of select="count(following-sibling::A[1]/preceding-sibling::*) + 1" />
        </xsl:when>
        <!-- or all the rest -->
        <xsl:otherwise>
          <xsl:value-of select="count(../*) + 1" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- this for debugging only -->
    <xsl:attribute name="range">
      <xsl:value-of select="concat($start + 1, '-', $stop - 1)" />
    </xsl:attribute>

    <!-- copy all nodes in the calculated range -->
    <xsl:for-each select="../*[position() &gt; $start and position() &lt; $stop]">
      <xsl:copy-of select="." />
    </xsl:for-each>
  </xsl:copy>
</xsl:template>

To enter:

<root>
  <A />
  <ab />
  <ac />
  <A />
  <ab />
  <ac />
</root>

I get (I left the "range" attribute to make the calculations visible):

<A range="2-3">
  <ab />
  <ac />
</A>
<A range="5-6">
  <ab />
  <ac />
</A>
+3

, .

:

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

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kFollowing" match="*[not(self::A)]"
  use="generate-id(preceding-sibling::A[1])"/>

    <xsl:template match="/*">
     <t>
       <xsl:apply-templates select="A"/>
     </t>
    </xsl:template>

    <xsl:template match="A">
     <A>
       <xsl:copy-of select=
          "key('kFollowing',generate-id())"/>
     </A>
    </xsl:template>
</xsl:stylesheet>

XML-:

<t>
    <A/>
    <ab/>
    <ac/>
    <A/>
    <ab/>
    <ac/>
</t>

:

<t>
   <A>
      <ab/>
      <ac/>
   </A>
   <A>
      <ab/>
      <ac/>
   </A>
</t>

, <xsl:key> key() <A/> .

+4

XSLT 2.0 Solution:

<xsl:for-each-group select="*" group-starting-with="A">
  <xsl:element name="{name(current-group()[1])}">
    <xsl:copy-of select="current-group()[position() gt 1]"/>  
  </xsl:element>
</xsl:for-each-group>
+3
source

Source: https://habr.com/ru/post/1705055/


All Articles