• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Section nesting

All:

How do you I transform below through xsl:
Input
<?xml version='1.0' encoding='utf-8'?>
<book>
      <section order="1"><p>Section 1: para</p></section>      
            <section order="1">
                 <p>Section 1: para</p>
                  <section order="2">
                        <p>Section 2: para</p>
                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
                  </section>
            </section>
      
      <section order="1">
      <p>Section 1: para</p>
            <section order="2"><p>Section 2: para</p></section>
      </section>
      <dont>
            <section order="1">
                  <p>not to touch</p>
            </section>
      </dont>
      
</book>

output:

<?xml version='1.0' encoding='utf-8'?>
<book>
      <section order="1"><p>Section 1: para</p></section>      
            <section order="1">
                 <p>Section 1: para</p>
                  <section order="2">
                        <p>Section 2: para</p>
                              <section order="3">
                                    <section order="4">
                                          <p>Section 4: para</p>
                                    </section>
                              </section>
                  </section>
            </section>
      
      <section order="1">
      <p>Section 1: para</p>
            <section order="2"><p>Section 2: para</p></section>
      </section>
      <dont>
            <section order="1">
                  <p>not to touch</p>
            </section>
      </dont>
      
</book>

But when I try with node position it always returns a different one. Pl advise. Moreover, order values goes upto "n" values of depth.

Thanks
0
rshk2001
Asked:
rshk2001
  • 5
  • 3
1 Solution
 
Geert BormansCommented:
Can you be more explicit about what exactly you want to happen.
I see that you added a section3, but what is the logic behind it?
I would like to help but have no clue where the question is
0
 
rshk2001Author Commented:
Gertone:

I am looking for grouping of sections based on the attribute value. Eg: The place where I added <section order="3"> is an hierarchy/depth of section level. Basically the order follows as: level 1, level 2, level 3..., when level 4 follows immediately after level 2 (ie <section order="2">) then a third order level is introduced before level 4 and that explains the above structure.

Thanks
0
 
Geert BormansCommented:
OK, I see, can you use XSLT2, or should this be done using XSLT1?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rshk2001Author Commented:
xslt1
0
 
Geert BormansCommented:
OK, I need more insight in the restrictions then.
Can I have a section 6 in a section 3, so that I have to add 4 AND 5?
0
 
Geert BormansCommented:
and what is the logic with the P?ยต
All P before the first section inside a section are left outside the grouping section,
or do you just need a wrapper around the subsection itself?

How do you deal with this?
  <section order="2">
                        <p>Section 2: para</p>
                        <p>Section 2: para</p>
                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
                  </section>

and can this happen?
<section order="2">
                        <p>Section 2: para</p>
                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
                        <p>Section 2: para</p>
                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
                         <p>Section 2: para</p>
                 </section>
and if so, how do you want to deal with it?

I must say that based on your extreme flood of communication,
this is more an analysis task than a development task :-)
0
 
rshk2001Author Commented:
Gertone: Pl find below

  <section order="2">
                        <p>Section 2: para</p>
                        <p>Section 2: para</p>
<section order="3">

                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
</section>
                  </section>

>> and can this happen? - Yes and below is the tagging structure
<section order="2">
                        <p>Section 2: para</p>
            <section order="3">
                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
</section>
                        <p>Section 2: para</p>
<section order="3">
                              <section order="4">
                                    <p>Section 4: para</p>
                              </section>
</section>
                         <p>Section 2: para</p>
                 </section>

Yes, if level 6 occurs then level 5 and level 4 must wrap before level 6.

Thanks
0
 
Geert BormansCommented:
This stylesheet will do most of what you want,
it will fail if subsequent sections have different @order.
If that is the case, you will have to run the stylesheet a couple of times in a row

If it gets more complex than this, please start using XSLT2
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()[1]"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"></xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="section[parent::section]">
        <xsl:variable name="par-order" select="parent::section/@order"></xsl:variable>
        <xsl:variable name="this-order" select="@order"></xsl:variable>
        <xsl:call-template name="create-grouping-section">
            <xsl:with-param name="par-order" select="parent::section/@order"/>
        </xsl:call-template>
        <xsl:apply-templates select="following-sibling::node()[not(self::section)][1]"></xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="section" mode="inner-sect">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="node()[1]"/>
            </xsl:copy>
            <xsl:apply-templates select="following-sibling::node()[1][self::section]" mode="inner-sect"/>
    </xsl:template>
    
    <xsl:template name="create-grouping-section">
        <xsl:param name="par-order"/>
        <xsl:choose>
            <xsl:when test="@order - $par-order > 1">
                <section order="{$par-order + 1}">
                    <xsl:call-template name="create-grouping-section">
                        <xsl:with-param name="par-order" select="$par-order + 1"/>
                    </xsl:call-template>
                </section>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates select="node()[1]"/>
                </xsl:copy>
                <xsl:apply-templates select="following-sibling::node()[1][self::section]" mode="inner-sect"/>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
    
    
    <xsl:template match="dont">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()" mode="dont"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"></xsl:apply-templates>
    </xsl:template>

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

</xsl:stylesheet>

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now