adding structure to flat-ish XML file via XSLT

I have some data that, when simplified, looks like this:


<data>
	<section>A..</section>
	<section type="1">B..</section>
	<section>C..</section>
	<section>D..</section>
	<section type="1">E..</section>
	<section>F..</section>
	<section>G..</section>
	<section>H..</section>
	<nonsection>J..</nonsection>
	<nonsection>K..</nonsection>
</data>

Open in new window


A type="1" attribute on the section element indicates the start of a group that ends when either another type="1" is found, or when a nonsection element is found.

I thought that putting some structure into the XML would help me with some work I need to do at the group level.
And so I would like to be able to transform the above to this:

<data>
	<section>A..</section>
	<sectiongroup>
		<section type="1">B..</section>
		<section>C..</section>
		<section>D..</section>
	</sectiongroup>
	<sectiongroup>
		<section type="1">E..</section>
		<section>F..</section>
		<section>G..</section>
		<section>H..</section>
	</sectiongroup>		
	<nonsection>J..</nonsection>
	<nonsection>K..</nonsection>
</data>

Open in new window


And that would then enable me to 'do something' in sectiongroup.

Can anyone help with the XSLT that would transform the data to add in sectiongroup elements?
LVL 1
LetterpartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
If you can use XSLT2, this is easy

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    
    <xsl:output indent="yes"></xsl:output>
    
    <xsl:template match="data">
        <data>
            <xsl:for-each-group 
                select="*"
                group-starting-with="*[@type = 1 or name() = 'nonsection']"
                >
                <xsl:choose>
                    <xsl:when test="@type = 1">
                        <sectiongroup>
                            <xsl:copy-of select="current-group()"></xsl:copy-of>
                        </sectiongroup>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="current-group()"></xsl:copy-of>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </data>
     </xsl:template>
    
</xsl:stylesheet>

Open in new window


A lot harder for XSLT1, let me know if you need a XSLT1 solution

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gertone (Geert Bormans)Information ArchitectCommented:
Well, not that bad actrually,
but a bit of recursive processing on the following axis,
kind of hard to grasp

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:output indent="yes"></xsl:output>
    
    <xsl:template match="data">
        <data>
            <xsl:apply-templates select="*[1]" mode="start-group"/>
        </data>
     </xsl:template>
    
    <xsl:template match="section[@type = '1']" mode="start-group">
        <sectiongroup>
            <xsl:copy-of select="."/>
            <xsl:apply-templates select="following-sibling::*[1]" mode="in-group"/>
        </sectiongroup>
        <xsl:apply-templates select="following-sibling::*[@type = 1 or name() = 'nonsection'][1]" mode="start-group"/>
    </xsl:template>
    
    <xsl:template match="section[not(@type = '1')] | nonsection " mode="start-group">
        <xsl:copy-of select="."/>
        <xsl:apply-templates select="following-sibling::*[1]" mode="start-group"/>
    </xsl:template>
    
    <xsl:template match="section[not(@type = '1')]" mode="in-group">
        <xsl:copy-of select="."/>
        <xsl:apply-templates select="following-sibling::*[1]" mode="in-group"/>
    </xsl:template>
    
    <xsl:template match="section[@type = '1'] | nonsection " mode="in-group"/>
    
    
</xsl:stylesheet>

Open in new window

LetterpartAuthor Commented:
Excellent, thanks Geert.
The XSLT2 solution should be fine for me - but very interesting to compare them.
Thanks.
Gertone (Geert Bormans)Information ArchitectCommented:
welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.