xsl-list
[Top] [All Lists]

Re: [xsl] Grouping data at multiple levels (can XSL do this?)

2011-06-28 16:17:31
You've described your problem in very procedural terms, so it's not surprising you are having trouble implementing it in a declarative language. Your readers including me will have the same problem - reverse engineering your requirements from an algorithm that doesn't work is not easy.

If you show some sample input and output then we may be able to recognize how they are related. If the relationship isn't obvious, please try and explain it. Usually if you express the problem in terms of rules like "this element in the output depends on these things in the input" then an implementation in a functional language can be derived from this without much difficulty.

Michael Kay
Saxonica

On 28/06/2011 19:47, Hank Ratzesberger wrote:
Hello,

I'm writing to this list for the first time because after using
XSL for some years I've come across a data pattern that I think
defies use by XSL.  It is the kind of thing that a language that
allows you to alter/update variables and iterate through lists
can do with relative ease, but I don't find it possible with
XSL.  It is an example of hierarchical meaning within flat
data, that is, there are groups of rows of data, but some
groups represent updates to information in prior groups.

In pseudo code, something like this

for each<receiver>
  if (the<receiver>  has a child<model-code>) then
   create the element<myReceiverHistory>  with all the children of
<receiver>
   for each following-sibling of receiver that does not have a
<model-code>  child
    create a new<myReceiverHistory>  by updating the most recent with
children info.

It is the kind of thing that loop construct with variables that
can be updated within the loop or that has a "continue" to
skip to the next, etc. can do.

Happy to be proved wrong or listen to your interest.

I can pull rows out of a database and put them into an xml format.
They are tidbits that, when sorted by date, create a group of changes
that occur on permanent GPS site. So, starting from this:

<result  xmlns="">
<row>
<site-transaction-type>frequency standard</site-transaction-type>
<site-transaction-name>type</site-transaction-name>
<site-transaction-value>INTERNAL</site-transaction-value>
<effective-date>1990-01-09T00:00:00.000</effective-date>
</row>
<row>
<site-transaction-type>frequency standard</site-transaction-type>
<site-transaction-name>additional information</site-transaction-name>
<site-transaction-value>ROGUE SNR-8 uses external rubidium standard</site-transaction-value>
<effective-date>1990-01-09T00:00:00.000</effective-date> </row>
<row>
...etc. etc.

I can use for-each-group to

<xsl:variable name="receivers">
<xsl:for-each-group select="$transactions/row[site-transaction-type='receiver']" group-by="effective-date">
<sopac:receiver xmlns:sopac="http://sopac.ucsd.edu/ns";>
<xsl:copy-of select="current-group()"/>
</sopac:receiver>
</xsl:for-each-group>
</xsl:variable>

Which allows me to put all the things together that make up the actual
transaction. The catch is that only when one of the rows is a
"site-transaction-name" with the value 'model code' does it represent a
change to the receiver.  Other changes, e.g. 'serial number' only
update the previous group of elements.  The above creates this:

<sopac:receiver  xmlns:sopac="http://sopac.ucsd.edu/ns";>
<row  xmlns="">
<site-transaction-type>receiver</site-transaction-type>
<site-transaction-name>model code</site-transaction-name>
<site-transaction-value>TRIMBLE 4000SST</site-transaction-value>
<effective-date>1990-02-09T00:00:00.000</effective-date>
</row>
<row  xmlns="">
<site-transaction-type>receiver</site-transaction-type>
<site-transaction-name>firmware version</site-transaction-name>
<site-transaction-value>4.11</site-transaction-value>
<effective-date>1990-02-09T00:00:00.000</effective-date>
</row>
<row  xmlns="">
<site-transaction-type>receiver</site-transaction-type>
<site-transaction-name>serial number</site-transaction-name>
<site-transaction-value>496</site-transaction-value>
<effective-date>1990-02-09T00:00:00.000</effective-date>
</row>
<row  xmlns="">
<site-transaction-type>receiver</site-transaction-type>
<site-transaction-name>satellite system</site-transaction-name>
<site-transaction-value>GPS</site-transaction-value>
<effective-date>1990-02-09T00:00:00.000</effective-date>
</row>
<row  xmlns="">
<site-transaction-type>receiver</site-transaction-type>
<site-transaction-name>additional information</site-transaction-name>
<site-transaction-value>First measurements</site-transaction-value>
<effective-date>1990-02-09T00:00:00.000</effective-date>
</row>
</sopac:receiver>
<sopac:receiver  xmlns:sopac="http://sopac.ucsd.edu/ns";>
<row  xmlns="">
<site-transaction-type>receiver</site-transaction-type>
<site-transaction-name>serial number</site-transaction-name>
<site-transaction-value>422</site-transaction-value>
<effective-date>1990-02-14T00:00:00.000</effective-date>
</row>
<row  xmlns="">
<site-transaction-type>receiver</site-transaction-type>
<site-transaction-name>sample interval</site-transaction-name>
<site-transaction-value>30</site-transaction-value>
<effective-date>1990-02-14T00:00:00.000</effective-date>
</row>
<row  xmlns="">
<site-transaction-type>receiver</site-transaction-type>
<site-transaction-name>additional information</site-transaction-name>
<site-transaction-value>Site ties 05-MAR-1990</site-transaction-value>
<effective-date>1990-02-14T00:00:00.000</effective-date>
</row>
</sopac:receiver>

...

Even creating a variable with the necessary hierarchy is problematic
because I can't go back to get the data -- or maybe I can with
a tricky previous-sibling ?

Thanks,
Hank





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--