2015-02-06 75 views
0

我有一个描述了成千上万条道路的XML。如果另一个元素等于某个特定值,我该如何更改一个元素的值?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> 

例如,使用上面的示例,

我想与A,C和E的“边缘ID”改变所有边缘的“优先级”值;然后将结果保存到新文件。

感谢

+2

我们不知道您用于处理XML的内容。你是否试图在XSLT中做到这一点? Java的? C#? C? PHP? Perl的? – 2015-02-06 17:05:16

+2

XML只是一种数据格式。如果你想操纵它,你需要一种编程语言。 – Quentin 2015-02-06 17:05:24

+0

对不起,我很乐意提供关于如何处理它的建议。我一直在尝试使用SED/AWK,XMLstartlet,但迄今为止进展甚微。 – BenP 2015-02-06 17:17:13

回答

0

您可以尝试,如:

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> 
+0

完美,谢谢 – BenP 2015-02-06 17:37:50

2

在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>