如何将 xmllint 获取到 return 只是 XML 元素属性?

How to get xmllint to return just the XML Element attribute?

<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>

我正在尝试获取 return 的 xmllint –xpath 表达式,只是 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
xmllint --xpath “(string(//Hosts/Host)[1]/@Host_FQDN)” /myFile.xml

但结果是以下一个或多个错误。
bash: 意外标记附近的语法错误 `('
XPath 错误:无效类型
XPath 求值失败

如果使用以下 XML 元素属性名称和值为 return。我只想要价值。

xmllint --xpath (//Hosts/Host)[1]/@Host_FQDN /myFile.xml

下面的结果有前导 space。
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})

bash: syntax error near unexpected token `('

字符 ( 被 bash 识别为 bash 标记,因此您必须转义它,但这不是您想要的。你想将 XPath 表达式传递给 xmllint,所以解决方法是将表达式放在括号中:

xmllint --xpath '//Hosts/Host[1]/@Host_FQDN' myFile.xml

您的第二个错误是 中断 XPath 表达式中间的 string(...) 函数。所以你尝试的正确版本看起来像

xmllint --xpath "string(//Hosts/Host[1]/@Host_FQDN)" myFile.xml

上面的表达式returns只是第一个Host元素的Host_FQDN属性的字符串值。


只是某种可怕的音符:
您使用了 引号字符而不是 " 引号字符!!!
这也可能是您出现故障的原因。