xsl-list
[Top] [All Lists]

RE: [xsl] Generic stylesheet to flatten XML hierarchy

2009-12-04 14:37:58
Sorry, I hit Send too quickly. 

With input like this: 
<rss ...some attributes>
  <channel ...some attributes>
   <title>value may be mixed</title>
   <link>value</link>
   ...more siblings that do not repeate
   <item>
    <title>value</title>
    <description> mixed content</description>
    <link>value</link>
    ...more descendants
  </item>
  <item> ...</item>
  ...more items
  </channel>
</rss> 

I would like XML output like this: 

<root>
<row>
  <rss-attr1>value</rss-attr1>
  ...more rss/@* as elements
  <channel-attr*>value</channel-attr*>
  ...more channel/@*
  <channel-title>value or perhaps CDATA surrounding mixed 
content</channel-title>
  <channel-link>value</channel-link>
  ... all other non-repeating siblings in order as elements
  <item-title>value for first item</item-title>
  <item-description> probably CDATA around mixed content</item-description>
  ...all other item fields in order traversing depth-first
</row>
<row>...again rss attributes, channel attributes, non-repeating children of 
channel followed by fields for second item </row>
...more rows ...
</root> 

This example is for a single level of repeating descendants, but my solution 
has to be able to handle any level of repeating descendants. More over, the 
stylesheet has no knowledge of the structure of the input document. 

I have a solution that works ok by traversing the input document in doc order 
-- but it does not handle the siblings of repeating nodes that are not 
themselves repeating. 

I have thought of doing this the opposite way, get a key of all repeating nodes 
and process only those at the lowest depth to generate rows.  I haven't 
actually written the logic. 

Any better ideas would be welcome.
--- On Thu, 12/3/09, Michael Kay <mike(_at_)saxonica(_dot_)com> wrote:

From: Michael Kay <mike(_at_)saxonica(_dot_)com>
Subject: RE: [xsl] Generic stylesheet to flatten XML hierarchy
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Date: Thursday, December 3, 2009, 3:54 PM
I've been wandering through the
archives and other searches 
and so far have not found exactly what I am hoping to
find. I 
need to write an XSLT stylesheet that accepts an
unknown XML 
document and flattens the hierarchy to a
two-dimensional 
row/columns structure. 


The hard part of the problem is specifying what you want to
happen. Once
you've done that, I would think that coding it is fairly
easy.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 



--~------------------------------------------------------------------
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>
--~--






--~------------------------------------------------------------------
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>
--~--