Re: [xsl] Transform XML to CSV with XSLT pipeline

Subject: Re: [xsl] Transform XML to CSV with XSLT pipeline
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 17 Sep 2010 09:13:16 +0100
On 16/09/2010 11:20 PM, Jesper Tverskov wrote:
I have a question to the list about the "equalizing" step stylesheet
(see tutorial), the step that adds missing optional elements. Is there
a better way to do it? My "equalizing" stylesheet works as follows
(flattening has already taken place):



It's difficult to tell whether there's a better way of doing it without a specification of what you are doing. It's not clear to me what you want to happen if for example one element has children (a,b,c) and another has children (c,a,b).


Let's suppose we only want to handle inputs in which all elements have a sequence of children that is a projection of some sequence S, and the requirement is to find the minimum sequence S for which this is true. Here's an approach to solving this.

First, find the set of ordered pairs of adjacent child elements. For example if the subsequences present are (a,b,c,z) and (a,b,d,z) this is (ab, bc, cz, bd, dz). Then apply the following algorithm recursively:

1. find a name that has not already been output that does not apear as the second of a pair of which the first name has not yet been output.

2. output this name

3. repeat until no further names are found

There's an error condition that needs detecting here if there is no partial ordering.

Translating this into XSLT terms is something I leave as an excercise for the reader! As often, the challenge is to decide what data structure to use. I think that for the "set of ordered pairs" I would probably use two parallel sequences of xs:QName values.

To allow for repeating elements, perhaps (a,b,c,c,d) should be renamed as (a1,b1,c1,c2,d1).

Michael Kay
Saxonica

Current Thread