您可以重用XML片段(以某种变量/引用方式)吗?

时间:2023-02-01 10:11:43

I am using SLD to style output from my geoserver. I have couple types of objects to be displayed. Each type have different icons, but text description is generated from the same information (text field in the database). I cannot use object_type as image name because you cannot use variable in image name (or I do not know how to do it...). So I Have SLD like (without unimportant parts):

我正在使用SLD来设置地理服务器的输出样式。我有几种类型的对象要显示。每种类型都有不同的图标,但文本描述是从相同的信息(数据库中的文本字段)生成的。我不能使用object_type作为图像名称,因为你不能在图像名称中使用变量(或者我不知道如何做...)。所以我喜欢SLD(没有不重要的部分):

<StyledLayerDescriptor>
  <NamedLayer>
    <UserStyle>
      <FeatureTypeStyle>
        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>1</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/1.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer>
            <Label>
              <ogc:PropertyName>name</ogc:PropertyName>
            </Label>
            <Font>
              <CssParameter name="font-family">Times New Roman</CssParameter>
              <CssParameter name="font-style">Normal</CssParameter>
              <CssParameter name="font-size">12</CssParameter>
            </Font>
            <Fill>
              <CssParameter name="fill">#110011</CssParameter>
              <CssParameter name="fill-opacity">1</CssParameter>
            </Fill>
            <Halo />
          </TextSymbolizer>
        </Rule>

        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>2</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/2.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer>
            <Label>
              <ogc:PropertyName>name</ogc:PropertyName>
            </Label>
            <Font>
              <CssParameter name="font-family">Times New Roman</CssParameter>
              <CssParameter name="font-style">Normal</CssParameter>
              <CssParameter name="font-size">12</CssParameter>
            </Font>
            <Fill>
              <CssParameter name="fill">#110011</CssParameter>
              <CssParameter name="fill-opacity">1</CssParameter>
            </Fill>
            <Halo />
          </TextSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

And the thing I'd love to do would be writing TextSymbolizer once and using it in two or more places... I have not heard of anything like that in XML, but maybe you have? :-)

我喜欢做的事情就是编写一次TextSymbolizer并在两个或更多的地方使用它...我还没有听说过XML中的那些,但也许你有吗? :-)

One solution that comes to me, is to write some very simple generator (xml.erb? :P) and let it generate XML...

我遇到的一个解决方案是编写一些非常简单的生成器(xml.erb?:P)并让它生成XML ...

3 个解决方案

#1


GeoServer's "Dynamic Symbolizer" extension to SLD does allow you to use database properties as all or part of icon URLs. You can even perform calculations and call filter functions if you want (they use embedded CQL in the URL.

GeoServer对SLD的“动态符号化”扩展允许您将数据库属性用作全部或部分图标URL。如果需要,您甚至可以执行计算和调用过滤器函数(它们在URL中使用嵌入式CQL。

Here's a blog post on the feature: http://blog.geoserver.org/2008/12/08/dynamic-symbolizers-part-1/

这是关于该功能的博客文章:http://blog.geoserver.org/2008/12/08/dynamic-symbolizers-part-1/

#2


Did you tried defining an XML Schema or optionally an XML DTD for your XML, and then using element identifiers http://www.w3.org/TR/xmlschema-0/#schemaConstraintsVsXML1, something like:

您是否尝试为XML定义XML模式或XML DTD,然后使用元素标识符http://www.w3.org/TR/xmlschema-0/#schemaConstraintsVsXML1,如下所示:



<StyledLayerDescriptor>
  <NamedLayer>
    <CommonStyles>
      <TextSymbolizer id="name_times_normal_12">
        <Label>
          <ogc:PropertyName>name</ogc:PropertyName>
        </Label>
        <Font>
          <CssParameter name="font-family">Times New Roman</CssParameter>
          <CssParameter name="font-style">Normal</CssParameter>
          <CssParameter name="font-size">12</CssParameter>
        </Font>
        <Fill>
          <CssParameter name="fill">#110011</CssParameter>
          <CssParameter name="fill-opacity">1</CssParameter>
        </Fill>
        <Halo />
      </TextSymbolizer>
    </CommonStyles>
    <UserStyle>
      <FeatureTypeStyle>
        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>1</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/1.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer ref="#name_times_normal_12" />
        </Rule>
        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>2</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/2.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer ref="#name_times_normal_12" />
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

#3


XML includes? http://www.xml.com/pub/a/2002/07/31/xinclude.html

XML包括? http://www.xml.com/pub/a/2002/07/31/xinclude.html

#1


GeoServer's "Dynamic Symbolizer" extension to SLD does allow you to use database properties as all or part of icon URLs. You can even perform calculations and call filter functions if you want (they use embedded CQL in the URL.

GeoServer对SLD的“动态符号化”扩展允许您将数据库属性用作全部或部分图标URL。如果需要,您甚至可以执行计算和调用过滤器函数(它们在URL中使用嵌入式CQL。

Here's a blog post on the feature: http://blog.geoserver.org/2008/12/08/dynamic-symbolizers-part-1/

这是关于该功能的博客文章:http://blog.geoserver.org/2008/12/08/dynamic-symbolizers-part-1/

#2


Did you tried defining an XML Schema or optionally an XML DTD for your XML, and then using element identifiers http://www.w3.org/TR/xmlschema-0/#schemaConstraintsVsXML1, something like:

您是否尝试为XML定义XML模式或XML DTD,然后使用元素标识符http://www.w3.org/TR/xmlschema-0/#schemaConstraintsVsXML1,如下所示:



<StyledLayerDescriptor>
  <NamedLayer>
    <CommonStyles>
      <TextSymbolizer id="name_times_normal_12">
        <Label>
          <ogc:PropertyName>name</ogc:PropertyName>
        </Label>
        <Font>
          <CssParameter name="font-family">Times New Roman</CssParameter>
          <CssParameter name="font-style">Normal</CssParameter>
          <CssParameter name="font-size">12</CssParameter>
        </Font>
        <Fill>
          <CssParameter name="fill">#110011</CssParameter>
          <CssParameter name="fill-opacity">1</CssParameter>
        </Fill>
        <Halo />
      </TextSymbolizer>
    </CommonStyles>
    <UserStyle>
      <FeatureTypeStyle>
        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>1</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/1.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer ref="#name_times_normal_12" />
        </Rule>
        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>2</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/2.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer ref="#name_times_normal_12" />
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

#3


XML includes? http://www.xml.com/pub/a/2002/07/31/xinclude.html

XML包括? http://www.xml.com/pub/a/2002/07/31/xinclude.html