【发布时间】:2020-12-24 00:55:28
【问题描述】:
问题> 我有一些从 API 调用返回的 xml,存储在 get_build_info.xml 中。我正在尝试从该 xml 中获取一个属性 build_id。这是xml:
<?xml version="1.0" encoding="UTF-8"?>
<buildinfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://analysiscenter.veracode.com/schema/4.0/buildinfo" xsi:schemaLocation="https://analysiscenter.veracode.com/schema/4.0/buildinfo https://analysiscenter.veracode.com/resource/4.0/buildinfo.xsd" buildinfo_version="1.4" account_id="1234" app_id="010101" sandbox_id="020202" build_id="987654321"><build version="4 Sep 2020 Static (2)" build_id="987654321" submitter="Someone Else" platform="Not Specified" lifecycle_stage="Not Specified" results_ready="true" policy_name="Some Development App Policy" policy_version="7" policy_compliance_status="Conditional Pass" rules_status="Not Assessed" grace_period_expired="false" scan_overdue="false" legacy_scan_engine="false">
<analysis_unit analysis_type="Static" published_date="2020-09-04T11:44:09-04:00" published_date_sec="1599234249" status="Results Ready" engine_version="20200821190810"/>
</build>
</buildinfo>
我尝试过的> 以下及其许多其他变体:
xmllint --xpath 'string(//xml/buildinfo/@build_id)' get_build_info.xml
xmllint --xpath 'string(//buildinfo/@build_id)' get_build_info.xml
xmllint --xpath 'string(/xml/buildinfo/@build_id)' get_build_info.xml
xmllint --xpath '(//xml/buildinfo/build_id/text())' get_build_info.xml
xmllint --xpath '(/xml/buildinfo/build_id/text())' get_build_info.xml
最后两个至少产生某种输出,尽管“XPath 集为空”。我在 --xpath 中使用那个 'string( +...+ @build_id 的前几个,我什么也没返回。这些似乎也从 bash 退出 0,所以我能做到的没有语法问题告诉。就像我说的,我是 n00b。我在 stackoverflow 上查看了其他流行的票证,这就是让我走到这一步的原因。我很可能不会在这里考虑一些明显的事情,所以假设我对 bash 一无所知(这几乎是真的)。我很高兴得到指示,让我自己去寻找答案。
【问题讨论】:
-
这是关于命名空间的,这是一个有用的解决方案:stackoverflow.com/questions/8264134/…
-
谢谢!我现在正在检查链接,似乎正是我的问题以及我需要阅读的内容。
-
如果您对替代方案感兴趣,例如 xidel,您可以这样做:
xidel -s get_build_info.xml -e '//@build_id'。 -
也会检查一下。谢谢