gpt4 book ai didi

xmlstarlet 如何根据子元素值更新属性

转载 作者:行者123 更新时间:2023-12-05 06:42:11 27 4
gpt4 key购买 nike

<?xml version='1.0' encoding='UTF-8'?>
<star ABC="90" ABCType="Catch" endDateTime="2020-12-30T23:59:59" startDateTime="2010-08-10T06:00:00" timePeriodInDays="1" xmlns="urn:xxx:dyn:xxx:version01">
<tpName>seg</tpName>
<Instance endDateTime="2016-06-08T00:01:00"
startDateTime="2016-06-08T00:01:00">
<Id>PASS1</Id>
</Instance>
<Instance endDateTime="2016-06-10T00:00:00"
startDateTime="2016-06- 08T00:01:00">
<Id>PASS2"</Id>
</Instance>
</star>

我是 xmlstartlet 的新手。在上面的代码中,我试图将 startDateTime 修改为 "AAAA" 的实例,其中 Id 是 PASS1。我尝试执行以下操作:

xml ed -N w=urn:xxx:dyn:xxx:version01 -u "/w:star/w:Instance[@w:startDateTime"]/@w:startDateTime" -v "AAAA" 1.xml 

但出现以下错误:

None of the XPaths matched; to match a node in the default namespace use '' as the prefix (see section 5.1 in the manual). For instance, use /:node instead of /node

最佳答案

您不需要在 XPath 中为属性添加前缀。

试试这个:

xml ed -N w=urn:xxx:dyn:xxx:version01 -u "/w:star/w:Instance[w:Id='PASS1']/@startDateTime" -v "AAAA" 1.xml 

输出:

<star xmlns="urn:xxx:dyn:xxx:version01" ABC="90" ABCType="Catch" endDateTime="2020-12-30T23:59:59" startDateTime="2010-08-10T06:00:00" timePeriodInDays=
"1">
<tpName>seg</tpName>
<Instance endDateTime="2016-06-08T00:01:00" startDateTime="AAAA">
<Id>PASS1</Id>
</Instance>
<Instance endDateTime="2016-06-10T00:00:00" startDateTime="2016-06- 08T00:01:00">
<Id>PASS2"</Id>
</Instance>
</star>

关于xmlstarlet 如何根据子元素值更新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38126601/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com