Today I get to learn how to use xmllint properly. It does not seem to be well covered 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 must read from this language file.
Currently I am using the following format in my xml file en.xml:
<?xml version="1.0" encoding="utf-8"?>
xmllint --xpath 'string(//description)' /path/en.xml
how best to target a specific and then drill down to its child element
The correct XPath expression to do this is:
In plain English: Start from the document node, to the document element
resources, on to its child
item, but only if the value of the
id attribute is "index.php", on to its child
description and retrieve its textual value.
I use xmllint to validate XML documents, but never for path expressions. In a 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 in the following way:
$ xmllint --xpath '/resources/item[@id="index.php"]/description/text()' en.xml > result.txt
--noout, which prevents xmllint from outputting anything. Redirect the output to a file.
$ cat result.txt DESCRIPTION