Sebastian Dwornik
Sebastian Dwornik

Reputation: 2576

How to update an XML string in a file?

Platform: MacOS

I've been struggling with getting xmlstarlet working and am out of ideas as to why it isn't performing my operation on the sample snippet below:

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<application xmlns="http://ns.adobe.com/air/application/32.0">    
    <filename>Administrator</filename>

    <name>Administrator</name>

    <versionLabel>4.5.0</versionLabel> 
    <versionNumber>1</versionNumber> 

</application>

My ultimate goal is to have a script that is run by intelliJ before the build to auto-increment the <versionNumber> tag.

It should read in the value, increment it, then write it back.

Something I've done plenty of other times with plistbuddy on other Xcode projects, but using a plain .xml file and the recommended xmlstarlet has proven to be frustrating.

Even just trying to write out the value in bash doesn't work for me:

xmlstarlet sel -t -v "//versionNumber" ./Administrator-app.xml

sed & awk are really messy for this stuff, but have shown to provide some results. What am I missing here?

Upvotes: 1

Views: 80

Answers (1)

Sebastian Dwornik
Sebastian Dwornik

Reputation: 2576

It's not working because the XML is in the default namespace

fyi: http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html#idm47077139530992

" ... 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 another way to solve handle the previous example would be:"

xmlstarlet sel -t -m "//_:versionNumber" -v . -n Administrator-app.xml

Result:

1

Upvotes: 2

Related Questions