xsl-list
[Top] [All Lists]

[xsl] Grouping paragraphs and sections in XSLT 2.0

2011-12-08 07:02:13
Hi,

I've got some XML of the format shown below i.e. some number of
paragraphs followed by a section containing one or more paragraphs. This
pattern then repeats.

    <test>
        <paragraph id="a.1">a.one</paragraph>
        <paragraph id="a.2">a.two</paragraph>
        <paragraph id="a.3">a.three</paragraph>
        <section id="alpha">
            <paragraph id="alpha.1">one</paragraph>
            <paragraph id="alpha.2">two</paragraph>
            <paragraph id="alpha.3">three</paragraph>
        </section>
        <paragraph id="b.1">one</paragraph>
        <paragraph id="b.2">two</paragraph>
        <paragraph id="b.3">three</paragraph>
        <section id="beta">
            <paragraph id="beta.1">one</paragraph>
            <paragraph id="beta.2">two</paragraph>
            <paragraph id="beta.3">three</paragraph>
        </section>
        <paragraph id="b.1">one</paragraph>
        <paragraph id="b.2">two</paragraph>
        <paragraph id="b.3">three</paragraph>
    </test>

I need to be able to group the data as shown below - namely paragraphs
up until the next section in a separate group, followed by the section
in a separate group and then the pattern repeats :

<results>
    <GROUP>
        <paragraph id="a.1">a.one</paragraph>
        <paragraph id="a.2">a.two</paragraph>
        <paragraph id="a.3">a.three</paragraph>
    </GROUP>
    <GROUP>
        <section id="alpha">
            <paragraph id="alpha.1">one</paragraph>
            <paragraph id="alpha.2">two</paragraph>
            <paragraph id="alpha.3">three</paragraph>
        </section>
    </GROUP>
    <GROUP>
        <paragraph id="b.1">one</paragraph>
        <paragraph id="b.2">two</paragraph>
        <paragraph id="b.3">three</paragraph>
    </GROUP>
    <GROUP>
        <section id="beta">
            <paragraph id="beta.1">one</paragraph>
            <paragraph id="beta.2">two</paragraph>
            <paragraph id="beta.3">three</paragraph>
        </section>
    </GROUP>
    <GROUP>
        <paragraph id="b.1">one</paragraph>
        <paragraph id="b.2">two</paragraph>
        <paragraph id="b.3">three</paragraph>
    </GROUP>
</results>

My latest attempt is still not there:

    <xsl:template match="test">
        <xsl:for-each-group select="paragraph | section" 
                            group-starting-with="section">
            <GROUP>
                <xsl:copy-of select="current-group()"/>

            </GROUP>
        </xsl:for-each-group>            
    </xsl:template>

This results in the following, where the problem is that the paragraphs
following a section are included in that group. I'm not sure how to
correctly demark the end of a section so that it starts a new section
for the subsequent paragraphs.

<results>
    <GROUP>
        <paragraph id="a.1">a.one</paragraph>
        <paragraph id="a.2">a.two</paragraph>
        <paragraph id="a.3">a.three</paragraph>
    </GROUP>
    <GROUP>
        <section id="alpha">
            <paragraph id="alpha.1">one</paragraph>
            <paragraph id="alpha.2">two</paragraph>
            <paragraph id="alpha.3">three</paragraph>
        </section>

<!-- WANT THESE TO BE IN A NEW GROUP -->
        <paragraph id="b.1">one</paragraph>
        <paragraph id="b.2">two</paragraph>
        <paragraph id="b.3">three</paragraph>
    </GROUP>
    <GROUP>
        <section id="beta">
            <paragraph id="beta.1">one</paragraph>
            <paragraph id="beta.2">two</paragraph>
            <paragraph id="beta.3">three</paragraph>
        </section>

<!-- WANT THESE TO BE IN A NEW GROUP -->
        <paragraph id="b.1">one</paragraph>
        <paragraph id="b.2">two</paragraph>
        <paragraph id="b.3">three</paragraph>
    </GROUP>
</results>

Could anyone advise on a suitable approach for this problem? Thanks in
advance for any assistance.

Neil




This e-mail is for the sole use of the intended recipient and contains 
information that may be privileged and/or confidential. If you are not an 
intended recipient, please notify the sender by return e-mail and delete this 
e-mail and any attachments. Certain required legal entity disclosures can be 
accessed on our http://thomsonreuters.com/prof_disclosures.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>