Using xsl:sort to sort output nodes by substrings


/ Published in: XSLT
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <!--
  2.  
  3. The stylesheet will transform this XML document:
  4.  
  5. <doc>
  6. <SortPrefixAndTitle>3|Third Course</SortPrefixAndTitle>
  7. <SortPrefixAndTitle>1|First Course</SortPrefixAndTitle>
  8. <SortPrefixAndTitle>2|Second Course</SortPrefixAndTitle>
  9. </doc>
  10.  
  11. into:
  12.  
  13. <courseTitles>
  14. <courseTitle>First Course</courseTitle>
  15. <courseTitle>Second Course</courseTitle>
  16. <courseTitle>Third Course</courseTitle>
  17. </courseTitles>
  18.  
  19. -->
  20.  
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  23. <xsl:template match="/">
  24. <courseTitles>
  25. <xsl:apply-templates select="//SortPrefixAndTitle">
  26. <xsl:sort select="substring-before(.,'|')"> </xsl:sort>
  27. </xsl:apply-templates>
  28. </courseTitles>
  29. </xsl:template>
  30. <xsl:template match="SortPrefixAndTitle">
  31. <xsl:variable name="title" select="substring-after(.,'|')" />
  32. <courseTitle>
  33. <xsl:value-of select="$title" />
  34. </courseTitle>
  35. </xsl:template>
  36. </xsl:stylesheet>

URL: xsl-sort-substrings

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.