xmlxml-namespacesxmlstarlet

How do I access an XML node that uses quote marks with xmlstarlet?


I'm trying to learn how to use xmlstarlet 1.6.1 https://xmlstar.sourceforge.net and I can't find how to escape the quotes or otherwise get xmlstarlet to recognize <content type="html"> as a node when editing inplace. In this case, I'm deleting the node. (But I also need to eventually read and edit it and then save it.)

File xmltest.xml:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:blogger="http://schemas.google.com/blogger/2018">
  <title>Test</title>
  <entry>
    <author>
      <name>Author</name>
    </author>
<title/>
    <content type="html">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod</content>
  </entry>

<entry>
    ...
  </entry>

Trying

xmlstarlet edit --inplace --delete '//content type="html">' xmltest.xml

throws the error

Invalid expression: //content type="html">


Solution

  • To target <content> XML element correctly, you need to bind the default namespace to a prefix using -N, then use that prefix in your XPath.

    xmlstarlet edit -N ns="http://www.w3.org/2005/Atom" -d "//ns:content[@type='html']" input.xml > output.xml