xsl-list
[Top] [All Lists]

[xsl] Ordering in XSL:Apply-Templates

2010-10-25 04:24:21
Hi, List

I am using xslt2.0 with saxon processor for my xml to xml transformation. I like to ordering the element while applying the template, as shown below.

For example: Input XML element order <surname>, <given-names>. But I want <given-names>, <surname>. here order of the other elements should retains as it appear in input.

Note: In input XML, before and after the name element may come any element. How we can achieve this using xslt2.0?

Input XML:
========
<?xml version="1.0" encoding="UTF-8"?>
<contrib-group>
   <contrib contrib-type="author">
<name><surname>Carlisle</surname><given-names>Suzanna K.</given-names></name>
       <degrees>RN, BSN, CCRP</degrees>
   </contrib>
   <contrib contrib-type="author">
<name><surname>Vargas</surname><given-names>Perla A.</given-names></name>
       <degrees>PhD</degrees>
   </contrib>
   <contrib contrib-type="author">
<name><surname>Jones</surname><given-names>Stacie M.</given-names></name>
       <degrees>MD</degrees>
   </contrib>
</contrib-group>

XSLT :
=====
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
   <xsl:output encoding="us-ascii" indent="yes" method="xml"/>
<xsl:template match="contrib-group"><aug><xsl:apply-templates/></aug></xsl:template> <xsl:template match="contrib"><au><xsl:apply-templates/></au></xsl:template> <xsl:template match="surname"><surname><xsl:apply-templates/></surname></xsl:template> <xsl:template match="given-names"><forename><xsl:apply-templates/></forename></xsl:template> <xsl:template match="degrees"><qualifications><xsl:apply-templates/></qualifications></xsl:template>
</xsl:stylesheet>

Output XML:
=========
<?xml version="1.0" encoding="us-ascii"?>
<aug>
<au><surname>Carlisle</surname><forename>Suzanna K.</forename><qualifications>RN, BSN, CCRP</qualifications></au> <au><surname>Vargas</surname><forename>Perla A.</forename><qualifications>PhD</qualifications></au> <au><surname>Jones</surname><forename>Stacie M.</forename><qualifications>MD</qualifications></au>
</aug>

Expected Output:
============
<?xml version="1.0" encoding="us-ascii"?>
<aug>
<au><forename>Suzanna K.</forename><surname>Carlisle</surname><qualifications>RN, BSN, CCRP</qualifications></au> <au><forename>Perla A.</forename><surname>Vargas</surname><qualifications>PhD</qualifications></au> <au><forename>Stacie M.</forename><surname>Jones</surname><qualifications>MD</qualifications></au>
</aug>



===========================================================
***Disclaimer***


This email, and any attachments ("this email"), is confidential. If you are not 
the addressee please tell the sender immediately, and destroy this email without using, 
sending or storing it. Any opinions, express or implied, in this email, are those of the 
sender, and are not necessarily approved by the company.  Except as expressly stated, 
this e-mail should not be regarded as an offer, solicitation, recommendation or agreement 
to buy or sell products or services, or to enter into any contract. E-mail transmissions 
are not secure and may suffer errors, viruses, delay, interception and amendment. The 
company does not accept liability for damage caused by any of the foregoing.

ALL INCOMING AND OUTGOING MAILS MAY BE MONITORED BY THE COMPANY.
============================================================

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--