XSLT如何根据描述值编辑KML地标样式

时间:2017-03-22 11:53:37

标签: xml xslt kml

我是XSLT的新手,我想编辑以下KML文件。

        o.writeObject(jsonObject2.toString());

特别是我想根据描述值修改每个地标的样式部分。如果描述值是23,我想以这种方式替换样式部分:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="doc">
<Schema name="Geographic_Placemarks">
  <SimpleField name="Description" type="string" />
  <SimpleField name="x" type="string" />
  <SimpleField name="y" type="string" />
</Schema>
<Folder>
  <name>Geographic_Placemarks</name>

     <Placemark>
    <name>Site 1</name>
    <description>23</description>
    <Style>
      <LineStyle>
        <color>ff0000ff</color>
      </LineStyle>
      <PolyStyle>
        <fill>0</fill>
      </PolyStyle>
    </Style>
    <ExtendedData>
      <SchemaData schemaUrl="#Geographic_Placemarks">
        <SimpleData name="x">571750    </SimpleData>
        <SimpleData name="y">4548250    </SimpleData>
      </SchemaData>
    </ExtendedData>
    <Polygon>
      <altitudeMode>clampToGround    </altitudeMode>
      <outerBoundaryIs>
        <LinearRing>
          <altitudeMode>clampToGround    </altitudeMode>
          <coordinates>11.1825432433631,45.6613329598511     11.1298128785963,45.7000370530753 11.1833198656477,45.6994951268141 11.1825432433631,45.6613329598511    </coordinates>
        </LinearRing>
      </outerBoundaryIs>
    </Polygon>
  </Placemark>

  <Placemark>
    <name>Site 2</name>
    <description>10</description>
    <Style>
      <LineStyle>
        <color>ff0000ff</color>
      </LineStyle>
      <PolyStyle>
        <fill>0</fill>
      </PolyStyle>
    </Style>
    <ExtendedData>
      <SchemaData schemaUrl="#Geographic_Placemarks">
        <SimpleData name="x">575750</SimpleData>
        <SimpleData name="y">4548250</SimpleData>
      </SchemaData>
    </ExtendedData>
    <Polygon>
      <altitudeMode>clampToGround</altitudeMode>
      <outerBoundaryIs>
        <LinearRing>
          <altitudeMode>clampToGround</altitudeMode>
          <coordinates>11.1825432433631,45.6613329598511     11.1833198656477,45.6994951268141 11.2337967406582,45.6989609013362 11.2329870100429,45.6607994408117 11.1825432433631,45.6613329598511      </coordinates>
        </LinearRing>
      </outerBoundaryIs>
      </Polygon>
     </Placemark>
  </Folder>
</Document>
</kml>

否则,如果描述值为10,我想以这种方式替换样式部分:

<LineStyle>
    <color>ff0000ff</color>
</LineStyle>
<PolyStyle>
    <fill>0.5</fill>
    <color>ff0000ff</color>
</PolyStyle>

换句话说,我想根据描述值

更改KML布局

请帮我解决XSL设置问题?提前谢谢。

编辑为了按照你的建议我包含xslt文件,但它没有很好地形成。正如我所说,我对XSLT并不熟练。

<LineStyle>
    <color>ff0000ff</color>
</LineStyle>
<PolyStyle>
    <fill>1</fill>
    <color>#ffff99</color>
</PolyStyle>

1 个答案:

答案 0 :(得分:0)

我建议使用单独的模板来替换需要更改的部分,而不是大选择/何时阻止。例如:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:kml="http://www.opengis.net/kml/2.2">
    <xsl:output method="xml"/>

    <!-- identity template copies nodes unchanged -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- matches Style with sibling of description='23' -->
    <xsl:template match="kml:Style[../kml:description='23']">
        <Style>
            <LineStyle>
                <color>ff0000ff</color>
            </LineStyle>
            <PolyStyle>
                <fill>0.5</fill>
                <color>ff0000ff</color>
            </PolyStyle>
        </Style>
    </xsl:template>

    <!-- matches Style with sibling of description='10' -->
    <xsl:template match="kml:Style[../kml:description='10']">
        <Style>
            <LineStyle>
                <color>ff0000ff</color>
            </LineStyle>
            <PolyStyle>
                <fill>1</fill>
                <color>#ffff99</color>
            </PolyStyle>
        </Style>
    </xsl:template>

</xsl:stylesheet>

这当然可以进一步细化,例如,如果您只是实际更改PolyStyle,则可以编写模板以匹配该元素。