[xsl] xsl: how to use <xsl:sequence>

Subject: [xsl] xsl: how to use <xsl:sequence>
From: Selva Ganesh <selvaganesh1985@xxxxxxxxx>
Date: Sun, 10 May 2009 07:34:01 +0530
Hi All,

Many thanks for your response,

Actually you were given the coding looking good, what I gave the
input, but I want to change the input structure the xslt not working
properly.

Here I place one more sample input and output (please read command
instruction in output file) for your reference.

The input not a constant structure, some time <s1> to <s4> will be
changing. Can you correct the XSLT2.0, wherever I change
(insert/delete line) the input, the xslt2.0 coding will be working as
per the structure and output

Input file
=======
<root>
<h2 class="LegP1GroupTitle">Citation, commencement and application</h2>
<p class="LegP1ParaText">
<span class="LegP1No">1.</span>&#8212;(1) These Regulations may </p>
<p class="LegP2ParaText">(2) These Regulations shall </p>
<p class="LegP2ParaText">(3) These Regulations apply </p>
<h2 class="LegP1GroupTitle">Amendment of the Local </h2>
<p class="LegP1ParaText"><span class="LegP1No">2.</span>  The Local
Government</p>
<h2 class="LegP1GroupTitle">Amendment of the Local </h2>
<p class="LegP1ParaText"><span class="LegP1No">3.</span>  The Local
Government Pension </p>
<p class="LegP3Container"><span class="LegP3No">(a)</span>
<span class="LegP3Text">after regulation 36 (actuarial </span>
</p>
</root>

What I need:
=========
<root>
<reg><no>1.&mdash;</no><ti>Citation, commencement and application</ti>
<s1><no>(1)</no><pt>These Regulations may </pt></s1>
<s1><no>(2)</no><pt>These Regulations shall </pt></s1>
<s1><no>(3)</no><pt>These Regulations apply </pt></s1></reg>
<reg><no>2.</no><ti>Amendment of the Local</ti>
<!--here not avilable &#8212;([0-9]), so here start only <pt>, not <s1>-->
<pt>The Local Government </pt></reg>
<reg><no>3.</no><ti>Amendment of the Local </ti>
<!--here not avilable &#8212;([0-9]), so here start only <pt>, not <s1>-->
<pt>The Local Government Pension </pt>
<!-- here not came s1 (0-9), so directly start s2-->
<s2><no>(a)</no><pt>after regulation 36 (actuarial </pt>

<!--
Note: if <p> will be start at
            (1) that's (0-9) is the para return as <s1> part
            (a) that's (a-z) is the para return as <s2> part
            (i) that's (roman letters) is the para return as <s3> part
            (aa) is the para return as <s4> part, totaly s4 levels only -->
</root>


Actually I get:
==========
<reg>
            <no>1.</no>
            <ti>Citation, commencement and application</ti>
            <s1><no>?</no><pt>1. These Regulations may</pt></s1>
            <s1><no>?</no><pt> These Regulations shall </pt></s1>
            <s1><no>?</no><pt> These Regulations apply </pt></s1>
</reg>
<reg>
            <no>2.</no><ti>Amendment of the Local </ti>
<s1><no>?</no><pt>2.  The Local Government</pt></s1>
</reg>
<reg>
            <no>3.</no>
            <ti>Amendment of the Local </ti>
            <s1><no>?</no><pt>3.  The Local Government Pension</pt></s1>
            <s1><no>?</no><pt>(a)after regulation 36 (actuarial </pt></s1>
</reg>

Regards,
Selva

----------------------------------------

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>

Current Thread