英文:
Using xmlstarlet to pattern match element tags
问题
我正在尝试确定如何使用xmlstarlet
来获取xml
标签和值,当标签元素不是固定的时。
例如,
输入文件:
<?xml version="1.0" encoding="us-ascii"?>
<ST>
<Data3020>
<value>110</value>
<detailType>N/A</detailType>
</Data3020>
<Data3030>
<value>10</value>
<detailType>N/A</detailType>
</Data3030>
<Data3040>
<value>70</value>
<detailType>N/A</detailType>
</Data3040>
<Data3080>
<value>770</value>
<detailType>N/A</detailType>
</Data3080>
<Data3090>
<value>44</value>
<detailType>N/A</detailType>
</Data3090>
</ST>
期望输出:
BoxName|value|detailType|
Data3020|100|N/A|
Data3030|10|N/A|
Data3030|70|N/A|
Data3040|770|N/A|
Data3050|44|N/A|
我尝试以下方法来分别获取标签名称和值,但我无法想出如何将它们组合起来:
xmlstarlet sel -t -m "/ST/*" -v "name()" -nl test.xml
Data3020
Data3030
Data3040
Data3080
Data3090
xmlstarlet sel --template --value-of "//ST" test.xml
110
N/A
10
N/A
70
N/A
770
N/A
44
N/A
英文:
I am trying to determine how to use xmlstarlet
to get the xml
tag and values when the tag elements aren't constant.
for example,
Input file:
<?xml version="1.0" encoding="us-ascii"?>
<ST>
<Data3020>
<value>110</value>
<detailType>N/A</detailType>
</Data3020>
<Data3030>
<value>10</value>
<detailType>N/A</detailType>
</Data3030>
<Data3040>
<value>70</value>
<detailType>N/A</detailType>
</Data3040>
<Data3080>
<value>770</value>
<detailType>N/A</detailType>
</Data3080>
<Data3090>
<value>44</value>
<detailType>N/A</detailType>
</Data3090>
</ST>
Expected Output:
BoxName|value|detailType|
Data3020|100|N/A|
Data3030|10|N/A|
Data3030|70|N/A|
Data3040|770|N/A|
Data3050|44|N/A|
I tried the following to get the tag name and the values separately but I can't figure out how to combine them
xmlstarlet sel -t -m "/ST/*" -v "name()" -nl test.xml
</br>
Data3020
Data3030
Data3040
Data3080
Data3090
</br>
xmlstarlet sel --template --value-of "//ST" test.xml
110
N/A
10
N/A
70
N/A
770
N/A
44
N/A
答案1
得分: 1
使用 [tag:xidel]:
xidel --xquery ''
for $n in /ST/*
let $name := $n/name()
let $val := $n/value
let $type := $n/detailType
return concat($name, "|", $val, "|", $type, "|")
' file
或者使用评论中提供的XPath @Yitzhak Khabinsky:
xidel -e ''/ST/*/concat(name(),"|", value, "|", detailType)'' file
或者使用saxon-lint
(我的项目):
saxon-lint --xpath ''/ST/*/concat(name(),"|", value, "|", detailType)'' file
或者使用 [tag:xmlstarlet] 通过 @Cyrus 的评论:
xmlstarlet sel -t -m ''/ST/*'' \
-v "concat(name(),'|',value,'|',detailType,'|')" \
-n file
Data3020|110|N/A|
Data3030|10|N/A|
Data3040|70|N/A|
Data3080|770|N/A|
Data3090|44|N/A|
英文:
With [tag:xidel]:
xidel --xquery '
for $n in /ST/*
let $name := $n/name()
let $val := $n/value
let $type := $n/detailType
return concat($name, "|", $val, "|", $type, "|")
' file
Or re-using the XPath @Yitzhak Khabinsky given in comments:
xidel -e '/ST/*/concat(name(),"|", value, "|", detailType)' file
or with saxon-lint
(my own project):
saxon-lint --xpath '/ST/*/concat(name(),"|", value, "|", detailType)' file
Or with [tag:xmlstarlet] via the comment of @Cyrus:
xmlstarlet sel -t -m '/ST/*' \
-v "concat(name(),'|',value,'|',detailType,'|')" \
-n file
</br>
Data3020|110|N/A|
Data3030|10|N/A|
Data3040|70|N/A|
Data3080|770|N/A|
Data3090|44|N/A|
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论