Command xpath
Name
xpath evaluate an XPATH expression and print the resultSynopsis
xpath [ serialization options ] [options] [xpath-expr]Options
-f,-file xpath-file | read xpath expression from file |
-i,-input context | use context as the source xml document, otherwise stdin if context is an XML expression which is an item then use it directly as the context item. |
-c,-context context | Use context as a literal context |
-cf,-context-file file | Use file as the name of an XML file for the context, or "-" for stdin |
-n | do not use a source context |
-q,-query xpath-expr | xpath expression |
-v | Read remaining pairs of arguments as name/value pairs to assign as xpath variables |
-nons | Do not import global namespace declarations from the shell environment |
-ns prefix=uri | Add a predeclared namespace delcaration |
-s,-string | Convert the result to a string if it is an attribute or element |
-b,-bool | Do not print anything. Treat the result as a boolean and exit with 0 if the value is true otherwise 1 |
-e,-exists | Execute xpath but do not print anything. The exit status is 0 if there was any selected values otherwise 1 |
Supports the standard [ serialization options ]
Note that the "-q" is optional. It is only required if you need to specify both -n and -v
Examples
xpath-n -q expr -v variable some-value
If the result (or any node of the result if a sequence) is an Attribute the it is converted to a string.-i is depreciated. It is a convenience option that does the following
- If context is an atomic value then treat as a filename to be loaded as the context item
- Otherwise treat as the literal context for the context item
-c or -cf are suggested which explicitly indicate if to use the expression literally or as a filename
Example
xls | xpath '//file[1]/@name/string()'
Result
.classpath
Simplified example, you don't need to use /string()
xls | xpath '//file[1]/@name'
Result
.classpath
Namespace example using xmlsh Namespaces support
declare namespace y=foo echo "<x:a xmlns:x='foo' />" | xpath /y:a
Namespace example using a renamed prefix for a namespace
echo "<x:a xmlns:x='foo' />" | xpath -nons -ns y=foo /y:a
Result
<x:a xmlns:x="foo"/>
Return Value
Returns 0 if the the xpath expression executed successfully and a non-empty result is returned. otherwise 1Commands
CategoryCommands