XSLT 如何根据描述值编辑 KML 地标样式
XSLT how to edit KML Placemark Style based on description value
我是 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>
而不是一个大的 choose/when 块,我建议使用单独的模板来替换需要更改的部分。例如:
<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,则可以编写模板来匹配该元素。
我是 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>
而不是一个大的 choose/when 块,我建议使用单独的模板来替换需要更改的部分。例如:
<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,则可以编写模板来匹配该元素。