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

[xsl] Getting sibling nodes from unique values


Subject: [xsl] Getting sibling nodes from unique values
From: Mike Ferrari <mikeferrari8@xxxxxxxxx>
Date: Tue, 4 Aug 2009 10:20:39 -0500

Hi
I am using the oxygen xml editor (its quite nice), xsl 2.0 and the
Saxon B 9.0.0.6 parser (which is really awesome).
I am trying to figure out how to generate multiple result documents
from one xml file. The xml file was saved as "xml data" from a excel
spreadsheet. The xml data has repetitive data in it, i need to use
unique values to generate a document for each unique value and put
information relevant to each unique value into the document.
Essentially i have a list with company info, and there are multiple
entries for each company, but the people and account numbers are
different for each entry.


My sample data looks something like this...
(dummy.xml)
<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <row>
        <company>Tree House Int</company>
        <account_num>2823994756</account_num>
        <representative>Bob Schafer</representative>
        <type>supplier</type>
    </row>
    <row>
        <company>Whatola</company>
        <account_num>567123456</account_num>
        <representative>Jane Adamms</representative>
        <type>manufacturer</type>
    </row>
    <row>
        <company>D+J Watson Ent</company>
        <account_num>456734</account_num>
        <representative>Janet Lerpray</representative>
        <type>supplier</type>
    </row>
    <row>
        <company>Whatola</company>
        <account_num>1111114</account_num>
        <representative>Elouise Peabody</representative>
        <type>manufacturer</type>
    </row>
    <row>
        <company>Big Flowers Industrial</company>
        <account_num>539862553</account_num>
        <representative>Rocky Cuccini</representative>
        <type>supplier</type>
    </row>
    <row>
        <company>Big Flowers Industrial</company>
        <account_num>53756647</account_num>
        <representative>Carmen Cuccini</representative>
        <type>supplier</type>
    </row>
</Root>

I have 4 different companies, Big Flowers Industrial and Whatola are multiple.

An example of one of my output reports needs to look like this...
(Whatola.xml)
<Root>
    <row>
        <company>Whatola</company>
        <account_num>567123456</account_num>
        <representative>Jane Adamms</representative>
        <type>manufacturer</type>
    </row>

    <row>
        <company>Whatola</company>
        <account_num>1111114</account_num>
        <representative>Elouise Peabody</representative>
        <type>manufacturer</type>
    </row>
</Root>

Ok, now that thats out of the way :-) I was able to produce some xsl
that output one xml file named after each unique value (Whatola.xml,
Big Flowers Industrial.xml, D+J Watson Ent.xml, Tree House Int.xml)
and insert the value of "." into them.

(report_gen.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:for-each select="distinct-values(//company)">
            <xsl:variable name="cmpname" select="." />
            <xsl:variable name="filename" select="concat($cmpname,'.xml')" />
            <xsl:value-of select="$filename"/>
            <xsl:result-document href="{$filename}">
                <xsl:value-of select="."/>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Now the hard part :-)
I want to take this to the next level, i need to generate the xml
output as listed in the whatola.xml example. The /Root/row/company and
account_num and representative and type...all into the result document
for each company. I need to take the values of these nodes and
surround them with tags, or get the node itself.

I am a noob with this, so it seems a bit foreign :-)

I think i am running into issues here because the <row> has no unique
identifier.. or the company name isn't an attribute at the row level.
I don't think i can change that unfortunately. I need to select the
entire row as a group, keying off of the <company> node in that <row>.
I think because i select the distinct values at the <company> level..
i can't get at the sibling nodes.. i cant just <xsl:value-of
select="representative/>. I tried to set up a variable for the path to
the node <representative> and use the variable in the result
document... but that gave me every value of every <representative>...
with no relation to the unique value.

Any suggestions? a direction to go? I am sure someone has done
something like this before, i just cant find it on the net. :-)


Current Thread
Keywords