Search code examples
xmlxpathxqueryxslt-2.0

HOW TO GENERATE THE Sequence of formating numbers UNDER THE 'sec/title' ELEMENT BASED ON 'sec/@disp-level' value


Here we are generating the sequence of numbers under the title element based on the 'sec/@disp-level' below are the defined level formating (Anyone can help me):

  • @disp-level='1' = I., II., III. etc.
  • @disp-level='2' = A., B., C. etc.
  • @disp-level='3' = 1., 2., 3. etc.
  • @disp-level='4' = a., b., c. etc.
  • @disp-level='5' = (1), (2), (3) etc.

Example : In this example roman nos. need to be continued.

I. WHEN PROCEEDINGS MAY BE INITIATED

III. JURISDICTION AND VENUE

Note - We are sequenceing under the each section.

INPUT XML:

enter image description here

<?xml version="1.0" encoding="UTF-8"?>
<root>
<sec id="ceb_750004516689gl" disp-level="1" specific-use="15.1">
    <title><named-content content-type="none">&#x00a7;15.1</named-content>  I.  WHEN PROCEEDINGS MAY BE INITIATED</title>
    <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>        
</sec>
<sec id="ceb_1889931838483us" disp-level="1">
    <title>III. JURISDICTION AND VENUE</title>
    <sec id="ceb_623954004359ua" disp-level="2">
        <title>A.   Trusts Under Continuing Court Jurisdiction</title>
        <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
    </sec>
    <sec id="ceb_9159513279045ff" disp-level="2">
        <title>A.   Pursuit of Claims</title>
        <sec id="ceb_791030056239at" disp-level="3" specific-use="15.2">
            <title><named-content content-type="none">&#x00a7;15.2</named-content>  1.  Supervised Versus Unsupervised Trusts</title>
            <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
        </sec>
        <sec id="ceb_825038660717of" disp-level="3" specific-use="15.3">
            <title><named-content content-type="none">&#x00a7;15.3</named-content>  3.  Removal From Continuing Court Jurisdiction</title>
            <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
            <sec id="ceb_2491751884245sa" disp-level="4" specific-use="15.3A" sec-type="A">
                <title><named-content content-type="none">&#x00a7;15.3A</named-content> a.  Mandatory Removal for Trust With Corporate Trustee</title>
                <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
            </sec>
            <sec id="ceb_655906801656qm" disp-level="4" specific-use="15.3B" sec-type="B">
                <title><named-content content-type="none">&#x00a7;15.3B</named-content> 2.  Discretionary Removal for Trust With Individual Trustee</title>
                <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
                <sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35">
                    <title><named-content content-type="none">&#x00a7;15.35</named-content> (1) Consent or Affirmation by Beneficiaries</title>
                    <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
                </sec>
                <sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35A" sec-type="A">
                    <title><named-content content-type="none">&#x00a7;15.35A</named-content>    (1) Consent or Affirmation by Beneficiaries (Added Section 5)</title>
                    <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
                </sec>
            </sec>
        </sec>
    </sec>
</sec>
<sec id="ceb_541077295354wt" disp-level="1">
    <title>VI.  LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES</title>
    <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</root>

EXPECTED OUTPUT:

enter image description here

<?xml version="1.0" encoding="UTF-8"?>
<root>
<sec id="ceb_750004516689gl" disp-level="1" specific-use="15.1">
    <title><named-content content-type="none">&#x00a7;15.1</named-content>  I.  WHEN PROCEEDINGS MAY BE INITIATED</title>
    <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>        
</sec>
<sec id="ceb_1889931838483us" disp-level="1">
    <title>II.  JURISDICTION AND VENUE</title>
    <sec id="ceb_623954004359ua" disp-level="2">
        <title>A.   Trusts Under Continuing Court Jurisdiction</title>
        <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
    </sec>
    <sec id="ceb_9159513279045ff" disp-level="2">
        <title>B.   Pursuit of Claims</title>
        <sec id="ceb_791030056239at" disp-level="3" specific-use="15.2">
            <title><named-content content-type="none">&#x00a7;15.2</named-content>  1.  Supervised Versus Unsupervised Trusts</title>
            <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
        </sec>
        <sec id="ceb_825038660717of" disp-level="3" specific-use="15.3">
            <title><named-content content-type="none">&#x00a7;15.3</named-content>  2.  Removal From Continuing Court Jurisdiction</title>
            <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
            <sec id="ceb_2491751884245sa" disp-level="4" specific-use="15.3A" sec-type="A">
                <title><named-content content-type="none">&#x00a7;15.3A</named-content> a.  Mandatory Removal for Trust With Corporate Trustee</title>
                <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
            </sec>
            <sec id="ceb_655906801656qm" disp-level="4" specific-use="15.3B" sec-type="B">
                <title><named-content content-type="none">&#x00a7;15.3B</named-content> b.  Discretionary Removal for Trust With Individual Trustee</title>
                <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
                <sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35">
                    <title><named-content content-type="none">&#x00a7;15.35</named-content> (1) Consent or Affirmation by Beneficiaries</title>
                    <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
                </sec>
                <sec id="ceb_113027963776kc" disp-level="5" specific-use="15.35A" sec-type="A">
                    <title><named-content content-type="none">&#x00a7;15.35A</named-content>    (2) Consent or Affirmation by Beneficiaries (Added Section 5)</title>
                    <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
                </sec>
            </sec>
        </sec>
    </sec>
</sec>
<sec id="ceb_541077295354wt" disp-level="1">
    <title>III. LITIGATION INVOLVING TRUSTEE AND THIRD PARTIES</title>
    <p content-type="new">In the course of administering a trustee or beneficiary, <italic>e.g.,</italic> to seek advance court approval of a proposed action.</p>
</sec>
</root>

XSLT CODE:

<?xml version="1.0" encoding="UTF-8"?>
<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:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Solution

  • I think you simply want to use xsl:number with an appropriate format:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="3.0">
     
      <xsl:param name="formats" as="map(xs:string, xs:string)"
        select="map { '1' : 'I.', '2' : 'A.', '3' : '1.', '4' : 'a.', '5' : '(1)' }"/>
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:template match="sec[@disp-level]/title/text()[1]">
          <xsl:variable name="number" as="xs:string">
              <xsl:number count="sec[@disp-level]" format="{$formats(current()/../../@disp-level)}"/>
          </xsl:variable>
          <xsl:value-of select="replace(., '^\s*\(?[A-Z0-9.]+\)?', $number)"/>
      </xsl:template>
      
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/naZYrqt

    That is XSLT 3 using an XPath 3.1 map to store the mapping from level number to level format but of course it could be modelled in an XML variable or a sequence in earlier versions of XSLT:

     <xsl:param name="formats" as="xs:string*"
        select="'I.', 'A.', '1.', 'a.', '(1)'"/>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
        
      <xsl:template match="sec[@disp-level]/title/text()[1]">
          <xsl:variable name="number" as="xs:string">
              <xsl:number count="sec[@disp-level]" format="{$formats[position() = current()/../../@disp-level]}"/>
          </xsl:variable>
          <xsl:value-of select="replace(., '^\s*\(?[A-Z0-9.]+\)?', $number)"/>
      </xsl:template>