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

xmlstarlet how to update an attribute based on subelement value

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