[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Assembling nodes from parts outside of the current node


Subject: [xsl] Assembling nodes from parts outside of the current node
From: "Mark Peters" <flickrmeister@xxxxxxxxx>
Date: Thu, 25 Oct 2007 12:01:47 -0400

Hi Everyone,

This one has me stumped. I have a collection of nodes that store the
names of insects and include sub-nodes that map to the parts of
insects, stored elsewhere in the document.

<?xml version=" 1.0" encoding="UTF-8"?>
<kingdoms>
    <kingdom>
        <insects>
            <insect>
                <name>Bee</name>
                <partMaps>
                    <partMap mapId="pmap01" partId="part01"/>
                    <partMap mapId="pmap01" partId="part02"/>
                </partMaps>
             </insect>
            <insect>
                <name>Caterpillar</name>
                <partMaps>
                    <partMap mapId="pmap02" partId="part01"/>
                </partMaps>
            </insect>
            <insect>
                <name>Butterfly</name>
                <partMaps>
                    <partMap mapId="pmap03" partId="part01"/>
                    <partMap mapId="pmap01" partId="part02"/>
                </partMaps>
            </insect>
        </insects>
        <allParts>
            <part id="part01" name="legs" description="Walks with these"/>
            <part id="part02" name="wings" description="Flies with these"/>
        </allParts>
    </kingdom>
</kingdoms>


What I'm trying to do is list each insect and its parts in a series of
nodes, as follows. For each insect, I only want to display the parts
that pertain to that insect.


<?xml version="1.0" encoding="UTF-8"?>
<insects>
    <insect>
        <name>Bee</name>
        <parts>
            <part>
                <name>Legs</name>
                <description>Walks with these</description>
            </part>
            <part>
                <name>Wings</name>
                <description>Flies with these</description>
            </part>
        </parts>
    </insect>
    <insect>
        <name>Caterpillar</name>
        <parts>
            <part>
                <name>Legs</name>
                <description>Walks with these</description>
            </part>
        </parts>
    </insect>
    <insect>
        <name>Butterfly</name>
        <parts>
            <part>
                <name>Legs</name>
                <description>Walks with these</description>
            </part>
            <part>
                <name>Wings</name>
                <description>Flies with these</description>
            </part>
        </parts>
    </insect>
</insects>


I thought I'd simply need to compare the partMap/@partId values with
the corresponding part/@id values, as follows. The output currently
displays the insects, but not their parts. (We could argue that if
there are no parts, there are no objects. But that's another
discussion. :-)


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" version=" 1.0"/>
    <xsl:template match="/">
        <insects>
            <xsl:for-each select="kingdoms/kingdom
/insects/insect">
                <xsl:sort select="name"/>
                <insect>
                    <name>
                        <xsl:value-of select="name"/>
                    </name>
                    <xsl:for-each select="partMaps/partMap">
                        <xsl:choose>
                            <xsl:when
test="@partId=:../../../allParts/part/@id">
                                <part>
                                    <name>
                                        <xsl:value-of
select=":../../../allParts/part/@name"/>
                                    </name>
                                    <description>
                                        <xsl:value-of
select=":../../../allParts/part/@description"/>
                                    </description>
                                </part>
                            </xsl:when>
                            <xsl:otherwise/>
                        </xsl:choose>
                    </xsl:for-each>
                </insect>
            </xsl:for-each>
        </insects>
    </xsl:template>
</xsl:stylesheet>


By the way, I'm not an entomologist. Insects probably don't belong
directly within kingdoms. They're probably a genus or species or
something. :-)  This is just test data.

Thanks in advance for any help you could offer.

Mark

-- 

Mark Peters
Senior Technical Writer
Saba Software


Current Thread