这个问题在这里已有答案:
我不确定如何使用正则表达式执行以下操作:在edge tags()中,检查是否type="highway.secondary"
,如果是,则将所有速度值替换为40
。
<edge id="-100396051#2" type="highway.unclassified">
<lane id="-100396051#2_0" index="0" speed="13.89">
<param key="origId" value="100396051"/>
</lane>
</edge>
<edge id="-101784374#0" type="highway.secondary">
<lane id="-101784374#0_0" index="0" speed="27.78" length="17.22" >
<param key="origId" value="101784374"/>
</lane>
<lane id="-101784374#0_1" index="1" speed="29.98" length="17.22" >
<param key="origId" value="101784374"/>
</lane>
</edge>
到目前为止我得到了这个:(?<=type="highway\.secondary")(speed)(?=edge)
,但是找不到速度......谢谢!
输入:edges.xml
<edges>
<edge id="-100396051#2" type="highway.unclassified">
<lane id="-100396051#2_0" index="0" speed="13.89">
<param key="origId" value="100396051"/>
</lane>
</edge>
<edge id="-101784374#0" type="highway.secondary">
<lane id="-101784374#0_0" index="0" speed="27.78" length="17.22" >
<param key="origId" value="101784374"/>
</lane>
<lane id="-101784374#0_1" index="1" speed="29.98" length="17.22" >
<param key="origId" value="101784374"/>
</lane>
</edge>
</edges>
STYLESHEET:change_speed.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="@speed[../parent::edge and ../../@type='highway.secondary']">
<xsl:attribute name="speed">
<xsl:value-of select="'40'"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
OUTPUT:
$ xsltproc change_speed.xsl edges.xml
<?xml version="1.0" encoding="utf-8"?>
<edges>
<edge id="-100396051#2" type="highway.unclassified">
<lane id="-100396051#2_0" index="0" speed="13.89">
<param key="origId" value="100396051"/>
</lane>
</edge>
<edge id="-101784374#0" type="highway.secondary">
<lane id="-101784374#0_0" index="0" speed="40" length="17.22">
<param key="origId" value="101784374"/>
</lane>
<lane id="-101784374#0_1" index="1" speed="40" length="17.22">
<param key="origId" value="101784374"/>
</lane>
</edge>
</edges>
说明:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
将递归复制所有节点和属性
<xsl:template match="@speed[../parent::edge and ../../@type='highway.secondary']">
<xsl:attribute name="speed">
<xsl:value-of select="'40'"/>
</xsl:attribute>
</xsl:template>
当您到达名为speed
的属性,其父节点名为edge
并且具有值为type
的属性highway.secondary
时,请将此属性的值更改为40
。