【问题标题】:regex/XSLT: replace specific string within boundaries [duplicate]正则表达式/XSLT:替换边界内的特定字符串[重复]
【发布时间】:2019-08-23 09:37:09
【问题描述】:

我不确定如何使用正则表达式执行以下操作: 在边缘标签()内,检查是否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>

到目前为止,我得到了这个: (?&lt;=type="highway\.secondary")(speed)(?=edge),但这并没有找到速度...... 谢谢!

【问题讨论】:

  • 改用 XML 解析器
  • 我的回答应该能解决你的问题!它基于 XSLT 技术来解析输入 XML 并生成所需的 XML 输出
  • 有人称它为summoning the daemon,其他人称它为the Call for Cthulhu,很少有人称它为turned mad and met the Pony。简而言之,永远不要使用正则表达式解析 XML 或 HTML!您是否尝试过诸如 xmlstarletxmllintxsltproc 之类的 XML 解析器?

标签: regex xml parsing xslt xml-parsing


【解决方案1】:

输入: 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>

样式表: 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>

输出:

$ 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

【讨论】:

    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2023-03-16
    • 2021-12-19
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多