Reputation: 327
I want to change the order that the elements are outputted in. It currently displays them like this: "Maths:English:Science:ABA (GCSE);(GCSE);(GCSE);" I need a way of ordering it so that I can display it like this: "Maths:A (GCSE); English:B (GCSE); Science:A (GCSE);" I am a novice with XML so please try not to show any overcomplicated solutions if possible!
XSL Code:
<xsl:template match="education">
<div style="float:left;">
<xsl:apply-templates select="qualifications/qual"/>
<xsl:apply-templates select="qualifications/grade"/>
<xsl:apply-templates select="qualifications/level"/>
</div>
</xsl:template>
<xsl:template match="qual"><span style="color:grey; font-size:15px; font-family:verdana;">
<xsl:value-of select="."/></span><p1>:</p1></xsl:template>
<xsl:template match="grade"><span style="color:grey; font-size:15px; font-family:verdana;"><xsl:value-of select="."/></span><p1> </p1></xsl:template>
<xsl:template match="level"><p1> (</p1><span style="color:grey; font-size:15px; font-family:verdana;"><xsl:value-of select="."/></span><p1>);</p1></xsl:template>
XML Code:
<qualifications>
<qual>Mathematics</qual> <grade>A</grade> <level>GCSE</level>
<qual>English</qual> <grade>B</grade> <level>GCSE</level>
<qual>Science</qual> <grade>A</grade> <level>GCSE</level>
</qualifications>
Upvotes: 4
Views: 49
Reputation: 60414
You're first applying templates to all the qual
children, then each grade
, then each level
and getting exactly the output you should expect from that. Instead, simply process the children in order inside your education
template:
<xsl:template match="education">
<div style="float:left;">
<xsl:apply-templates select="qualifications/*" />
</div>
</xsl:template>
This applies templates to all children of qualifications
in document order (i.e. the order they appear in the document). There is no need to loop or select particular siblings. Let the XSLT processor do the work for you.
Upvotes: 3
Reputation: 5432
This should do. looping on each qual, storing position in a variable and applying templates to elements in order:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="education">
<div style="float:left;">
<xsl:for-each select="qualifications/qual">
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select="../qual[$pos]"/>
<xsl:apply-templates select="../grade[$pos]"/>
<xsl:apply-templates select="../level[$pos]"/>
</xsl:for-each>
</div>
</xsl:template>
<xsl:template match="qual"><span style="color:grey; font-size:15px; font-family:verdana;">
<xsl:value-of select="."/></span><p1>:</p1></xsl:template>
<xsl:template match="grade"><span style="color:grey; font-size:15px; font-family:verdana;"><xsl:value-of select="."/></span><p1> </p1></xsl:template>
<xsl:template match="level"><p1> (</p1><span style="color:grey; font-size:15px; font-family:verdana;"><xsl:value-of select="."/></span><p1>);</p1></xsl:template>
</xsl:stylesheet>
Upvotes: 1