Posted By

iroybot on 05/16/13


Tagged


Versions (?)

XML structure to Tree View


 / Published in: XSLT
 

URL: http://stackoverflow.com/questions/15958557/tree-presentation-of-xml-structure-how-to-test-for-parents-preceding-siblings

a perfect tree representation of a XML structure (with i, l and t and empty node images)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet exclude-result-prefixes="xsl xsi html xsd" version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output encoding="UTF-8" indent="no" method="xml" omit-xml-declaration="yes"/>
  4. <xsl:strip-space elements="*"/>
  5.  
  6. <xsl:param name="id" />
  7.  
  8. <xsl:template match="/">
  9. <xsl:apply-templates select="//navigation[@id=$id]" />
  10. </xsl:template>
  11.  
  12. <xsl:template match="navigation">
  13. <h1>Edit <xsl:value-of select="name" /> (<xsl:value-of select="language" />)</h1><br />
  14. <table class="table" id="links">
  15. <thead>
  16. <tr>
  17. <th>Link</th>
  18. <th>&#160;</th>
  19. <th>URL</th>
  20. <th>Title</th>
  21. <th>Active</th>
  22. <th>Actions</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <tr class="level0">
  27. <td colspan="6"><i class="icon-home"><xsl:text> </xsl:text></i></td>
  28. </tr>
  29. <xsl:apply-templates select="links" />
  30. <tr><td colspan="6"><a data-navigation="{$id}" class="add-link btn btn-success btn-small" href="#add-link"><i class="icon-plus"><xsl:text> </xsl:text></i> Append a Link</a></td></tr>
  31. </tbody>
  32. </table>
  33. </xsl:template>
  34.  
  35. <xsl:template match="link">
  36. <xsl:variable name="chain" select="ancestor-or-self::link" />
  37. <tr class="level{count($chain)}">
  38. <td>
  39. <xsl:apply-templates select="$chain" mode="tracks">
  40. <xsl:with-param name="context" select="." />
  41. </xsl:apply-templates>
  42. <strong><xsl:value-of select="text" /></strong>
  43. </td>
  44. <td><a href="#" class="btn btn-success btn-mini add-link" data-navigation="{$id}" data-parent="{@id}"><i class="icon-plus"><xsl:text> </xsl:text></i> Append a Sublink</a><xsl:text> </xsl:text></td>
  45. <td><xsl:value-of select="url" /></td>
  46. <td>
  47. <xsl:choose>
  48. <xsl:when test="string-length(title)>0">
  49. <xsl:value-of select="substring(title,0,50)" />
  50. </xsl:when>
  51. <xsl:otherwise>
  52. <i class="red icon-bolt"><xsl:text> </xsl:text></i>
  53. </xsl:otherwise>
  54. </xsl:choose>
  55. </td>
  56. <td><xsl:value-of select="active" /></td>
  57. <td>
  58. <div class="btn-group">
  59. <xsl:choose>
  60. <xsl:when test="active='yes'">
  61. <a href="#" class="btn btn-mini deactivate" data-link="{@id}" data-navigation="{$id}"><i class="icon-check-empty"><xsl:text> </xsl:text></i> Deactivate</a>
  62. </xsl:when>
  63. <xsl:when test="active='no'">
  64. <a href="#" class="btn btn-mini activate" data-link="{@id}" data-navigation="{$id}"><i class="icon-check"><xsl:text> </xsl:text></i> Activate</a>
  65. </xsl:when>
  66. </xsl:choose>
  67. <a href="#" class="btn btn-mini edit" data-id="{@id}" data-navigation="{$id}"><i class="icon-edit"><xsl:text> </xsl:text></i> Edit</a>
  68.  
  69. <a href="#" class="btn btn-mini edit" data-id="{@id}" data-navigation="{$id}"><i class="icon-edit"><xsl:text> </xsl:text></i> Advanced Edit</a>
  70.  
  71. <a href="#">
  72. <xsl:attribute name="class">btn btn-mini
  73. <xsl:choose>
  74. <xsl:when test="position()=1"> disabled</xsl:when>
  75. </xsl:choose>
  76. </xsl:attribute>
  77. <i class="icon-circle-arrow-up"><xsl:text> </xsl:text></i> Up
  78. </a>
  79. <a href="#">
  80. <xsl:attribute name="class">btn btn-mini
  81. <xsl:choose>
  82. <xsl:when test="position()=count($chain)"> disabled</xsl:when>
  83. </xsl:choose>
  84. </xsl:attribute>
  85. <i class="icon-circle-arrow-down"><xsl:text> </xsl:text></i> Down
  86. </a>
  87. <a href="#" class="btn btn-mini btn-danger delete-link" data-id="{@id}" data-navigation="{$id}"><i class="icon-trash"><xsl:text> </xsl:text></i> Delete</a><xsl:text> </xsl:text>
  88. </div>
  89. </td>
  90. </tr>
  91. <xsl:apply-templates select="links" />
  92. </xsl:template>
  93.  
  94. <xsl:template match="link" mode="tracks">
  95. <xsl:param name="context" />
  96. <xsl:variable name="isSame" select="generate-id(.) = generate-id($context)" />
  97. <xsl:variable name="pos" select="count(preceding-sibling::link)" />
  98. <xsl:variable name="isLast" select="not(following-sibling::link)" />
  99. <xsl:variable name="type">
  100. <xsl:choose>
  101. <xsl:when test="$isSame">
  102. <xsl:if test="(following::link or links/link) and not($isLast)">t</xsl:if>
  103. <xsl:if test="$isLast">l</xsl:if>
  104. </xsl:when>
  105. <xsl:when test="$isLast and links/link">e</xsl:when>
  106. <xsl:when test="following::link and links/link">i</xsl:when>
  107. <xsl:when test="not(following::link)">e</xsl:when>
  108. </xsl:choose>
  109. </xsl:variable>
  110. <xsl:call-template name="node-type">
  111. <xsl:with-param name="type" select="$type" />
  112. </xsl:call-template>
  113. </xsl:template>
  114.  
  115. <xsl:template name="node-type">
  116. <xsl:param name="type" select="'t'" />
  117. <img class="textmiddle" src="/images/backend/{$type}-node.png" />
  118. </xsl:template>
  119. </xsl:stylesheet>

Report this snippet  

You need to login to post a comment.