Inquiry: Reordering XML elements through XSL

G. Ken Holman gkholman at CraneSoftwrights.com
Sat Jun 19 06:04:23 BST 1999


At 99/06/19 10:12 +0700, James Clark wrote:
>> At 99/06/18 18:34 -0700, Aaron Fischer wrote:
>> >However, this still leaves the reverse problem of translating from a tag
>> >order of AAABBB to ABABAB.
>
>A simpler approach is:
>
>perform the following on the document element "test"
>  for each a element
>    copy the element
>    let n be the position of this a element (ie this element is the n-th
>a element)
>    copy the n-th of the following b siblings

When I read this I thought this would only work when the number of <a>
elements is not less than the number of <b> elements, so I tried an example
below to confirm that.

Although Aaron's example has an equal number of elements, I tried to be
more general by handling an arbitrary number of either element (though I
don't know if this is his need or not).  The approach I took is based on a
numerical index rather than relying on the existence of either element.

Certainly if we stick to his original example, your code works, so I was
just playing outside the sandbox.

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


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

T:\fischer>type jclark3.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
                indent-result="yes">
<xsl:template match="test">
<test>
   <xsl:for-each select="a">
     <xsl:copy-of select="."/>
     <xsl:variable name="n" expr="position()"/>
     <xsl:copy-of select="from-following-siblings(b[$n])"/>
   </xsl:for-each>
</test>
</xsl:template>
</xsl:stylesheet>

T:\fischer>call xsl test3.xml jclark3.xsl test4.xml
T:\fischer>type test4.xml
<test>
<a name="kathryn"/>
<b name="ken"/>
<a name="kaitlyn"/>
<b name="alex"/>
</test>

T:\fischer>



--
G. Ken Holman                    mailto:gkholman at CraneSoftwrights.com
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
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