How to declare XPath namespaces in xmlstarlet? How to declare XPath namespaces in xmlstarlet? xml xml

How to declare XPath namespaces in xmlstarlet?


Explicit Namespace Declaration

Adding -N s=http://www.w3.org/2000/svg and then using the s: namespace prefix works:

xmlstarlet ed  -N s=http://www.w3.org/2000/svg -u "/s:svg/s:defs/s:linearGradient[@id='linearGradient6204']/@id" -v 'linearGradient9999' text.txt

Implicit Declaration of Default Namespace

Starting with XMLStarlet v1.2.1, an explicit command line definition for the default namespace (such as is the case with OP's SVG file) can be avoided via use of an automated binding of _ to the default namespace:

1.3. A More Convenient Solution

XML documents can also use different namespace prefixes, on any element in the document. In order to handle namespaces with greater ease, XMLStarlet (versions 1.2.1+) will use the namespace prefixes declared on the root element of the input document. The default namespace will be bound to the prefixes _ and DEFAULT (in versions 1.5.0+).

So, the above command line could be re-written as:

xmlstarlet ed -u "/_:svg/_:defs/_:linearGradient[@id='linearGradient6204']/@id" -v 'linearGradient9999' text.txt