xsl-list
[Top] [All Lists]

Re: XML to XML transformation

2003-04-15 00:50:27
Cheers for that Mike, it will be most useful.

Here is the more complex structure I want to create (data from source in the
[]).

<!-- <mdatas>
    <label>[from external reference]</label>
    <starttime>[ROWSET/first-child()/TIMESTAMP</starttime>
    <endtime>[ROWSET/last-child()/TIMESTAMP</endtime>
    [for-each-child() of ROWSET]
        <mdata>
            [if first-child()]
                <timestamp>[TIMESTAMP]</timestamp>
            [end-if]
            [for-each-child() && not(first-child())]
                <svalue>
                    <label>[local-name()]</label>
                    <value>[text()]</value>
                </svalue>
            [end-for]
        </mdata>
    [end-for]
</mdatas> -->

Since my last post I have created a xsl to do this (I think, as I can'T get
it to run yet from other bugs) but I'm not sure if I am using xsl correctly
to produce a Document rather than text.  It looks ok to me, but does seem a
little shoddy, but here it is:

"<xsl:stylesheet version="1.0"
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\";>"
"<xsl:output method="text"/>"
"<xsl:template match="/">"
"  <slowcontrol>"
"    <xsl:apply-templates />"
"  </slowcontrol>"
"</xsl:template>"
"<xsl:template match="ROWSET">"
"  <measuringdatasequence>"
"    <label>fp1v1</label>"
"    <xsl:for-each select="ROW">"
"      <xsl:choose>"
"        <xsl:when test="position() = 1">"
"          <starttime><xsl:value-of select="./TIMESTAMP/text()"
/></starttime>"
"        </xsl:when>"
"        <xsl:when test="position() = last()">"
"          <endtime><xsl:value-of select="./TIMESTAMP/text()" /></endtime>"
"        </xsl:when>"
"        <xsl:otherwise />"
"      </xsl:choose>"
"    </xsl:for-each>"
"    <xsl:apply-templates />"
"  </measuringdatasequence>"
"</xsl:template>"
"<xsl:template match="ROW">"
"  <measuringdata>"
"  <xsl:for-each select="*">"
"    <xsl:choose>"
"      <xsl:when test="string(local-name())=string('TIMESTAMP')">"
"        <timestamp><xsl:value-of select="./text()" /></timestamp>"
"      </xsl:when>"
"      <xsl:otherwise>"
"        <sensorvalue>"
"          <label><xsl:value-of select="local-name()" /></label>"
"          <value><xsl:value-of select="./text()" /></value>"
"        </sensorvalue>"
"      </xsl:otherwise>"
"    </xsl:choose>"
"  </xsl:for-each>"
"</xsl:template>"
"</xsl:stylesheet>"

Any help would be most appriciated.

Kind regards

Simon

----- Original Message -----
From: "Mike Brown" <mike(_at_)skew(_dot_)org>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Monday, April 14, 2003 9:29 PM
Subject: Re: [xsl] XML to XML transformation


Simon Kelly wrote:
I have been looking throught the xslt-cookbook to try to work out the
best
was of turning an flat XML document, in to quite a deep complex
document.
But I am having a couple of problems working out what it the best way of
doing this in xsl.

The flat structure is an output from a relational db, using ROWSET, ROW
then
$column-name as the child node identifiers.

You mean you have something like this?

<DATA>
  <ROWSET>
    <ROW>
      <Id>123</Id>
      <GivenName>Jane</GivenName>
      <FamilyName>Doe</FamilyName>
    </ROW>
    ...
  </ROWSET>
  ...
</DATA>

What's your definition of deep and complex, i.e. what is an example of the
XML you want in your output, given the above input?

However, I will be keeping none of the node names in the new structure,
and I want xsl to create a new document and not a string output.
I am using the javax.xml.transform.Transformer.

In XSLT, you create a new document in the form of an XPath node tree that
is
similar to a DOM Document object. Typically, this "result tree" is
serialized
as an encoded string after construction, but different XSLT processors
offer
alternative types of output, including DOM Document objects. Processors
that
implement JAXP may support the DOMResult class, which you can create an
instance of to be the destination for the result tree. You pass this
instance
as the second argument to the Transformer's transform() method. Read more
about
DOMResult in the javax.xml.transform docs. Your XSLT processor probably
comes
with examples demonstrating its usage.

When constructing the new result tree, you are free to create any nodes
you
want, and to traverse any part of the source tree, starting at the root
node.
Your new elements and attributes can be given any names you want; there's
no
obligation to copy the input, or to even base anything on the input at
all.

I also need the transform to be very quick, as the relational document
size
can be in excess of 4MB.

This will be more of a problem. Very large input files are difficult to
process because some nuances of XSLT require that the document be cached
and
operated on with the entire document being in memory at once. There are
various ideas for working around this, including using a SAX parser to
break
the document into chunks that the XSLT processor can handle more quickly
(and
then you have to collect and reassemble the output yourself),
vendor-specific
extension functions such as saxon:preview (but only in limited
situations),
and using SAX-based transformation approaches such as STX. See also
http://www.biglist.com/lists/xsl-list/archives/200301/msg00804.html

Mike

--
  Mike J. Brown   |  http://skew.org/~mike/resume/
  Denver, CO, USA |  http://skew.org/xml/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>