使用xmlstarlet进行元素标签的模式匹配。

huangapple go评论65阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;us-ascii&quot;?&gt;
&lt;ST&gt;
  &lt;Data3020&gt;
    &lt;value&gt;110&lt;/value&gt;
    &lt;detailType&gt;N/A&lt;/detailType&gt;
  &lt;/Data3020&gt;
  &lt;Data3030&gt;
    &lt;value&gt;10&lt;/value&gt;
    &lt;detailType&gt;N/A&lt;/detailType&gt;
  &lt;/Data3030&gt;
  &lt;Data3040&gt;
    &lt;value&gt;70&lt;/value&gt;
    &lt;detailType&gt;N/A&lt;/detailType&gt;
  &lt;/Data3040&gt;
  &lt;Data3080&gt;
    &lt;value&gt;770&lt;/value&gt;
    &lt;detailType&gt;N/A&lt;/detailType&gt;
  &lt;/Data3080&gt;
  &lt;Data3090&gt;
    &lt;value&gt;44&lt;/value&gt;
    &lt;detailType&gt;N/A&lt;/detailType&gt;
  &lt;/Data3090&gt;
&lt;/ST&gt;

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 &quot;/ST/*&quot; -v &quot;name()&quot; -nl test.xml

</br>

Data3020
Data3030
Data3040
Data3080
Data3090

</br>

xmlstarlet sel --template --value-of &quot;//ST&quot; test.xml


110
N/A


10
N/A


70
N/A


770
N/A


44
N/A

答案1

得分: 1

使用 [tag:xidel]:

xidel --xquery '&#39;
    for $n in /ST/*
        let $name := $n/name()
        let $val  := $n/value
        let $type := $n/detailType
    return concat($name, &quot;|&quot;, $val, &quot;|&quot;, $type, &quot;|&quot;)
&#39; file

或者使用评论中提供的XPath @Yitzhak Khabinsky:

xidel -e '&#39;/ST/*/concat(name(),&quot;|&quot;, value, &quot;|&quot;, detailType)&#39;' file

或者使用saxon-lint(我的项目):

saxon-lint --xpath '&#39;/ST/*/concat(name(),&quot;|&quot;, value, &quot;|&quot;, detailType)&#39;' file

或者使用 [tag:xmlstarlet] 通过 @Cyrus 的评论:

xmlstarlet sel -t -m '&#39;/ST/*&#39;' \
    -v &quot;concat(name(),&#39;|&#39;,value,&#39;|&#39;,detailType,&#39;|&#39;)&quot; \
    -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 &#39;
    for $n in /ST/*
        let $name := $n/name()
        let $val  := $n/value
        let $type := $n/detailType
    return concat($name, &quot;|&quot;, $val, &quot;|&quot;, $type, &quot;|&quot;)
&#39; file

Or re-using the XPath @Yitzhak Khabinsky given in comments:

xidel -e &#39;/ST/*/concat(name(),&quot;|&quot;, value, &quot;|&quot;, detailType)&#39; file

or with saxon-lint (my own project):

saxon-lint --xpath &#39;/ST/*/concat(name(),&quot;|&quot;, value, &quot;|&quot;, detailType)&#39; file

Or with [tag:xmlstarlet] via the comment of @Cyrus:

xmlstarlet sel -t -m &#39;/ST/*&#39; \
    -v &quot;concat(name(),&#39;|&#39;,value,&#39;|&#39;,detailType,&#39;|&#39;)&quot; \
    -n file

</br>

Data3020|110|N/A|
Data3030|10|N/A|
Data3040|70|N/A|
Data3080|770|N/A|
Data3090|44|N/A|

huangapple
  • 本文由 发表于 2023年4月20日 04:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定