Re: [xsl] Help: convert without parent element: throughout XSLT-2.0

Subject: Re: [xsl] Help: convert without parent element: throughout XSLT-2.0
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Thu, 07 May 2009 15:02:07 +0200
Selva Ganesh wrote:
How to convert without parent element, all the elements had in same
level but I want the output is multi level elements, how is possible

Input file:
...
...
<h2 class="LegP1GroupTitle">Determination that a </h2>
<p class="LegP1ParaText"><span class="LegP1No">3.</span>&#8212;(1)
This regulation</p>
<p class="LegP2ParaText">(2) Subject to paragraph (3)</p>
<p class="LegP3Container"><span class="LegP3No">(a)</span><span
class="LegP3Text">during an interview </span></p>
<p class="LegP3Container"><span class="LegP3No">(b)</span><span
class="LegP3Text">the confession is in </span></p>
<p class="LegP3Container"><span class="LegP3No">(c)</span><span
class="LegP3Text">the confession inclu</span></p>
<p class="LegP4Container"><span class="LegP4No">(i)</span><span
class="LegP4Text">the period during </span></p>
<p class="LegP5Container"><span class="LegP5No">(aa)</span><span
class="LegP5Text">the period during </span></p>
...

Output file:
...
...
<reg><no>3.&mdash;</no>
<ti>Determination that a </ti>
<s1><no>(1)</no><pt>This regulation </pt></s1>
<s1><no>(2)</no><pt>Subject to paragraph (3)</pt>
<s2><no>(a)</no><pt>during an interview </pt></s2>
<s2><no>(b)</no><pt>the confession is in</pt></s2>
<s2><no>(c)</no><pt>the confession includes</pt>
<s3><no>(i)</no><pt>the period during </pt>
<s4><no>(aa)</no><pt>the period during </pt>
</s4>
</s3>
</s2>
</s1>
</reg>

I did not understand where the 'no' elements for the 's2' elements get their contents from so the following only shows you how to group the elements with xsl:for-each-group group-starting-with:


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

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/*">
<reg>
<xsl:for-each-group select="*" group-starting-with="h2[@class eq 'LegP1GroupTitle']">
<no>
<xsl:value-of select="following-sibling::p[@class eq 'LegP1ParaText'][1]/span[@class eq 'LegP1No']"/>
</no>
<ti>
<xsl:value-of select="."/>
</ti>
<xsl:for-each-group select="current-group() except ." group-starting-with="p[@class eq 'LegP1ParaText']">
<s1>
<no>?</no>
<pt>
<xsl:value-of select="normalize-space(replace(., '\(\d+\)', ''))"/>
</pt>
</s1>
<xsl:for-each-group select="current-group() except ." group-starting-with="p[@class eq 'LegP2ParaText']">
<s1>
<no>?</no>
<pt>
<xsl:value-of select="replace(., '^\(\d+\)', '')"/>
</pt>
<xsl:sequence select="mf:group(current-group() except ., 3)"/>
</s1>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:for-each-group>
</reg>
</xsl:template>


<xsl:function name="mf:group" as="element()*">
<xsl:param name="els" as="element()*"/>
<xsl:param name="n" as="xs:integer"/>
<xsl:for-each-group select="$els" group-starting-with="p[@class eq concat('LegP', $n, 'Container')]">
<xsl:element name="s{$n - 1}">
<no>
<xsl:value-of select="span[@class eq concat('LegP', $n, 'No')]"/>
</no>
<pt>
<xsl:value-of select="span[@class eq concat('LegP', $n, 'Text')]"/>
</pt>
<xsl:sequence select="mf:group(current-group() except ., $n + 1)"/>
</xsl:element>
</xsl:for-each-group>
</xsl:function>


</xsl:stylesheet>

When applied to

<?xml version="1.0" encoding="utf-8" ?>
<root>
<h2 class="LegP1GroupTitle">Determination that a </h2>
<p class="LegP1ParaText">
  <span class="LegP1No">3.</span>&#8212;(1)
  This regulation
</p>
<p class="LegP2ParaText">(2) Subject to paragraph (3)</p>
<p class="LegP3Container">
  <span class="LegP3No">(a)</span>
  <span
class="LegP3Text">during an interview </span>
</p>
<p class="LegP3Container">
  <span class="LegP3No">(b)</span>
  <span
class="LegP3Text">the confession is in </span>
</p>
<p class="LegP3Container">
  <span class="LegP3No">(c)</span>
  <span
class="LegP3Text">the confession inclu</span>
</p>
<p class="LegP4Container">
  <span class="LegP4No">(i)</span>
  <span
class="LegP4Text">the period during </span>
</p>
<p class="LegP5Container">
  <span class="LegP5No">(aa)</span>
  <span
class="LegP5Text">the period during </span>
</p>
</root>

the result with Saxon 9 is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<reg>
   <no>3.</no>
   <ti>Determination that a </ti>
   <s1>
      <no>?</no>
      <pt>3. This regulation</pt>
   </s1>
   <s1>
      <no>?</no>
      <pt> Subject to paragraph (3)</pt>
      <s2>
         <no>(a)</no>
         <pt>during an interview </pt>
      </s2>
      <s2>
         <no>(b)</no>
         <pt>the confession is in </pt>
      </s2>
      <s2>
         <no>(c)</no>
         <pt>the confession inclu</pt>
         <s3>
            <no>(i)</no>
            <pt>the period during </pt>
            <s4>
               <no>(aa)</no>
               <pt>the period during </pt>
            </s4>
         </s3>
      </s2>
   </s1>
</reg>


--


	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread