This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Flat file to hierarchy?


Allison,

Yours is a version of a semi-FAQ, which you will find addressed at http://www.dpawson.co.uk/xsl/sect2/flatfile.html .

Actually, recursion isn't the easiest solution. It's to "prewire" the hierarchy you want with keys. It's easiest to think of the wiring from bottom up. It sounds from your description like your lowest level is C, which are directly inside their most-previous A (or possibly P, whichever is more recent). This relation can be established with

<xsl:key name="Records-by-parent" match="Record[F1='C']"
use="generate-id(preceding-sibling::Record[F1='A' or F1='P'][1])"/>

The way this key works is, if I give it the value of a string, it will return for me all C records that "belong to" (are intended to be children of) any P or A record whose generated unique ID equals my string.

Likewise,

<xsl:key name="Records-by-parent" match="Record[F1='A']"
use="generate-id(preceding-sibling::Record[F1='P'][1])"/>

gets me back A records from the ID of a P record.

I build my hierarchy by pulling out all my P records, then inside each P record's element, getting all the A and C records that belong to it (using the ID of the P record as the key to retrieve them), then inside the As, getting all the C records that belong to them.

Sort of recursion inside out, if you like.

I hope between this synopsis and the FAQ (more code), you can see what you have to do. If not, ask again.

Good luck,
Wendell

At 03:37 PM 6/10/2002, you wrote:

Hello,

I have a flat file that needs to get turned into a hierarchy based on the
content of a certain element.

If the content of the element is P, a top level element should be created.
If content is C, second level; A third level.  Each content=P restarts the
leveling process--ie, each A or C will only be a child of the immediate
previous P.  As and Cs can follow Ps in any order, but if As follow a C they
are a child of that C until another C or P occurs.  (Please see the sample
data below.)

The answer must be recursion, but could anyone help show me how to generate
this?  In a flat file I'm not sure how to test so that As and Cs will be
children only of the immediately preceding P.

Thanks in advance,
Allison Denny

Sample input:
<RECORDS>
        <RECORD>
                <F1>P</F1>
                <F2>21142</F2>
                <F3>19300</F3>
                <F4>Congressional Committees</F4>
        </RECORD>
        <RECORD>
                <F1>C</F1>
                <F2>26102</F2>
                <F3>19400</F3>
                <F4>General Accounting Office</F4>
        </RECORD>
        <RECORD>
                <F1>A</F1>
                <F2>26104</F2>
                <F3>19400</F3>
                <F4/>
                <F5>GAO/OIMC/OPA-00-1A</F5>
                <F6>26104-17.1</F6>
                <F7>L</F7>
        </RECORD>
        <RECORD>
                <F1>A</F1>
                <F2>26104</F2>
                <F3>19400</F3>
                <F4/>
                <F5>GAO/OIMC/OPA-00-1B</F5>
                <F6>26104-17.2</F6>
                <F7>L</F7>
        </RECORD>
        <RECORD>
                <F1>P</F1>
                <F2>17632</F2>
                <F3>18000</F3>
                <F4>U.S. National Alcohols Fuels Commission</F4>
        </RECORD>
</RECORDS>

Sample output:
<SUDOC-INDEX>
        <AGENCY-GROUP>
                <AGENCY-GROUP-HEAD>Congressional
Committees</AGENCY-GROUP-HEAD>
                <AGENCY-GROUP-LEVEL-TWO>
                        <AGENCY-LEVEL-TWO-HEAD>General Accounting
Office</AGENCY-LEVEL-TWO-HEAD>
                        <AGENCY-RPT-NO-ENTRY>

<AGENCY-RPT-NO>GAO/OIMC/OPA-00-1A</AGENCY-RPT-NO>
                                <ACCESSION-NO-GROUP>
                                        <ISSUE-NUMBER>L</ISSUE-NUMBER>

<ACCESSION-NO>26104-17.1</ACCESSION-NO>
                                </ACCESSION-NO-GROUP>
                        </AGENCY-RPT-NO-ENTRY>
                        <AGENCY-RPT-NO-ENTRY>

<AGENCY-RPT-NO>GAO/OIMC/OPA-00-1B</AGENCY-RPT-NO>
                                <ACCESSION-NO-GROUP>
                                        <ISSUE-NUMBER>L</ISSUE-NUMBER>

<ACCESSION-NO>26104-17.2</ACCESSION-NO>
                                </ACCESSION-NO-GROUP>
                        </AGENCY-RPT-NO-ENTRY>
                </AGENCY-GROUP-LEVEL-TWO>
        </AGENCY-GROUP>
        <AGENCY-GROUP>
                <AGENCY-GROUP-HEAD>U.S. National Alcohols Fuels
Commission</AGENCY-GROUP-HEAD>
                Etc...
</SUDOC-INDEX>

======================================================================
Wendell Piez                            mailto:wapiez@mulberrytech.com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]