example.xsl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!--
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. -->
  18. <!--
  19. Simple transform of Solr query results to HTML
  20. -->
  21. <xsl:stylesheet version='1.0'
  22. xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  23. >
  24. <xsl:output media-type="text/html" encoding="UTF-8"/>
  25. <xsl:variable name="title" select="concat('Solr search results (',response/result/@numFound,' documents)')"/>
  26. <xsl:template match='/'>
  27. <html>
  28. <head>
  29. <title><xsl:value-of select="$title"/></title>
  30. <xsl:call-template name="css"/>
  31. </head>
  32. <body>
  33. <h1><xsl:value-of select="$title"/></h1>
  34. <div class="note">
  35. This has been formatted by the sample "example.xsl" transform -
  36. use your own XSLT to get a nicer page
  37. </div>
  38. <xsl:apply-templates select="response/result/doc"/>
  39. </body>
  40. </html>
  41. </xsl:template>
  42. <xsl:template match="doc">
  43. <xsl:variable name="pos" select="position()"/>
  44. <div class="doc">
  45. <table width="100%">
  46. <xsl:apply-templates>
  47. <xsl:with-param name="pos"><xsl:value-of select="$pos"/></xsl:with-param>
  48. </xsl:apply-templates>
  49. </table>
  50. </div>
  51. </xsl:template>
  52. <xsl:template match="doc/*[@name='score']" priority="100">
  53. <xsl:param name="pos"></xsl:param>
  54. <tr>
  55. <td class="name">
  56. <xsl:value-of select="@name"/>
  57. </td>
  58. <td class="value">
  59. <xsl:value-of select="."/>
  60. <xsl:if test="boolean(//lst[@name='explain'])">
  61. <xsl:element name="a">
  62. <!-- can't allow whitespace here -->
  63. <xsl:attribute name="href">javascript:toggle("<xsl:value-of select="concat('exp-',$pos)" />");</xsl:attribute>?</xsl:element>
  64. <br/>
  65. <xsl:element name="div">
  66. <xsl:attribute name="class">exp</xsl:attribute>
  67. <xsl:attribute name="id">
  68. <xsl:value-of select="concat('exp-',$pos)" />
  69. </xsl:attribute>
  70. <xsl:value-of select="//lst[@name='explain']/str[position()=$pos]"/>
  71. </xsl:element>
  72. </xsl:if>
  73. </td>
  74. </tr>
  75. </xsl:template>
  76. <xsl:template match="doc/arr" priority="100">
  77. <tr>
  78. <td class="name">
  79. <xsl:value-of select="@name"/>
  80. </td>
  81. <td class="value">
  82. <ul>
  83. <xsl:for-each select="*">
  84. <li><xsl:value-of select="."/></li>
  85. </xsl:for-each>
  86. </ul>
  87. </td>
  88. </tr>
  89. </xsl:template>
  90. <xsl:template match="doc/*">
  91. <tr>
  92. <td class="name">
  93. <xsl:value-of select="@name"/>
  94. </td>
  95. <td class="value">
  96. <xsl:value-of select="."/>
  97. </td>
  98. </tr>
  99. </xsl:template>
  100. <xsl:template match="*"/>
  101. <xsl:template name="css">
  102. <script>
  103. function toggle(id) {
  104. var obj = document.getElementById(id);
  105. obj.style.display = (obj.style.display != 'block') ? 'block' : 'none';
  106. }
  107. </script>
  108. <style type="text/css">
  109. body { font-family: "Lucida Grande", sans-serif }
  110. td.name { font-style: italic; font-size:80%; }
  111. td { vertical-align: top; }
  112. ul { margin: 0px; margin-left: 1em; padding: 0px; }
  113. .note { font-size:80%; }
  114. .doc { margin-top: 1em; border-top: solid grey 1px; }
  115. .exp { display: none; font-family: monospace; white-space: pre; }
  116. </style>
  117. </xsl:template>
  118. </xsl:stylesheet>