Sorting XML based on several sub-elements using XSLT -


i'm trying sort xml (using xslt) based on few child elements , return result xml. know it's not difficult first experience using xslt , it's giving me troubles. here's xml:

<root>   <subject>     <coursesubjectheader>         <subjectcode>b</subjectcode>         <subjectname>text</subjectname>         <unit>text</unit>         <faculty>text</faculty>     </coursesubjectheader>     <course>       <crslevel>text</crslevel>       <subjectandnumber>b 200</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>     <course>       <crslevel>text</crslevel>       <subjectandnumber>b 100</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>   </subject>   <subject>     <coursesubjectheader>         <subjectcode>c</subjectcode>         <subjectname>text</subjectname>         <unit>text</unit>         <faculty>text</faculty>     </coursesubjectheader>     <course>       <crslevel>text</crslevel>       <subjectandnumber>c 300</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>     <course>       <crslevel>text</crslevel>       <subjectandnumber>c 100</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>   </subject>   <subject>     <coursesubjectheader>         <subjectcode>a</subjectcode>         <subjectname>text</subjectname>         <unit>text</unit>         <faculty>text</faculty>     </coursesubjectheader>     <course>       <crslevel>text</crslevel>       <subjectandnumber>a 300</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>     <course>       <crslevel>text</crslevel>       <subjectandnumber>a 200</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>   </subject> </root> 

i'd sort 'subjects' 'subjectcode' child element, , courses within each subject 'subjectandnumber' child element. resulting xml be...

<root>   <subject>     <coursesubjectheader>         <subjectcode>a</subjectcode>         <subjectname>text</subjectname>         <unit>text</unit>         <faculty>text</faculty>     </coursesubjectheader>     <course>       <crslevel>text</crslevel>       <subjectandnumber>a 200</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>     <course>       <crslevel>text</crslevel>       <subjectandnumber>a 300</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>   </subject>   <subject>     <coursesubjectheader>         <subjectcode>b</subjectcode>         <subjectname>text</subjectname>         <unit>text</unit>         <faculty>text</faculty>     </coursesubjectheader>     <course>       <crslevel>text</crslevel>       <subjectandnumber>b 100</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>     <course>       <crslevel>text</crslevel>       <subjectandnumber>b 200</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>   </subject>   <subject>     <coursesubjectheader>         <subjectcode>c</subjectcode>         <subjectname>text</subjectname>         <unit>text</unit>         <faculty>text</faculty>     </coursesubjectheader>     <course>       <crslevel>text</crslevel>       <subjectandnumber>c 100</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>     <course>       <crslevel>text</crslevel>       <subjectandnumber>c 300</subjectandnumber>       <units>3.0</units>       <hours>3-0</hours>     </course>   </subject> </root> 

and finally, here's (pretty awful) attempt @ xslt:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"      xmlns:xsl="http://www.w3.org/1999/ /transform">      <xsl:output method="xml" indent="yes"/>     <xsl:strip-space elements="*"/>      <xsl:template match="@*|node()">         <xsl:copy>             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>     </xsl:template>      <xsl:template match="/">         <xsl:copy>             <xsl:apply-templates select="subject">                 <xsl:sort select="subjectcode"/>             </xsl:apply-templates>         </xsl:copy>     </xsl:template>      <xsl:template match="subject">         <xsl:copy>             <xsl:apply-templates select="course">                 <xsl:sort select="subjectandnumber"/>             </xsl:apply-templates>         </xsl:copy>     </xsl:template>  </xsl:stylesheet> 

any appreciated, thanks!

you have few minor mistakes - compare:

<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="@*|node()">     <xsl:copy>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template>  <xsl:template match="/*">     <xsl:copy>         <xsl:apply-templates select="subject">             <xsl:sort select="coursesubjectheader/subjectcode"/>         </xsl:apply-templates>     </xsl:copy> </xsl:template>  <xsl:template match="subject">     <xsl:copy>         <xsl:copy-of select="coursesubjectheader"/>         <xsl:apply-templates select="course">             <xsl:sort select="subjectandnumber"/>         </xsl:apply-templates>     </xsl:copy> </xsl:template>  </xsl:stylesheet> 

note: serious mistake this: xmlns:xsl="http://www.w3.org/1999/ /transform". if don't declare xslt namespace properly, document not stylesheet @ all.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -