2017-03-22 34 views
0

我是XSLT新手,我想編輯以下KML文件。XSLT如何根據描述值編輯KML地標風格

<?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> 

特別是我想根據描述值修改每個地標的樣式部分。如果描述值是23,我想用這種方式來替代樣式的一部分:

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

否則萬一描述值是10,我想代替這樣的風格部分:

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

換句話說,我想改變基礎上,說明價值KML佈局

請你能幫助我在XSL設置?提前致謝。

編輯按照你的建議,我包括了xslt文件,但它不是很好的形成。正如我所說我在XSLT方面並不熟練。

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

<xsl:output indent="yes"/> 
<xsl:strip-space elements="*"/> 

    <xsl:template match="@* | node()"> 
    <xsl:for-each select="kml:Placemark"> 
    <xsl:variable name="Value" select="@description"/> 
     <xsl:when test="$Value = '10'"> 
     <xsl:element name="$Style"> 

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

     </xsl:element> 
     </xsl:when> 

    </xsl:for-each> 
    <xsl:copy> 
    <xsl:apply-templates select="@*| node()"/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 
+0

你究竟在哪裏堅持下去? - 另外,23和10是唯一可能的值?如果不是的話,當價值不是這些時會發生什麼? –

+0

我嘗試了幾次從網上的例子來寫這樣一個xslt文件,但我沒有這個語言的基本語句來實現這個問題的知識。我將這些字符串('23'和'10')僅用於第一個簡單而通用的方法,以便理解xslt結構,但也可以理解如何總體評估相應的數字轉換值。例如,如何評估描述值(轉換爲數字)是否在25到50之間。 thx耐心等待 – Thaunes

+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你可以寫的模板,以該元素相匹配。