index.rst 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. .. _xslt:
  2. XSLT WFS output format module
  3. =============================
  4. The ``xslt`` module for GeoServer is a WFS output format generator which brings together a base output,
  5. such as GML, and a XSLT 1.0 style sheet to generate a new textual output format of user choosing.
  6. The configuration for this output format can be either performed directly on disk, or via a REST API.
  7. Manual configuration
  8. --------------------
  9. All the configuration for the output resides in the ``$GEOSERVER_DATA_DIR/wfs/transform`` folder,
  10. which is going to be created on startup when missing if the XSLT output format has been installed
  11. in GeoServer.
  12. Each XSLT transformation must be configured with its own xml file in the ``$GEOSERVER_DATA_DIR/wfs/transform`` folder,
  13. which in turn points to a xslt file for the transformation. While the names can be freeform, it is suggested to follow
  14. a simple naming convention:
  15. * <mytransformation>.xml for the xml config file
  16. * <mytransformation>.xslt for the xslt tile
  17. Transformations can be either global, and thus applicable to any feature type, or type specific, in which case
  18. the transformation knows about the specific attributes of the transformed feature type.
  19. Global transformation example
  20. -----------------------------
  21. Here is an example of a global transformation setup. The ``$GEOSERVER_DATA_DIR/wfs/transform/global.xml`` file will
  22. look like:
  23. .. code-block:: xml
  24. <transform>
  25. <sourceFormat>text/xml; subtype=gml/2.1.2</sourceFormat>
  26. <outputFormat>HTML</outputFormat>
  27. <outputMimeType>text/html</outputMimeType>
  28. <fileExtension>html</fileExtension>
  29. <xslt>global.xslt</xslt>
  30. </transform>
  31. Here is an explanation of each element:
  32. * ``sourceFormat`` (mandatory): the output format used as the source of the XSLT transformation
  33. * ``outputFormat`` (mandatory): the output format generated by the transformation
  34. * ``outputMimeType`` (optional): the mime type for the generated output. In case it's missing, the ``outputFormat`` is assumed to be a mime type and used for the purpose.
  35. * ``fileExtension`` (optional): the file extension for the generated output. In case it's missing ``txt`` will be used.
  36. * ``xslt`` (mandatory): the name of XSLT 1.0 style sheet used for the transformation
  37. The associated XSLT file will be ``$GEOSERVER_DATA_DIR/wfs/transform/global.xslt`` folder, and it will be able to transform any GML2 input into a corresponding HTML file.
  38. Here is an example:
  39. .. code-block:: xml
  40. <?xml version="1.0" encoding="ISO-8859-1"?>
  41. <xsl:stylesheet version="1.0" xmlns:wfs="http://www.opengis.net/wfs"
  42. xmlns:tiger="http://www.census.gov" xmlns:gml="http://www.opengis.net/gml"
  43. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  44. <xsl:template match="/">
  45. <html>
  46. <body>
  47. <xsl:for-each select="wfs:FeatureCollection/gml:featureMember/*">
  48. <h2><xsl:value-of select="@fid"/></h2>
  49. <table border="1">
  50. <tr>
  51. <th>Attribute</th>
  52. <th>Value</th>
  53. </tr>
  54. <!-- [not(*)] strips away all nodes having
  55. children, in particular, geometries -->
  56. <xsl:for-each select="./*[not(*)]">
  57. <tr>
  58. <td>
  59. <xsl:value-of select="name()" />
  60. </td>
  61. <td>
  62. <xsl:value-of select="." />
  63. </td>
  64. </tr>
  65. </xsl:for-each>
  66. </table>
  67. </xsl:for-each>
  68. </body>
  69. </html>
  70. </xsl:template>
  71. </xsl:stylesheet>
  72. Type specific transformations
  73. -----------------------------
  74. Type specific transformations can refer to a specific type and leverage its attributes directly. While not required, it is good practice
  75. to setup a global transformation that can handle any feature type (since the output format is declared in the capabilities document as being
  76. general, not type specific) and then override it for specific feature types in order to create special transformations for them.
  77. Here is an example of a transformation declaration that is type specific, that will be located at ``$GEOSERVER_DATA_DIR/wfs/transform/html_bridges.xml``
  78. .. code-block:: xml
  79. <transform>
  80. <sourceFormat>text/xml; subtype=gml/2.1.2</sourceFormat>
  81. <outputFormat>HTML</outputFormat>
  82. <outputMimeType>text/html</outputMimeType>
  83. <fileExtension>html</fileExtension>
  84. <xslt>html_bridges.xslt</xslt>
  85. <featureType>
  86. <id>cite:Bridges</id>
  87. </featureType>
  88. </transform>
  89. The extra ``featureType`` element associates the transformation to the specific feature type
  90. The associated xslt file will be located at ``$GEOSERVER_DATA_DIR/wfs/transform/html_bridges.xslt`` and will
  91. leveraging knowledge about the input attributes:
  92. .. code-block:: xml
  93. <?xml version="1.0" encoding="ISO-8859-1"?>
  94. <xsl:stylesheet version="1.0" xmlns:wfs="http://www.opengis.net/wfs"
  95. xmlns:cite="http://www.opengis.net/cite" xmlns:gml="http://www.opengis.net/gml"
  96. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  97. <xsl:template match="/">
  98. <html>
  99. <body>
  100. <h2>Bridges</h2>
  101. <xsl:for-each
  102. select="wfs:FeatureCollection/gml:featureMember/cite:Bridges">
  103. <ul>
  104. <li>ID: <xsl:value-of select="@fid" /></li>
  105. <li>FID: <xsl:value-of select="cite:FID" /></li>
  106. <li>Name: <xsl:value-of select="cite:NAME" /></li>
  107. </ul>
  108. <p/>
  109. </xsl:for-each>
  110. </body>
  111. </html>
  112. </xsl:template>
  113. </xsl:stylesheet>
  114. .. note:: While writing the XSLT always remember to declare all prefixes used in the sheet in the ``stylesheet`` element, otherwise you might encounter hard to understand error messages
  115. A specific feature type can be associated to multiple output formats. While uncommon, the same xslt file can also be associated to multiple feature types by creating
  116. multiple xml configuration files, and associating a different feature type in each.
  117. Rest configuration
  118. ------------------
  119. Transformations can be created, updated and deleted via the REST api (normally, this requires administrator privileges).
  120. Each transformation is represented with the same XML format used on disk, but with two variants:
  121. * a new ``name`` attribute appears, which matches the XML file name
  122. * the ``featureType`` element contains also a link to the resource representing the feature type in the REST config tree
  123. For example:
  124. .. code-block:: xml
  125. <transform>
  126. <name>test</name>
  127. <sourceFormat>text/xml; subtype=gml/2.1.2</sourceFormat>
  128. <outputFormat>text/html</outputFormat>
  129. <fileExtension>html</fileExtension>
  130. <xslt>test-tx.xslt</xslt>
  131. <featureType>
  132. <name>tiger:poi</name>
  133. <atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="alternate" href="http://localhost:8080/geoserver/rest/workspaces/cite/datastores/cite/featuretypes/bridges.xml" type="application/xml"/>
  134. </featureType>
  135. </transform>
  136. Here is a list of resources and the HTTP methods that can be used on them.
  137. ``/rest/services/wfs/transforms[.<format>]``
  138. .. list-table::
  139. :header-rows: 1
  140. * - Method
  141. - Action
  142. - Return Code
  143. - Formats
  144. - Default Format
  145. - Parameters
  146. * - GET
  147. - List all available transforms
  148. - 200
  149. - HTML, XML, JSON
  150. - HTML
  151. -
  152. * - POST
  153. - Add a new transformation
  154. - 201, with ``Location`` header
  155. - XML, JSON
  156. -
  157. - name, sourceFormat, outputFormat, outputMimeType
  158. * - PUT
  159. - Update global settings
  160. - 200
  161. - XML, JSON
  162. -
  163. -
  164. * - DELETE
  165. -
  166. - 405
  167. -
  168. -
  169. -
  170. The ``POST`` method can be used to create a transformation in two ways:
  171. * if the content type used is ``application/xml`` the server will assume a ``<transform>`` definition is being posted,
  172. and the XSLT will have to be uploaded separately using a ``PUT`` request with content type ``application/xslt+xml``
  173. against the transformation resource
  174. * if the content type used is ``application/xslt+xml`` the server will assume the XSLT itself is being posted, and
  175. the ``name``, ``sourceFormat``, ``outputFormat``, ``outputMimeType`` query parameters will be used to fill in the
  176. transform configuration instead
  177. ``/rest/services/wfs/transforms/<transform>[.<format>]``
  178. .. list-table::
  179. :header-rows: 1
  180. * - Method
  181. - Action
  182. - Return Code
  183. - Formats
  184. - Default Format
  185. * - GET
  186. - Returns the transformation
  187. - 200
  188. - HTML, XML, XSLT
  189. - HTML
  190. * - POST
  191. -
  192. - 405
  193. -
  194. -
  195. * - PUT
  196. - Updates either the transformation configuration, or its XSLT, depending on the mime type used
  197. - 200
  198. - XML, XSLT
  199. -
  200. * - DELETE
  201. - Deletes the transformation
  202. - 200
  203. -
  204. -
  205. The PUT operation behaves differently depending on the content type used in the request:
  206. * if the content type used is ``application/xml`` the server will assume a ``<transform>`` definition is being sent
  207. and will update it
  208. * if the content type used is ``application/xslt+xml`` the server will assume the XSLT itself is being posted, as such
  209. the configuration won't be modified, but the XSLT associated to it will be overwritten instead