Search code examples
bashshellxpathxmlstarlet

XMLStarlet: Editing a node with specific attribute value


I want to edit the value of the node "title" in the following XML file, using XMLStarlet. Only the title node which is a child node of locale with attribute name="de-DE" should be edited.

<?xml version="1.0" encoding="UTF-8"?>
    <package xmlns="http://apple.com/itunes/importer" version="software5.4">
      <software>
        <software_metadata app_platform="ios">
          <versions>
            <version string="1.1.1">
              <locales>
                <locale name="de-DE">
                  <title>title_DE</title>
                </locale>
                <locale name="en-US">
                  <title>title_EN</title>
                </locale>
              </locales>
            </version>
          </versions>
        </software_metadata>
      </software>
    </package>

I tried the following command, but it does nothing:

xml ed -N x="http://apple.com/itunes/importer" -u '/x:package/x:software/x:software_metadata/x:versions/x:version/x:locales/x:locale[@name='de-DE']/x:title' -v 'new titel' metadata.xml 

Using the same XPath query for selecting works fine:

xml sel -N x="http://apple.com/itunes/importer" -t -v "/x:package/x:software/x:software_metadata/x:versions/x:version/x:locales/x:locale[@name='de-DE']/x:title" metadata.xml

Does anybody know why selecting is working but editing is not?


Solution

  • In your edit statement you should also use double quotes on the outer definition. The way it is now @name='de-DE' is the problem because the single quote ends the command.

    xml ed -N x="http://apple.com/itunes/importer" -u '/x:package/x:software/x:software_metadata/x:versions/x:version/x:locales/x:locale[@name="de-DE"]/x:title' -v 'new titel' metadata.xml
    

    My general preference is to use single quotes first and on the inside use double quotes but since both works it is up to choice as long as consistency is kept.