【问题标题】:Shell Command to extract a value from an xml file into the Shell script用于将 xml 文件中的值提取到 Shell 脚本中的 Shell 命令
【发布时间】:2017-12-22 08:05:18
【问题描述】:

我有以下 xml 文本

<?xml version="1.0" encoding="UTF-8"?>

<buildinfo xmlns:xsi="http&#x3a;&#x2f;&#x2f;www.w3.org&#x2f;2001&#x2f;XMLSchema-instance" xmlns="https&#x3a;&#x2f;&#x2f;abc.com&#x2f;schema&#x2f;4.0&#x2f;buildinfo" xsi:schemaLocation="https&#x3a;&#x2f;&#x2f;abc.com&#x2f;schema&#x2f;4.0&#x2f;buildinfo https&#x3a;&#x2f;&#x2f;abc.com&#x2f;resource&#x2f;4.0&#x2f;buildinfo.xsd" buildinfo_version="1" account_id="23456" app_id="456321" sandbox_id="223457" build_id="1234567">    
<build version="1 Jul 2016 Static" build_id="1234567" submitter="David" platform="Not Specified" lifecycle_stage="Not Specified" results_ready="true" scan_overdue="false" legacy_scan_engine="false">
  <analysis_unit analysis_type="Static" published_date="2016-07-1T15&#x3a;58&#x3a;55-04&#x3a;00" published_date_sec="324532678" status="Results Ready" engine_version="2341267"/>
</build>
</buildinfo>

我有一个需要此文件中的 build_id 的 shell 脚本(build_id 不断变化)。如何将此文件中的“build_id”值提取到该脚本中的变量中。

【问题讨论】:

    标签: xml shell xpath xmllint


    【解决方案1】:

    xmlstarlet 对于查询 XML 文件很有用

    示例:

    xmlstarlet sel -t -m 'buildinfo' sample.xml
    
    Output: 
    buildinfo 
    buildinfo/build 
    buildinfo/build/analysis_unit
    

    更多信息:http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html

    【讨论】:

    • 当我使用上述命令时,我似乎没有得到任何输出。 :(
    • 请检查您的安装,我在 MacOS 上试过,效果很好。
    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多