tags:

views:

28

answers:

2

Suppose i have an xml document like this

XML File:

<document>
  <educationalsection>
  educational details
  </educationalsection>

  <professionalsection>
  professional section details
  </professionalsection>
</document>

I have created XSL to converge it into my required format but the problem is if i want to change the order of the sections how can i do that? For instance if i want the professional section to come on top of educational without changing the xml, how is that possible? WHat will i need to add to my existing xsl or xml so that when my web application send the xml for transformation it can have different orders of elements as specified by the web app.

A: 

The xsl:apply-templates and xsl:for-each elements can have xsl:sort child elements that can be used to order the child nodes that were selected. Use different sort orders whenever you need to.

You can also use the mode attribute on xsl:template to select different templates using different sort orders.

Oded
any code examples for mode?
Sultan Saadat
@Sultan Saadat - examples of `mode`. These are not specific to sorting, but should give you the general idea. http://zvon.org/xxl/XSLTreference/OutputExamples/example_1_20_frame.html
Oded
i got the idea but my sections are different so i will have to use a section serial number kind of field to prioritize each section then reflect that in the output...
Sultan Saadat
@Sultan Saadat - Sorry, but I don't understand what you mean.
Oded
<educationalsection><id>1</id></educationalsection>now can i order on the id basis?
Sultan Saadat
@Sultan Saadat - that's not enough information. You can use any keyword in the `mode` attribute and in your `apply-templates` use it in conjunction with a specific `mode`. I would simply use different sorts as I first suggested.
Oded
@Sultan Saadat - I would rather you edit and update your question and add details to it. That's how we do things on StackOverflow.
Oded
ok then let me paste the entire XML file here.
Sultan Saadat
<personalinfo> <age id="1"> 26 </age> <name id="3"> sana sultan </name> <email id="2"> [email protected] </email> </personalinfo>Now i want the email to be on top of other elements when xslt output is seen. In plain words how will i do it?
Sultan Saadat
+1  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:param name="pOrder" select="'professionalsection,educationalsection'"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*">
                <xsl:sort select="string-length(
                                     substring-before(
                                        concat(',',$pOrder,','),
                                        concat(',',name(),',')))"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output:

<document>
    <professionalsection>
     professional section details
    </professionalsection>
    <educationalsection>
     educational details
    </educationalsection>
</document>
Alejandro
is this a dynamic solution? meaning you can set the order of sections automatically?
Sultan Saadat
@Sultan Saadat: You should pass your new order string as 'pOrder' parameter to the processor.
Alejandro
thankyou so much for your help :) it was really helpful! :)
Sultan Saadat
@Sultan Saadat: You are wellcome. I'm glad it was helpful.
Alejandro