加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xmlstarlet选择值

发布时间:2020-12-16 07:44:01 所属栏目:百科 来源:网络整理
导读:这是xml数据: DATA VERSION="1.0" TABLES ITEM identifyer V="1234"/identifyer property1 V="abcde"/property1 Property2 V="qwerty"/property2 /ITEM ITEM identifyer V="5678"/identifyer Property1 V="zyxwv"/property1 Property2 V="dvorak"/property2
这是xml数据:
<DATA VERSION="1.0">
  <TABLES>
    <ITEM>
      <identifyer V="1234"></identifyer>
      <property1 V="abcde"></property1>
      <Property2 V="qwerty"></property2>
    </ITEM>
    <ITEM>
      <identifyer V="5678"></identifyer>
      <Property1 V="zyxwv"></property1>
      <Property2 V="dvorak"></property2>
    </ITEM>
  </TABLES>
</DATA>

我正在尝试找到物品的属性2,其中识别者的值为1234.我可以选择数据:

$xmlstarlet sel -t -c "/DATA/TABLES/ITEM/identifyer [@V=1234]" test.xml 
<identifyer V="1234"/>

两种类型的输出将是可取的:

$xmlstarlet <some magic>
<identifyer V="1234"></identifyer>
<property1 V="abcde"></property1>
<Property2 V="qwerty"></property2>

和:

$xmlstarlet <some magic>
qwerty
关键是从ITEM节点开始,而不是识别器:
$xmlstarlet sel -t -c "/DATA/TABLES/ITEM[identifyer/@V=1234]" test.xml
<ITEM>
      <identifyer V="1234"/>
      <property1 V="abcde"/>
      <Property2 V="qwerty"/>
    </ITEM>

然后你可以选出你想要的位:

$xmlstarlet sel -t -c "/DATA/TABLES/ITEM[identifyer/@V=1234]/*" test.xml
<identifyer V="1234"/><property1 V="abcde"/><Property2 V="qwerty"/>
$xmlstarlet sel -t -v "/DATA/TABLES/ITEM[identifyer/@V=1234]/Property2/@V" test.xml
qwerty

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读