我的脚本是一个简单的单行程序,它使用curl来获取带有xml输出的URL。我正在尝试仅输出以下标记中的文本:
<TEXT>No Hosts Queued for Purging</TEXT>
我的剧本:
curl -u username:password -H 'X-Requested-With:QualysApiExplorer' 'https://qualysapi.qualys.com:443/api/2.0/fo/asset/host/' -d "action=purge&ips=$1&" | xmlstarlet sel -t -m '/BATCH_RETURN/RESPONSE/BATCH_LIST/BATCH/TEXT' -v "."
不使用xmlstarlet时,curl输出如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE BATCH_RETURN SYSTEM "https://qualysapi.qualys.com/api/2.0/batch_return.dtd">
<BATCH_RETURN>
<RESPONSE>
<DATETIME>2017-04-20T20:27:15Z</DATETIME>
<BATCH_LIST>
<BATCH>
<CODE>1921</CODE>
<TEXT>No Hosts Queued for Purging</TEXT>
</BATCH>
</BATCH_LIST>
</RESPONSE>
</BATCH_RETURN>
使用xmlstarlet时出现以下错误:
failed to load external entity "https://qualysapi.qualys.com/api/2.0/batch_return.dtd"
TYPE BATCH_RETURN SYSTEM "https://qualysapi.qualys.com/api/2.0/batch_return.dtd"
^
答案 0 :(得分:2)
您可以在执行查询(fo -D
)之前使用sel
删除doctype:
xml fo --help
XMLStarlet Toolkit: Format XML document
Usage: xml fo [<options>] <xml-file>
where <options> are
-D or --dropdtd - remove the DOCTYPE of the input docs
以下方法可行:
curl -u username:password \
-H 'X-Requested-With:QualysApiExplorer' \
'https://qualysapi.qualys.com:443/api/2.0/fo/asset/host/' \
-d "action=purge&ips=$1&" | \
xmlstarlet fo -D | \
xmlstarlet sel -t -v "BATCH_RETURN/RESPONSE/BATCH_LIST/BATCH/TEXT"