Inquiry: Reordering XML elements through XSL

G. Ken Holman gkholman at CraneSoftwrights.com
Sat Jun 19 04:45:46 BST 1999


At 99/06/18 18:34 -0700, Aaron Fischer wrote:
>My XML documents consist of two basic tag blocks, <A></A> and <B></B>.
>They can either be interwoven, i.e. <A></A><B></B><A></A><B></B>, or
>separated into sections, i.e. <A></A><A></A><B></B><B></B>.

Fine ... I'll assume the interweaving and separation you desire is in
document order of the original in each case.

>I know I can apply a cludge to solve the above problem.  Namely, use
>element omission and make two XSL sweeps of the same document.  Run one XSL
>script that will output only <A> tags, another script that will output <B>
>tags, then concatenate the two output files.

No kludge necessary ... just make two passes of the source tree (visiting
only the nodes you desire) in a single stylesheet.

>However, this still leaves the reverse problem of translating from a tag
>order of AAABBB to ABABAB.  I'm not sure how this problem could be solved
>without the use of some direct reordering functionality.

Again, I'll assume you want to preserve the document order of the input in
the elements of the output ... if you want another order than document
order, the example below won't show that.  I've added attributes to the
elements to illustrate the ordering.

I've also illustrated using different numbers of A and B, though it also
works with the identical number.

I hope this helps.

............ Ken


T:\fischer>type test.xml
<?xml version="1.0"?>
<test>
<a name="ken"/>
<b name="kathryn"/>
<a name="alex"/>
<b name="kaitlyn"/>
<a name="ted"/>
<a name="john"/>
</test>
T:\fischer>type test1.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
                indent-result="yes">

<xsl:template match="/*">                       <!--document element-->
  <xsl:copy>
    <xsl:copy-of select="@*"/>                     <!--preserve atts-->
      <xsl:copy-of select="//a"/>              <!--preserve elements-->
      <xsl:copy-of select="//b"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

T:\fischer>call xsl test.xml test1.xsl test1.xml
T:\fischer>type test1.xml
<test>
<a name="ken"/>
<a name="alex"/>
<a name="ted"/>
<a name="john"/>
<b name="kathryn"/>
<b name="kaitlyn"/>
</test>

T:\fischer>type test2.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
                indent-result="yes">

<xsl:template match="/*">                       <!--document element-->
  <xsl:copy>
    <xsl:copy-of select="@*"/>                     <!--preserve atts-->
    <xsl:call-template name="do-rest"/>
  </xsl:copy>
</xsl:template>

<xsl:template name="do-rest">
  <xsl:param-variable name="index" expr="1"/>   <!--init'd only once-->
  <xsl:if test="//a[$index] | //b[$index]">
    <xsl:copy-of select="//a[$index]"/>        <!--preserve elements-->
    <xsl:copy-of select="//b[$index]"/>
    <xsl:call-template name="do-rest">                   <!--recurse-->
      <xsl:param name="index" expr="$index + 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

T:\fischer>call xsl test1.xml test2.xsl test2.xml
T:\fischer>type test2.xml

<test>
<a name="ken"/>
<b name="kathryn"/>
<a name="alex"/>
<b name="kaitlyn"/>
<a name="ted"/>
<a name="john"/>
</test>

T:\fischer> 

--
G. Ken Holman                    mailto:gkholman at CraneSoftwrights.com
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/x/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Publications:   Introduction to XSLT (3rd Edition) ISBN 1-894049-00-4


xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)





More information about the Xml-dev mailing list