【问题标题】:Extract specific values from XML configuration从 XML 配置中提取特定值
【发布时间】:2022-01-05 08:38:05
【问题描述】:

输入 XML 看起来像几个块:

<Parties>
  <Party compid="CUST1" side="1">
                <Connections>
                    <SocketConnection listenport="9029" />
                </Connections>
   </Party>
</Parties>

我的目标是为每个客户提取他的特定参数,例如: compid;监听端口

现在我可以让所有客户使用

xmlstarlet fo -D config.xml | xmlstarlet select -T -t -m '//Parties/Party' -v '@compid' -nl

下一步将是每个客户获取他的监听端口的循环,但是

xmlstarlet fo -D config.xml | xmlstarlet select -T -t -m '//Parties/Party[@compid="CUST1"]' -v 'Connections/SocketConnection/@listenport'

或任何其他尝试都不会返回任何内容。 我在使用[@value=string] 进行过滤时是否遗漏了什么?

提前致谢!

【问题讨论】:

    标签: xml bash xpath xmlstarlet


    【解决方案1】:

    以你的例子和xmlstarlet:

    xmlstarlet select --text --template --match '//Parties/Party' --value-of \
      'concat(@compid,";",Connections/SocketConnection/@listenport)' -n config.xml
    

    输出:

    CUST1;9029

    见:xmlstarlet select --help

    【讨论】:

      猜你喜欢
      • 2014-08-06
      • 2018-07-30
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2014-02-02
      • 2016-12-22
      相关资源
      最近更新 更多