从 XML 配置中提取特定值

Extract specific values from XML configuration

输入XML看起来像几个块:

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

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

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

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'

或任何其他尝试 returns 什么都不做。 我在使用 [@value=string] 过滤时是否遗漏了什么?

提前致谢!

以你的例子和xmlstarlet:

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

输出:

CUST1;9029

参见:xmlstarlet select --help