Linux Bash XMLLINT with XPATH

Today I will learn how to use xmllint correctly. It seems that this is not well described or explained. I plan to use a single language resource file to run my entire system. I have a mixture of bash scripts and php pages that should read from this language file.

I am currently using the following format in my XML file en.xml:

<?xml version="1.0" encoding="utf-8"?> <resources> <item id="index.php"> <label>LABEL</label> <value>VALUE</value> <description>DESCRIPTION</description> </item> <item id="config.php"> <label>LABEL</label> <value>VALUE</value> <description>DESCRIPTION</description> </item> </resources> 

Now I need to start with the bash script line, which should pull the data values ​​from the xml file. For example, I want to get the DESCRIPTION value from the index.php element.

I used

 xmllint --xpath 'string(//description)' /path/en.xml 

for another layout that worked, but now when I change the layout of my xml file, I get lost as to what is the best way to target a specific <item> , and then expand its child in a bash script.

Can someone help with the xmllint --xpath line to get this value?

+5
source share
3 answers

what is the best way to target a specific one and then drill down its child

The correct XPath expression for this is:

 /resources/item[@id="index.php"]/description/text() 

In plain English: Start with a node document, on a resources document element, on its child item element, but only if the id attribute value is "index.php", on its child description element and get its text value.

I use xmllint to validate XML documents, but never for path expressions. In the bash shell (at least with Mac OS) there is an even simpler tool for evaluating XPath expressions called "xpath":

 $ xpath en.xml '/resources/item[@id="index.php"]/description/text()' 

Then the following result is obtained:

 Found 1 nodes: -- NODE -- DESCRIPTION 

If you still prefer xmllint, use it as follows:

 $ xmllint --xpath '/resources/item[@id="index.php"]/description/text()' en.xml > result.txt 

By default, --xpath implies --noout , which prevents xmllint from being output. Redirect the output to a file.

 $ cat result.txt DESCRIPTION 
+6
source

My favorite is xmlstarlet , because it seems more powerful than xmllint :

 xmlstarlet sel -t -v '/resources/item[@id="index.php"]/description/text()' en.xml 
+1
source

I had the same problem a few minutes ago and I saw this message.

After hacking a bit, I found the following solution to extract the city:

 (wget 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=53.244921,-2.479539&sensor=true' -O dummy.xml -o /dev/null;xmllint --format --xpath '/GeocodeResponse/result[type = "postal_town"]/address_component[type = "postal_town"]/short_name/node()' dummy.xml) 

You must specify the correct X-Path to obtain the desired XML tag, and then return only the node value.

0
source

Source: https://habr.com/ru/post/1206071/


All Articles