Augeas to sort XML file

210 Views Asked by At

I need to edit some XML files, I'm OK with removing and setting, but I don't see if and how is it possible to sort XML file using only Augeas?

Has anyone done it, I am trying to avoid other resources than Augeas for now?

I am using puppet, ruby, shell scripts. I can use augeas in puppet, not only augtool.

This is my exact task: sort elements on element name, attributes name, table and column

I have a large XML file which actually contains a lot of tables, this is example of one table:

<table name="validation_rule" numRows="6" remarks="" schema="public" type="TABLE">
  <column autoUpdated="false" digits="0" id="0" name="id" nullable="false" remarks="" size="10" type="int4">
    <child column="validation_rule_id" foreignKey="meta_field_name_validation_rule_id_fkey" implied="false" onDeleteCascade="false" table="meta_field_name"/>
    <child column="validation_rule_id" foreignKey="preference_type_validation_rule_id_fkey" implied="false" onDeleteCascade="false" table="preference_type"/>
    <child column="validation_rule_id" foreignKey="validation_rule_attributes_validation_rule_id_fkey" implied="false" onDeleteCascade="false" table="validation_rule_attributes"/>
  </column>
  <column autoUpdated="false" digits="0" id="1" name="rule_type" nullable="false" remarks="" size="25" type="varchar"/>
  <column autoUpdated="false" digits="0" id="2" name="enabled" nullable="true" remarks="" size="1" type="bool"/>
  <column autoUpdated="false" digits="0" id="3" name="optlock" nullable="false" remarks="" size="10" type="int4"/>
  <primaryKey column="id" sequenceNumberInPK="1"/>
  <index name="validation_rule_pkey" unique="true">
    <column ascending="true" name="id"/>
  </index>
</table>

This is how I delete from same sample file:

augtool> set /augeas/load/xml/lens "Xml.lns" 
augtool> set /augeas/load/xml/incl /home/ESSENT/i.camilovic/test/test.xml
augtool> load
augtool> rm /files/home/ESSENT/i.camilovic/test/test.xml/TopLevel/FooBar
augtool> save

Here is a code sample in puppet that we use for something else:

    augeas { "${name} ReverseBuild Threshold fails":
      lens    => 'Xml.lns',
      incl    => $config_file,
      context => "${context}/triggers/jenkins.triggers.ReverseBuildTrigger/threshold",
      changes => [
        "set name/#text 'FAILURE'",
        "set ordinal/#text '2'",
        "set color/#text 'RED'",
        "set completeBuild/#text 'true'",
      ],
      notify  => Exec['reload-configuration'],
      require => Augeas["${name} Upstream Projects"],
    }
2

There are 2 best solutions below

6
On

This cannot be achieved with Augeas alone (i.e. augtool), but it could be achieved using the Augeas library together with language bindings. In which context are you using Augeas?

2
On

Instead of using Augeas to achieve that task in Puppet, I would recommend using an exec, e.g.:

exec { "Sort test.xml":
  path        => $::path,
  command     => 'xmllint -c14n -o /path/to/test.xml /path/to/test.xml',
  refreshonly => true,
}