xml - 如何让 xmllint 只返回 XML Element 属性?
问题描述
<Hosts>
<Host Host_FQDN="myhost00.com">
<ip_Address>text</ip_Address>
<ip_Address>text</ip_Address>
<ip_Address>text</ip_Address>
<Host_Directory_To_Clean Perform="text">
<Perform_Deletes>true</Perform_Deletes>
<Delete_Files_AbsolutePath>String</Delete_Files_AbsolutePath>
<Delete_Files_Recursively>true</Delete_Files_Recursively>
<Delete_Files_Selection>String</Delete_Files_Selection>
<Delete_Files_Selection>String</Delete_Files_Selection>
<Delete_Files_Selection>String</Delete_Files_Selection>
</Host_Directory_To_Clean>
<Host_FileSystem_Backup Perform="text">
<Perform_Backup>False</Perform_Backup>
<Backup_Full_Path>String</Backup_Full_Path>
<Backup_File_Name>String</Backup_File_Name>
</Host_FileSystem_Backup>
</Host>
</Hosts>
<Hosts>
<Host Host_FQDN=" myhost01.com">
<ip_Address>text</ip_Address>
<ip_Address>text</ip_Address>
<ip_Address>text</ip_Address>
<Host_Directory_To_Clean Perform="text">
<Perform_Deletes>true</Perform_Deletes>
<Delete_Files_AbsolutePath>String</Delete_Files_AbsolutePath>
<Delete_Files_Recursively>true</Delete_Files_Recursively>
<Delete_Files_Selection>String</Delete_Files_Selection>
<Delete_Files_Selection>String</Delete_Files_Selection>
<Delete_Files_Selection>String</Delete_Files_Selection>
</Host_Directory_To_Clean>
<Host_FileSystem_Backup Perform="text">
<Perform_Backup>N</Perform_Backup>
<Backup_Full_Path>String</Backup_Full_Path>
<Backup_File_Name>String</Backup_File_Name>
</Host_FileSystem_Backup>
</Host>
</Hosts>
<Hosts>
<Host Host_FQDN=" myhost02.com">
<ip_Address>text</ip_Address>
<ip_Address>text</ip_Address>
<ip_Address>text</ip_Address>
<Host_Directory_To_Clean Perform="text">
<Perform_Deletes>true</Perform_Deletes>
<Delete_Files_AbsolutePath>String</Delete_Files_AbsolutePath>
<Delete_Files_Recursively>true</Delete_Files_Recursively>
<Delete_Files_Selection>String</Delete_Files_Selection>
<Delete_Files_Selection>String</Delete_Files_Selection>
<Delete_Files_Selection>String</Delete_Files_Selection>
</Host_Directory_To_Clean>
<Host_FileSystem_Backup Perform="text">
<Perform_Backup>NO</Perform_Backup>
<Backup_Full_Path>String</Backup_Full_Path>
<Backup_File_Name>String</Backup_File_Name>
</Host_FileSystem_Backup>
</Host>
</Hosts>
我正在尝试获取一个 xmllint –xpath 表达式来仅返回 XML 元素的值。我试过了……</p>
xmllint --xpath string(//Hosts/Host)[1]/@Host_FQDN /myFile.xml
xmllint --xpath “string(//Hosts/Host)[1]/@Host_FQDN” /myFile.xml
xmllint --xpath (string(//Hosts/Host)[1]/@Host_FQDN) /myFile.xml
xmllint --xpath “(string(//Hosts/Host)[1]/@Host_FQDN)” /myFile.xml
但结果是以下一个或多个错误。
bash:意外标记附近的语法错误`('
XPath错误:无效类型
XPath评估失败
如果使用以下内容,则返回 XML 元素属性名称和值。我只想要价值。
xmllint --xpath (//Hosts/Host)[1]/@Host_FQDN /myFile.xml
下面的结果有一个前导空格。
Host_FQDN="myhost.com"
我觉得奇怪的是 count() 函数工作得很好。请参见下面的示例。
xmllint --xpath count(//Hosts/Host)[1]/@Host_FQDN /myFile.xml
下面是在 BASH 脚本中调用 xmllint 命令的代码片段。
unset wwString
wwString="xmllint --xpath string(//"
wwString="$wwString$xmlElementName)"
wwString="$wwString["
wwString="$wwString$i]/@"
wwString="$wwString$xmlElementAttributeName "
wwString="$wwString$propertiesFileFullPath"
wwString="$wwString/"
wwString="$wwString$fullXMLpropertiesFileName"
wwExtracted=$(${wwString})
解决方案
推荐阅读
- proxy - 特使中的 JSON 排序和记录响应/请求正文
- spring - 需要 dataSource 或 dataSourceClassName 或 jdbcUrl
- python - 如何在 Health-News-dataset.zip 文件下的 train 文件夹中加载文件
- apache-flink - Apache Flink 接收数据到 StatsD
- excel - 在使用 libxl 库时,以下部分中使用的这些命令是什么?
- django - 在 Django 模板中访问字典
- java - 使用 intelliJ 时 maven pom.xml 中的未知错误
- c# - 项目未添加到类列表中?
- javascript - 使用 Fetch API 将 JSON 数据发送到 WordPress Admin Ajax 时出现 400 错误
- batch-file - 从批处理文件启动 Docker Desktop 和 Docker 堆栈 (Windows)