[quoted text, click to view] Cerebrus99 wrote:
> I get your point. I need to use XSLT to sort the XML and then copy the
> result back. I cannot sort it in-place. That really clears up my
> doubts. Actually I wasn't aware that I could use XSLT to transform XML
> into another XML structure.
That's XSLT's primary purpose...T for Transformations.
[quoted text, click to view] > I will however have to study deeper as to
> how to implement that. I am using .NET Framework 1.1. Since my XSLT
> isn't very strong, could you suggest any links that give a step by step
> guide on how to do this.(Copy an XML file to another). I'm still trying
> to understand the sample code you've attached, Peter. :-(
I can't help with .NET, I'm afraid, but as for the code:
[quoted text, click to view] > <xsl:template match="*">
> <xsl:copy>
> <xsl:for-each select="@*">
> <xsl:copy/>
> </xsl:for-each>
> <xsl:apply-templates/>
> </xsl:copy>
> </xsl:template>
This is an "identity transform" template: it matches all element nodes
which are not matched by any other template (the * is the wildcard),
and copies them exactly as they stand into the destination tree. In
each element thus processed, it then loops through any attributes and
copies them to the destination tree as well. You aren't using any
attributes in your example, but just in case. Finally it applies any
matching template to any child element nodes.
[quoted text, click to view] > <xsl:template match="BOOKDATA">
> <xsl:copy>
> <xsl:for-each select="@*">
> <xsl:copy/>
> </xsl:for-each>
> <xsl:for-each select="BOOK">
> <xsl:sort select="AUTHOR"/>
> <xsl:apply-templates select="."/>
> </xsl:for-each>
> </xsl:copy>
> </xsl:template>
This template matches a BOOKDATA element node. It again copies itself
to the destination tree, plus its attributes, but then it goes through
each BOOK element within it, sorts them by AUTHOR (you could specify a
different element type name if needed), and then performs an
"apply-templates" on itself ("." refers to the current context, in this
case each BOOK element node as it is handled), which makes the processor
search for a matching template...which in all cases will be matched by
the * template above.
The effect is to output everything as-is except that the BOOK elements
will be serialized (written out) in the sorted order.
It's probably a lousy way to sort the data in an XML document, but it
works.
[quoted text, click to view] > As to the size of the XML file, I presume that it will grow to a
> maximum of 1-2 MB.
That's pretty small, but it will take a measurable number of seconds
to process if there's the overhead of running XSLT afresh each time.
[quoted text, click to view] > I figure that would not impose serious performance
> implications to discourage the use of XML.
Not to discourage XML, but perhaps to discourage using XSLT to sort a
file repetitively in real time at the kind of rate you seem to be
indicating.
The real question is, why do you want to sort the file each time? If
it's so that the data appears sorted when the next access comes up,
that could be handled in many other different ways: it doesn't have
to be done by physically re-sorting the XML on disk afresh each time.
///Peter
--