paulhr
paulhr

Reputation: 379

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>

I am trying to get an xmllint –xpath expression to return just the value of an XML Element. I have tried…

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

But the result is one or more of the following errors.
bash: syntax error near unexpected token `('
XPath error : Invalid type
XPath evaluation failure

If the following is used the XML Element attribute name and value is return. I just want the value.

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

Results below have a leading space.
Host_FQDN="myhost.com"

What I find odd is that the count() function works just fine. See the example below.

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

The code snippet that invokes the xmllint command in a BASH Script is below.

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

Upvotes: 0

Views: 1313

Answers (1)

zx485
zx485

Reputation: 29052

bash: syntax error near unexpected token `('

The char ( is recognized by bash to be a bash token, so you would have to escape it, but that is not what you want. You want to pass the XPath expression to xmllint, so the solution is to put the expression in brackets:

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

Your second error was to interrupt the string(...) function in the middle of your XPath expression. So a correct version of your attempts would look like

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

The above expression returns only the string value of the first Host element's Host_FQDN attribute.


Just some kind of a horrid note:
You used the quote char instead of the " quote char!!!
That could have been a reason for your malfunctions as well.

Upvotes: 2

Related Questions