XSLT WFS输出格式模块

这个 xslt geoserver模块是一个WFS输出格式生成器,它将一个基本输出(如gml)和一个XSLT1.0样式表结合在一起,以生成用户选择的新文本输出格式。

此输出格式的配置可以直接在磁盘上执行,也可以通过RESTAPI执行。

手动配置

输出的所有配置都位于 $GEOSERVER_DATA_DIR/wfs/transform 文件夹,如果在GeoServer中安装了XSLT输出格式,则将在启动时创建该文件夹。

每个XSLT转换都必须在 $GEOSERVER_DATA_DIR/wfs/transform 文件夹,该文件夹又指向用于转换的xslt文件。虽然名称可以是自由形式,但建议遵循一个简单的命名约定:

  • XML配置文件的.xml

  • 对于xslt图块,<mytransformation>xslt

转换可以是全局的,因此适用于任何特征类型,也可以是特定于类型的,在这种情况下,转换知道转换后的特征类型的特定属性。

全局转换示例

下面是全局转换设置的示例。这个 $GEOSERVER_DATA_DIR/wfs/transform/global.xml 文件将如下所示:

<transform>
  <sourceFormat>text/xml; subtype=gml/2.1.2</sourceFormat>
  <outputFormat>HTML</outputFormat>
  <outputMimeType>text/html</outputMimeType>
  <fileExtension>html</fileExtension>
  <xslt>global.xslt</xslt>
</transform>

以下是每个元素的解释:

  • sourceFormat (必需):用作XSLT转换源的输出格式

  • outputFormat (必需):转换生成的输出格式

  • outputMimeType (可选):生成输出的mime类型。万一它不见了 outputFormat 假定为mime类型并用于此目的。

  • fileExtension (可选):生成输出的文件扩展名。万一它不见了 txt 将被使用。

  • xslt (必需):用于转换的XSLT 1.0样式表的名称

关联的XSLT文件将是 $GEOSERVER_DATA_DIR/wfs/transform/global.xslt 文件夹,它将能够将任何GML2输入转换为相应的HTML文件。下面是一个例子:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:wfs="http://www.opengis.net/wfs"
  xmlns:tiger="http://www.census.gov" xmlns:gml="http://www.opengis.net/gml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
      <xsl:for-each select="wfs:FeatureCollection/gml:featureMember/*">
        <h2><xsl:value-of select="@fid"/></h2>
        <table border="1">
          <tr>
            <th>Attribute</th>
            <th>Value</th>
          </tr>
            <!-- [not(*)] strips away all nodes having
                 children, in particular, geometries -->
            <xsl:for-each select="./*[not(*)]">
            <tr>
              <td>
                <xsl:value-of select="name()" />
              </td>
              <td>
                <xsl:value-of select="." />
              </td>
            </tr>
            </xsl:for-each>
        </table>
     </xsl:for-each>
     </body>
   </html>
  </xsl:template>
</xsl:stylesheet>

类型特定的转换

特定于类型的转换可以引用特定类型并直接利用其属性。虽然不是必需的,但最好设置一个全局转换,该转换可以处理任何功能类型(因为输出格式在功能文档中声明为常规,而不是特定于类型),然后为特定的功能类型重写它,以创建特殊的转换为了他们。

下面是一个特定于类型的转换声明的示例,它位于 $GEOSERVER_DATA_DIR/wfs/transform/html_bridges.xml

<transform>
  <sourceFormat>text/xml; subtype=gml/2.1.2</sourceFormat>
  <outputFormat>HTML</outputFormat>
  <outputMimeType>text/html</outputMimeType>
  <fileExtension>html</fileExtension>
  <xslt>html_bridges.xslt</xslt>
  <featureType>
    <id>cite:Bridges</id>
  </featureType>
</transform>

额外的 featureType 元素将转换关联到特定的特征类型

关联的xslt文件将位于 $GEOSERVER_DATA_DIR/wfs/transform/html_bridges.xslt 并将利用有关输入属性的知识:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:wfs="http://www.opengis.net/wfs"
  xmlns:cite="http://www.opengis.net/cite" xmlns:gml="http://www.opengis.net/gml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <h2>Bridges</h2>
        <xsl:for-each
            select="wfs:FeatureCollection/gml:featureMember/cite:Bridges">
        <ul>
          <li>ID: <xsl:value-of select="@fid" /></li>
          <li>FID: <xsl:value-of select="cite:FID" /></li>
          <li>Name: <xsl:value-of select="cite:NAME" /></li>
        </ul>
        <p/>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

备注

编写XSLT时,请始终记住声明工作表中使用的所有前缀 stylesheet 元素,否则可能会遇到难以理解的错误消息

特定的功能类型可以与多个输出格式相关联。虽然不常见,但通过创建多个XML配置文件并在每个文件中关联不同的功能类型,相同的XSLT文件也可以关联到多个功能类型。

休息配置

可以通过RESTAPI创建、更新和删除转换(通常,这需要管理员特权)。每个转换都用磁盘上使用的相同XML格式表示,但有两种变体:

  • 一个新的 name 属性出现,该属性与XML文件名匹配

  • 这个 featureType 元素还包含指向资源的链接,该资源表示REST配置树中的功能类型

例如:

<transform>
  <name>test</name>
  <sourceFormat>text/xml; subtype=gml/2.1.2</sourceFormat>
  <outputFormat>text/html</outputFormat>
  <fileExtension>html</fileExtension>
  <xslt>test-tx.xslt</xslt>
  <featureType>
    <name>tiger:poi</name>
    <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"/>
  </featureType>
</transform>

下面是一个资源列表以及可以在其上使用的HTTP方法。

/rest/services/wfs/transforms[.<format>]

方法

行动

返回代码

格式

默认格式

参数

GET

列出所有可用的转换

200

HTML、XML、JSON

HTML

POST

添加新转换

201,带 Location 页眉

XML、JSON

名称,源格式,输出格式,输出类型

PUT

更新全局设置

200

XML、JSON

DELETE

405

这个 POST 方法可以通过两种方式创建转换:

  • 如果使用的内容类型是 application/xml 服务器将假定 <transform> 正在发布定义,必须使用 PUT 具有内容类型的请求 application/xslt+xml 针对转换资源

  • 如果使用的内容类型是 application/xslt+xml 服务器将假定正在发布XSLT本身,并且 namesourceFormatoutputFormatoutputMimeType 查询参数将用于填充转换配置

/rest/services/wfs/transforms/<transform>[.<format>]

方法

行动

返回代码

格式

默认格式

GET

返回转换

200

HTML、XML、XSLT

HTML

POST

405

PUT

根据所使用的mime类型,更新转换配置或其xslt

200

XML、XSLT

DELETE

删除转换

200

根据请求中使用的内容类型,Put操作的行为不同:

  • 如果使用的内容类型是 application/xml 服务器将假定 <transform> 正在发送定义并将更新它

  • 如果使用的内容类型是 application/xslt+xml 服务器将假定正在发布XSLT本身,因此不会修改配置,但与之关联的XSLT将被覆盖。