Fish Shell:
Working with XML
How to:
Fish doesn’t have built-in XML parsing, so you’ll lean on external tools like xmllint
or xmlstarlet
. Here’s a snippet to read values:
# Parse XML using xmlstarlet
echo '<root><element>Hello World</element></root>' | xmlstarlet sel -t -v "/root/element"
Output:
Hello World
To edit XML, use this:
# Edit XML element using xmlstarlet
echo '<root><element>Old Value</element></root>' | xmlstarlet ed -u "/root/element" -v 'New Value'
Output:
<?xml version="1.0"?>
<root>
<element>New Value</element>
</root>
Deep Dive:
XML’s been around since the late ’90s, crafted for readability and machine-friendliness. While JSON’s usurped some of XML’s popularity due to simplicity, XML remains entrenched where document validation and namespaces are key.
Alternatives? Sure—JSON, YAML, or even binary formats like Protocol Buffers for those performance-intensive apps. But XML’s schema and XSLT (for XML transformations) can be deal-breakers for complex scenarios where robustness matters.
Under the hood, tools like xmlstarlet
wrap powerful libraries like libxml2, handing you XPath and XQuery for fine-grained XML tinkering. These aren’t just XML tools but gateways to DOM manipulation, as you’d apply similar concepts in any language that touches XML.