xsl-list
[Top] [All Lists]

Re: [xsl] Taking flat XML and parsing into multi level nexted

2007-08-08 02:44:45
Hi Paul,

This is a general grouping problem of the form flat-to-hierarchical.
Doing a google on those terms shows this excellent thread in the
archives, with many solutions:
http://www.biglist.com/lists/xsl-list/archives/200701/msg00150.html
(the archive messes up threads terribly if you browse them, but the
link above and the follow-ups still belong to the same thread and
all are very good approaches)

If you have to do it in XSLT 1.0, check Jeni Tennison's pages, who
explains the Muenchian Method (named after Muench who first invented
it) for grouping.

Instead of Muenchian, if you really *have* to stick to XSLT 1.0,
considering the kind of problem you have, a tree-walking algorithm
may suit you better (take the first of a certain kind and then
process the siblings that match a certain pattern etc).

BTW, you talk of opening an closing *tags*. It is a good thing to
change your mindset about XML when "thinking XSLT" and to start
thinking of nodes (a node is an atomic piece that serializes to a
closing and ending tag, or a closing and ending comment-tag or a
text-node (no closing/ending) or an attribute (including =-sign and
quotes) etc). Once you get that right and the word "tag" does not
appear in your head anymore when doing XSLT, you will find yourself
understanding and coding XSLT way more easily.

BTW2: your problem is not trivial and the methods presented require
a little knowledge of XSLT, and in XSLT 1.0, they require a lot of
knowledge of XSLT. If you are unfamiliar with it, I recommend
reading Jeni Tennison's introductory books or the introductory
chapters in Michael Key's reference books.

BTW3: if you find yourself coding XSLT 1.0 now, consider moving to
XSLT 2.0. You'll never regret making that move, it's a large benefit
in many ways ;)

BTW4: try not to nest with nesting xsl:for-each. Instead, try to
find a generic (template matching) approach, otherwise your nesting
level is equal to the depth of the output and that will end up
horribly unmaintainable and unflexible.


Cheers,
-- Abel Braaksma


I have some horrible pre-generated source XML which is in this form:

<item>Item Name One</item>
<categoryStart>Category Name One</categoryStart>
<item>Item Name Two</item>
<item>Item Name Three</item>
<categoryStart>Category Name Two</categoryStart>
<item>Item Name Four</item>
<categoryEnd>Category Name Two</categoryEnd>
<item>Item Name Five</item>
<categoryEnd>Category Name One</categoryEnd>
<item>Item Name Six</item>

Now, in the destination XML, the categories are also items, which
just
indicate another level of nesting, and so the above needs to be
transformed to something along these lines:

<item>
    <title>Item Name One</title>
</item>
<group>
    <title>Category Name One</title>
    <item>
        <title>Item Name Two</title>
    </item>
    <item>
        <title>Item Name Three</title>
    </item>
    <group>
            <title>Category Name Two</title>
            <item>
                <item>Item Name Four</item>
            </item>
    </group>
    <item>
        <title>Item Name Five</title>
    </item>
</group>
<item>
    <title>Item Name Five</title>
</item>



--~------------------------------------------------------------------
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>
--~--