XML IF元素替换

XML IF Element Replacement

我有一个 XML 描述了成千上万条道路。如果另一个元素等于某个值,我如何/如何更改一个元素的值?

 <edge id="a" from="823472303" to="1157679486" priority="2" type="highway.service" shape="71013.78,70416.19 71009.26,70418.36 71004.38,70417.75 70998.91,70412.27">
    <lane id="-100151810_0" index="0" allow="delivery" speed="5.56" length="17.67" shape="71013.24,70418.28 71009.54,70420.06 71003.61,70419.32 70997.74,70413.44"/>
</edge>
<edge id="b" from="1158231870" to="1158231886" priority="2" type="highway.service" shape="66981.74,70626.70 66973.61,70322.61 66985.21,70284.19">
    <lane id="-100203601_0" index="0" allow="delivery" speed="5.56" length="344.33" shape="66980.09,70626.74 66971.95,70322.39 66981.11,70292.05"/>
</edge>
<edge id="c" from="2149636885" to="349236976" priority="5" type="highway.unclassified" shape="20785.34,49337.55 20786.22,49280.50 20785.67,49194.22 20783.27,49173.44">
    <lane id="-100271410_0" index="0" speed="22.22" length="164.26" shape="20783.69,49337.52 20784.57,49280.47 20784.02,49194.23 20781.65,49173.77"/>
</edge>
<edge id="d" from="1142559441" to="1162085213" priority="2" type="highway.service" shape="70850.72,62133.69 70847.59,62151.63 70820.27,62173.78 70787.71,62211.29 70774.77,62228.21">
    <lane id="-100528728_0" index="0" allow="delivery" speed="5.56" length="124.35" shape="70852.35,62133.97 70849.11,62152.52 70821.42,62174.97 70788.96,62212.37 70776.18,62229.09"/>
</edge>

例如,使用上面的示例,

我想更改所有具有 "edge id" a、c 和 e 的边的 "priority" 值;然后将结果保存到一个新文件中。

谢谢

你可以试试 ,比如:

xmlstarlet ed -u '//edge[@id="a" or @id="c" or @id="e"]/@priority' -v '7' file

它使用ed命令编辑输入文件,-u更新任何值,后跟xpath表达式和-v表示替换值。假设一个带有根节点(不是你的)的正确 xml 文件会产生:

<?xml version="1.0"?>
<root>
  <edge id="a" from="823472303" to="1157679486" priority="7" type="highway.service" shape="71013.78,70416.19 71009.26,70418.36 71004.38,70417.75 70998.91,70412.27">
    <lane id="-100151810_0" index="0" allow="delivery" speed="5.56" length="17.67" shape="71013.24,70418.28 71009.54,70420.06 71003.61,70419.32 70997.74,70413.44"/>
  </edge>
  <edge id="b" from="1158231870" to="1158231886" priority="2" type="highway.service" shape="66981.74,70626.70 66973.61,70322.61 66985.21,70284.19">
    <lane id="-100203601_0" index="0" allow="delivery" speed="5.56" length="344.33" shape="66980.09,70626.74 66971.95,70322.39 66981.11,70292.05"/>
  </edge>
  <edge id="c" from="2149636885" to="349236976" priority="7" type="highway.unclassified" shape="20785.34,49337.55 20786.22,49280.50 20785.67,49194.22 20783.27,49173.44">
    <lane id="-100271410_0" index="0" speed="22.22" length="164.26" shape="20783.69,49337.52 20784.57,49280.47 20784.02,49194.23 20781.65,49173.77"/>
  </edge>
  <edge id="d" from="1142559441" to="1162085213" priority="2" type="highway.service" shape="70850.72,62133.69 70847.59,62151.63 70820.27,62173.78 70787.71,62211.29 70774.77,62228.21">
    <lane id="-100528728_0" index="0" allow="delivery" speed="5.56" length="124.35" shape="70852.35,62133.97 70849.11,62152.52 70821.42,62174.97 70788.96,62212.37 70776.18,62229.09"/>
  </edge>
</root>

在 XSLT 中,定义复制所有内容的默认规则:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

然后是进行所需修改的另一条规则:

<xsl:template match="edge[@id=('a', 'c', 'e')]">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:attribute name="priority" select="(new value)"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>