使用 xmlstarlet 进行 XPath 查询 [英] XPath query with xmlstarlet
问题描述
我有与此类似的 XML:
I have XML similar to this one:
<orders>
<orderCompleteRequestType>
<Head>
<Aufnr>11111</Aufnr>
</Head>
<Register>
<Id>180</Id>
<value1>11</value1>
<value2>22</value2>
</Register>
<Register>
<Id>181</Id>
<value1>3</value1>
<value2>43</value2>
</Register>
<Register>
<Id>160</Id>
<value1>5</value1>
<value2>25</value2>
</Register>
</orderCompleteRequestType>
<orderCompleteRequestType>
<Head>
<Aufnr>22222</Aufnr>
</Head>
<Register>
<Id>280</Id>
<value1>1</value1>
<value2>12</value2>
</Register>
<Register>
<Id>160</Id>
<value1>12</value1>
<value2>7</value2>
</Register>
<Register>
<Id>281</Id>
<value1>94</value1>
<value2>22</value2>
</Register>
</orderCompleteRequestType>
</orders>
我想以 CSV 格式从每个orderCompleteRequestType"结构中选择一些值:
I want to select in CSV format some values from each "orderCompleteRequestType" structure:
- 负责人/Aufrn
- 注册/ID
- 注册/值1
- 注册/值2
使用以下命令行时:
xmlstarlet sel -T -t -m "/orders/orderCompleteRequestType" -v "Head/Aufnr" -o ";" -v "Register/Id" -o ";" -v "Register/value1" -o ";" -v "Register/value2" -n -n test.xml
我明白了:
11111;180
181
160;11
3
5;22
43
25
22222;280
160
281;1
12
94;12
7
22
所以,首先是 Register/Id 节点的所有值,然后是所有 Register/value1,最后是所有 Register/value2,但我期望的是这样的:
so, first goes all values of Register/Id nodes, next all Register/value1's and finally all Register/value2's, but rather of this I expect something like:
11111;180;11;22
11111;181;3;43
11111;160;5;25
22222;280;1;12
22222;160;12;7
22222;281;94;22
谁能帮帮我,因为我的大脑拒绝工作...
Can anyone help me, because my brain rejecting to do work...
推荐答案
与其匹配 orderCompleteRequestType
,不如考虑匹配 Register
...
Instead of matching orderCompleteRequestType
, consider matching Register
instead...
xmlstarlet sel -T -t -m "/orders/orderCompleteRequestType/Register" -v "concat(../Head/Aufnr,';',Id,';',value1,';',value2)" -n test.xml
输出...
11111;180;11;22
11111;181;3;43
11111;160;5;25
22222;280;1;12
22222;160;12;7
22222;281;94;22
每个 orderCompleteRequestType
之间没有额外的换行符,但也许这没什么大不了的?如果是,那么编写一个 XSLT 并使用 xmlstarlet 调用它可能会更容易.
There isn't an extra newline between each orderCompleteRequestType
, but maybe that's not a big deal? If it is, it might be easier to just write an XSLT and call that with xmlstarlet.
这篇关于使用 xmlstarlet 进行 XPath 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!