首页 > 解决方案 > 如何让 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})

标签: xmlbashxmllint

解决方案



推荐阅读