使用 xmllint 根据另一个标签限定符的值提取标签内容
Extract tag contents based on value of another tag qualifier using xmllint
我正在尝试使用 xmllint 从标签中提取数据(如果条件存在于前一个标签中)。我知道可能有更好的工具,但我仅限于 xmllint and/or 系统标准命令,如 sed、awk 等
xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<MainGroup>
<MainGroupEntry name="aaa" function="xxx">
<EntryType type="AAA"/>
<EntryDescription>Capture This A</EntryDescription>
<EntryRandomList>Just,a,random,list,of,things,to,discard</EntryRandomList>
</MainGroupEntry>
<MainGroupEntry name="aaa" function="xxx">
<EntryType type="AAA"/>
<EntryDescription>Capture This A</EntryDescription>
<EntryRandomList>Just,a,random,list,of,things,to,discard</EntryRandomList>
</MainGroupEntry>
<MainGroupEntry name="bbb" function="yyy">
<EntryType type="BBB"/>
<EntryDescription>Capture This B</EntryDescription>
<EntryRandomList>Just,a,random,list,of,things,to,discard</EntryRandomList>
</MainGroupEntry>
<MainGroupEntry name="bbb" function="yyy">
<EntryType type="BBB"/>
<EntryDescription>Capture This B</EntryDescription>
<EntryRandomList>Just,a,random,list,of,things,to,discard</EntryRandomList>
</MainGroupEntry>
</MainGroup>
我“试图做的是;对于每个 Entry type="AAA"
,打印随附的 EntryDescription
。我尝试了不同的变体:xmllint --xpath '//MainGroupEntry/EntryType[@type="AAA"]/EntryDescription/text()' my_file.xml
但我总是得到一个空的 XPath 集。如果我放弃尝试获取描述文本,我可以看到符合我的 'type' 条件的条目:
xmllint --xpath '//MainGroupEntry/EntryType[@type="AAA"]' my_file.xml
<EntryType type="AAA"/><EntryType type="AAA"/>
我似乎无法弄清楚如何只从描述字段中获取文本。想法?
您可以使用 following-sibling
轴和 text()
函数从描述中仅提取文本:
xmllint --xpath '/MainGroup/MainGroupEntry/EntryType[@type="AAA"]/following-sibling::EntryDescription/text()' file.xml
要分隔文本,您可以使用 --shell
选项和 cat
:
echo 'cat /MainGroup/MainGroupEntry/EntryType[@type="AAA"]/following-sibling::EntryDescription/text()' \
| xmllint --shell file.xml
您可能需要 | grep -v ' -----\|/ >'
输出以删除分隔符和提示。
我正在尝试使用 xmllint 从标签中提取数据(如果条件存在于前一个标签中)。我知道可能有更好的工具,但我仅限于 xmllint and/or 系统标准命令,如 sed、awk 等
xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<MainGroup>
<MainGroupEntry name="aaa" function="xxx">
<EntryType type="AAA"/>
<EntryDescription>Capture This A</EntryDescription>
<EntryRandomList>Just,a,random,list,of,things,to,discard</EntryRandomList>
</MainGroupEntry>
<MainGroupEntry name="aaa" function="xxx">
<EntryType type="AAA"/>
<EntryDescription>Capture This A</EntryDescription>
<EntryRandomList>Just,a,random,list,of,things,to,discard</EntryRandomList>
</MainGroupEntry>
<MainGroupEntry name="bbb" function="yyy">
<EntryType type="BBB"/>
<EntryDescription>Capture This B</EntryDescription>
<EntryRandomList>Just,a,random,list,of,things,to,discard</EntryRandomList>
</MainGroupEntry>
<MainGroupEntry name="bbb" function="yyy">
<EntryType type="BBB"/>
<EntryDescription>Capture This B</EntryDescription>
<EntryRandomList>Just,a,random,list,of,things,to,discard</EntryRandomList>
</MainGroupEntry>
</MainGroup>
我“试图做的是;对于每个 Entry type="AAA"
,打印随附的 EntryDescription
。我尝试了不同的变体:xmllint --xpath '//MainGroupEntry/EntryType[@type="AAA"]/EntryDescription/text()' my_file.xml
但我总是得到一个空的 XPath 集。如果我放弃尝试获取描述文本,我可以看到符合我的 'type' 条件的条目:
xmllint --xpath '//MainGroupEntry/EntryType[@type="AAA"]' my_file.xml
<EntryType type="AAA"/><EntryType type="AAA"/>
我似乎无法弄清楚如何只从描述字段中获取文本。想法?
您可以使用 following-sibling
轴和 text()
函数从描述中仅提取文本:
xmllint --xpath '/MainGroup/MainGroupEntry/EntryType[@type="AAA"]/following-sibling::EntryDescription/text()' file.xml
要分隔文本,您可以使用 --shell
选项和 cat
:
echo 'cat /MainGroup/MainGroupEntry/EntryType[@type="AAA"]/following-sibling::EntryDescription/text()' \
| xmllint --shell file.xml
您可能需要 | grep -v ' -----\|/ >'
输出以删除分隔符和提示。