XML IF元素替换
我有一个描述数千条道路的XML。如果另一个元素等于某个值,我如何更改一个元素的值XML IF元素替换,xml,xml-parsing,xmlstarlet,Xml,Xml Parsing,Xmlstarlet,我有一个描述数千条道路的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
<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>
例如,使用上面的示例
我想更改“边id”为a、c和e的所有边的“优先级”值;然后将结果保存到新文件中
谢谢您可以试试,比如:
它使用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>
我们不知道您正在使用什么来处理XML。您是否试图在XSLT中实现这一点?JAVAC#?CPHP?Perl?XML只是一种数据格式。如果你想操作它,你需要一种编程语言。对不起,我愿意听取关于如何处理它的建议。我一直在尝试使用SED/AWK和xmlstartet,但迄今为止进展甚微。
<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>