2

I was looking this question xmlstarlet update an attribute and trying to replace an attribute inside a Jboss configuration file. I post here just a little part of the xml:

<?xml version='1.0' encoding='UTF-8'?>

<server xmlns="urn:jboss:domain:10.0">
    <extensions>
        <extension module="org.jboss.as.clustering.infinispan"/>
    </extensions>
    <system-properties>
        <property name="hibernate.hbm2ddl.auto" value="validate"/>
    </system-properties>
</server>

What i would like to replace is the value of hibernate.hbm2ddl.auto from validate to update

Following the previous answer i tried this command, but dont update the value:

xmlstarlet edit   --update "//property[@name='hibernate.hbm2ddl.auto']/@value"   --value "update" conf.xml

I tried to follow the full path, but the result is the same: no update.

xmlstarlet edit   --update "/server/system-properties/property[@name='hibernate.hbm2ddl.auto']/@value"   --value "update" conf.xml
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Mistre83
  • 2,677
  • 6
  • 40
  • 77

2 Answers2

3

Your file uses namespaces (xmlns="urn:jboss:domain:10.0").

xmlstarlet edit --update '//*[local-name()="property"][@name="hibernate.hbm2ddl.auto"]/@value' -v "update" conf.xml

I used //*[local-name()="property"] to bypass all namespaces in conf.xml

Cyrus
  • 84,225
  • 14
  • 89
  • 153
2

In Saxon 10.0's Gizmo utility (which aims to serve similar purposes to xmlstarlet) I decided to make unprefixed names match any namespace (or none). So the equivalent would be

java net.sf.saxon.Gizmo -s:conf.xml
/>update //property[@name="hibernate.hbm2ddl.auto"]/@value with "update"
/>save conf.xml
/>quit

After years of seeing people struggling with namespaces, I'm coming to the view that having unprefixed names match any namespace is much more user-friendly.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • That's an awesome idea! You're right, there are so many questions on SO from people stuck by namespaces. Wish this was the default behavior for XPath / XSL – William Walseth May 01 '20 at 12:23