Subject: RE: [xsl] algorithm for this hierarchy, anyone? From: "Ruggier, Mario" <Mario.Ruggier@xxxxxxxxxxxxxxxx> Date: Mon, 4 Feb 2002 15:54:15 +0100 |
Hi Joerg, thanks a lot for the sytlesheet you offer below, that works like a charm... For the extra questions you, please see my comments below. Best Regards. Mario Ruggier -- SoftPlumbers SA, 26 rue Maunoir, CH-1207 Genève Mobile +41.79.240.8636 | Office +41.22.849.1038 > From: Joerg Heinicke [mailto:joerg.heinicke@xxxxxx] > Sent: Monday, February 04, 2002 12:21 PM > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: Re: [xsl] algorithm for this hierarchy, anyone? > > Hello Mario, > > I have some questions about your XML: > 1. You have for example {"fr"}, {"fr","un"}, {"fr","un","doo"}and > "fr","un","doo","trois"}. Why storing one information/array multiple ties? This is an imposition of the input XML on which I have no control. The powers that be decided to indicate the location of each element in some hierarchy by writing out the categories outer -> last value, inner -> first value. The example I gave is a simplistic example just to give the gist (and maybe using langauge as outermost category is not the most representative of examples...) > 2. And is the language always the last? It's always a bit critical, if the > output depends on the order of the input nodes. In my eyes for example it's > better to put the language as an attribute to the <p.array>-node. As above, the last <v> defines the ost general category, and the langauge as exampel is only incidental. > 3. In theory you don't need the size of an array. You can count the elements > in eery <p.array>. No, array size is superfluous. But this is guaranteed to always be in the input XML, so may use it if it makes things easier. > Then my suggestion: You always have to select (apply-templates) the last <v> > in an array. And you must group the <p.array>s by it's last <v>. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <!-- grouping key --> <xsl:key name="arrays" match="p.array" use="v[last()]"/> <xsl:template match="r"> <xsl:copy> <!-- select unique <p.array>s, grouped by it's language (last <v>) --> <xsl:apply-templates select="obj/p.array[count( . | key('arrays',v[last()])[1] ) = 1]" mode="unique"/> </xsl:copy> </xsl:template> <xsl:template match="p.array" mode="unique"> <!-- sort all <p.array>s with this language --> <xsl:apply-templates select="key('arrays', v[last()])"> <!-- alternatively select="count(v)" if no @size-attribute --> <xsl:sort select="@size" order="descending"/> </xsl:apply-templates> </xsl:template> <xsl:template match="p.array"> <!-- select only the <p.array> of this language with the most entries --> <xsl:if test="position() = 1"> <!-- begin with the last <v>, the language --> <xsl:apply-templates select="v[last()]"/> </xsl:if> </xsl:template> <xsl:template match="v"> <obj name="{text()}"> <!-- select the <v> backwards for order 1, 2, 3 and so on --> <xsl:apply-templates select="preceding-sibling::v[1]"/> </obj> </xsl:template> </xsl:stylesheet> > You get your wanted output with this stylesheet. > > Regards, > > Joerg > > > Hello Mario, > > I answered this question already yesterday, but it seems that > this mail > hasn't gone over this list. I can't find my mail in the Mulberry mail > archive at sources.redhat.com, but it is in the ASPN archive: > > http://aspn.activestate.com/ASPN/Mail/Message/XSL-List/1012271 > > Does this happen often? Really strange! > > Joerg > > ----- Original Message ----- > From: "Ruggier, Mario" <Mario.Ruggier@xxxxxxxxxxxxxxxx> > To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> > Sent: Monday, February 04, 2002 12:02 PM > Subject: [xsl] algorithm for this hierarchy, anyone? > > > > Hello, > > > > I would like to convert the list of <obj> elements to > > the hierarchical XML output, as below, but am having > > problems identifying a workable algorithm. Can anyone > > suggest a simplest (or so ;-) algorithm for this? > > > > Mario Ruggier > > -- > > SoftPlumbers SA, 26 rue Maunoir, CH-1207 Genève > > Mobile +41.79.240.8636 | Office +41.22.849.1038 > > > > > > =================================== > > Input XML > > (<obj> elements may occur in any order) > > ("destination" depth of an <obj> is determined by the > > value of p.array/@size, and which could be anything) > > (no level jumps -- if there is a p.array[@size=n], then > > there will always be all intermediate occurances of > > p.array[size = n-1 ... 1 ], which will be composed of > > the corresponding number of "bottom" elements of p.array) > > > > <r> > > <obj> > > <p.array size="1"> > > <v>en</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="1"> > > <v>it</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="1"> > > <v>fr</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="2"> > > <v>one</v> > > <v>en</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="2"> > > <v>uno</v> > > <v>it</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="2"> > > <v>un</v> > > <v>fr</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="3"> > > <v>two</v> > > <v>one</v> > > <v>en</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="3"> > > <v>due</v> > > <v>uno</v> > > <v>it</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="3"> > > <v>doo</v> > > <v>un</v> > > <v>fr</v> > > </p.array> > > </obj> > > <obj> > > <p.array size="4"> > > <v>trois</v> > > <v>doo</v> > > <v>un</v> > > <v>fr</v> > > </p.array> > > </obj> > > </r> > > > > =================================== > > Desired Output > > (order of sibling <obj> elements is not important): > > > > <r> > > <obj name="en"> > > <obj name="one"> > > <obj name="two" /> > > </obj> > > </obj> > > <obj name="fr"> > > <obj name="un"> > > <obj name="doo"> > > <obj name="trois"/> > > </obj> > > </obj> > > </obj> > > <obj name="it"> > > <obj name="uno"> > > <obj name="due" /> > > </obj> > > </obj> > > </r> > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] algorithm for this hierar, Ruggier, Mario | Thread | [xsl] Ordered Records, Stefano Bonnin |
Re: [xsl] iterating over unstructur, W. Eliot Kimber | Date | Re: [xsl] for-each over variable pr, Joerg Heinicke |
Month |