【问题标题】:Update xml attribute values using xmlStarlet使用 xmlStarlet 更新 xml 属性值
【发布时间】:2022-01-06 18:22:51
【问题描述】:
        <?xml version="1.0"?>
<Student xmlns="http://www.tibco.com/schemas/TrainingPOCs/SharedRec/Schemas/Schema.xsd">
  <Phno type="kwh" value="70" name=""/>
  <Phno type="wh" value="100" name="80">
    <Type>D</Type>
    <Mobile>7777777777</Mobile>
    <TPhone>6666666666</TPhone>
  </Phno>
  <Phno type="kwh" value="200" name="">
    <Type>E</Type>
    <Mobile>5555555555</Mobile>
    <TPhone>4444444444</TPhone>
  </Phno
</Student>

我的输入文件 需要用值属性值更新名称属性,只要类型属性有我工作的 kWh 脚本

xmlstarlet edit -N w="http://www.tibco.com/schemas/TrainingPOCs/SharedRec/Schemas/Schema.xsd" -u "/w:Student/w:Phno[@type='kwh']/@name" -x "/w:Student/w:Phno[@type='kwh']/@value" sample.xml

【问题讨论】:

    标签: xml shell xmlstarlet


    【解决方案1】:

    您快到了,但使用相对 XPath 表达式 -x / --expr 选项:

    xmlstarlet edit \
        -N w='http://www.tibco.com/schemas/TrainingPOCs/SharedRec/Schemas/Schema.xsd' \
        -u '/w:Student/w:Phno[@type="kwh"]/@name' \
        -x 'string(../@value)' file.xml
    

    或者,使用默认命名空间 shortcut,

    xmlstarlet ed -u '/_:Student/_:Phno[@type="kwh"]/@name' -x 'string(../@value)' file.xml
    

    并且可能添加一个-L / --inplace 选项(请参阅 xmlstarlet.txt) 用于就地编辑。

    【讨论】:

    • 非常感谢@urcodebetterznow,它现在运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2023-03-23
    • 2015-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多