Re: Creating an xml doc from another xml doc via xsl

Subject: Re: Creating an xml doc from another xml doc via xsl
From: "Steve Muench" <smuench@xxxxxxxxxxxxx>
Date: Sun, 9 Apr 2000 11:38:41 -0700
Matthew,

Here you're wanting to use XSLT to do grouping of the data
to factor out the course info which is "flat" in the
result set. You can check out the XSLT FAQ for pointers
on techniques for grouping using the 1.0 facilities provided
by XSLT, but none of the pure-XSLT-1.0 solutions (even the
most elegant ones by David Carlise) could be called pretty
and surely won't be very performant over large XML documents
since the techniques involve rescanning lists of previous
sibling nodes to detect whether a thing you want to group
on has occurred before or not. The Saxon XSLT engine by
Mike Kay offers a <saxon:group> extension to help out with
the job, but since you're already using a database, and assuming
you will have more than two students in one course in
your real-world example, then why not let the database do
the grouping  work for you and then use XSLT to just convert
the format.

I assume your query is:

  SELECT name,age,course
    FROM course_assignments

If instead you use your database's data-shaping capabilities
and grouping capabilities to do a query like:

  SELECT course, CURSOR(SELECT name,age
                          FROM course_assignments b
                         WHERE b.course = a.course
                       ) AS students
    FROM course_assignments a
GROUP BY course

Then you'll get results like:

<rowset>
   <row num="1">
      <course>cos-399</course>
      <students>
         <students_row num="1">
            <name>matt</name>
            <age>23</age>
         </students_row>
         <students_row num="2">
            <name>joe</name>
            <age>22</age>
         </students_row>
      </students>
   </row>
</rowset>

Which can be then easily converted into your desired
format by applying the XSLT stylesheet:

<courses xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xsl:version="1.0">
  <xsl:for-each select="/rowset/row">
    <course title="{course}">
      <xsl:for-each select="students/students_row">
        <student>
            <name><xsl:value-of select="name"/></name>
            <age><xsl:value-of select="age"/></age>
        </student>
      </xsl:for-each>
    </course>
  </xsl:for-each>
</courses>

This is done without tons of rescanning of nodes and without
taxing the XSLT engine to do the sorting that the database
has efficient indexes to do for you.

If you glue these two things together using an XML/SQL/XSLT template
technology like Oracle XSQL Pages, you can create an XSQL Page like:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="matt.xsl"?>
<!-- matt.xsql -->
<xsql:query tag-case="lower" connection="demo" xmlns:xsql="urn:oracle-xsql" >

  select course,cursor(select name,age
                         from course_assignments b
                        where b.course = a.course
                      ) as students
    from course_assignments a
  group by course

</xsql:query>

Then use the XSQL Servlet or XSQL Command Line utility like:

  $ xsql matt.xsql

to automate producing your desired SQL-based XML format...

<?xml version = '1.0' encoding = 'UTF-8'?>
<courses>
  <course title="cos-399">
    <student>
      <name>matt</name>
      <age>23</age>
    </student>
    <student>
      <name>joe</name>
      <age>22</age>
    </student>
  </course>
</courses>

In short, if you have a database, don't forget it's
great at doing the sorting and grouping when datasets
get large rather than relying on in-memory sorting
and grouping that can be done by an XSLT 1.0-compliant
XSLT processor.

Have fun.

______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
Business Components for Java & XSQL Servlet Development Teams
Oracle Rep to the W3C XSL Working Group
----- Original Message -----
From: "Matthew Cordes" <mcorde61@xxxxxxxxx>
To: <perl-xml@xxxxxxxxxxxxxxxxxxxxxxxx>; <cocoon-users-help@xxxxxxxxxxxxxx>;
<xsl-list@xxxxxxxxxxxxxxxx>
Sent: Sunday, April 09, 2000 10:53 AM
Subject: Creating an xml doc from another xml doc via xsl


| I know it is possible, but I've yet to see any examples on how to create an
| xml document from an xml document and a stylesheet.  For instance, I have
| this xml from a database:
|
| <resultset>
|    <result>
|         <name> Matt </name>
|         <age>    23 </age>
|         <course> cos-399 </course>
|     </result>
|     <result>
|         <name> Joe </name>
|         <age>    22 </age>
|         <course> cos-399 </course>
|     </result>
| </resultset>
|
| and I want to transform it to be
|
| <courses>
|     <course title="cos-399">
|         <student>
|             <name> Matt </name>
|             <age>    23 </age>
|         </student>
|         <student>
|             <name> Joe </name>
|             <age>    22 </age>
|         </student>
|     </course>
| </courses>
|
|
| Can anyone point out some exmaples?  Or explain how to do this?
|
| -matt
|
|
|  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