0

I am trying to add another "revision" node to my XML file using xmlstarlet but have not been successful. Here's my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://docbook.org/ns/docbook" xmlns:mw="http://abcxyz.com/namespace/secbook" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0-variant secbook5.0" status="some_phase">
  <title><xi:include href="urn:abcxyz:pn:tm_test.xml"><xi:fallback><!-- fallback for tm_test --></xi:fallback></xi:include>Interface Reference</title>
  <info>
    <productname>
      <xi:include href="urn:abcxyz:pn:tm_test.xml">
        <xi:fallback>
          <!-- fallback for tm_test -->
        </xi:fallback>
      </xi:include>
    </productname>
    <releaseinfo>
      <xi:include href="urn:abcxyz:pn:this_release_name.xml">
        <xi:fallback>
          <!-- fallback for this_release_name -->
        </xi:fallback>
      </xi:include>
    </releaseinfo>
    <revhistory>
      <revision>
        <revnumber>Online</revnumber>
        <date>July 2021</date>
        <revremark>Version 2.3</revremark>
      </revision>
      <revision>
        <revnumber>Online</revnumber>
        <date>Jan 2022</date>
        <revremark>Version 2.4</revremark>
      </revision>
    </revhistory>
    <biblioid class="pubsnumber">interface_ref</biblioid>
    <subjectset>
      <subject>
        <subjectterm>SOMETERM</subjectterm>
      </subject>
    </subjectset>
  </info>
  </book>

Here's what I have tried so far:

xmlstarlet edit -s /book/info/revhistory --type elem --name revisionTMP --value "" \
            -s /book/info/revhistory/revisionTMP --type elem --name revnumber  --value "Online" \
            -s /book/info/revhistory/revisionTMP --type elem --name date  --value "Jul 2022" \
            -s /book/info/revhistory/revisionTMP --type elem --name revremark  --value "Version 2.5" \
            -r /book/info/revhistory/revisionTMP -v revision \
           ./test_book.xml

While this code works on a simpler XML file like this one where I have been able to successfully add a new "student", it fails on the larger "test_book.xml" file. What am I missing? I am open to other elegant solutions not involving xmlstartlet.

<?xml version="1.0"?>
<Students>
  <student>
    <name>john</name>
    <id>123</id>
  </student>
  <student>
    <name>mike</name>
    <id>234</id>
  </student>
</Students>

I put the xmlstarlet command in s shell script. The script executes but nothing changes.

4
  • 1
    What am I missing? Your file uses namespaces. Commented Nov 1, 2021 at 18:23
  • So how do I get this to work with namespaces? Commented Nov 1, 2021 at 19:12
  • @Cyrus based on your suggestion, I did this: xmlstarlet edit -s "//_:revhistory" --type elem --name revisionTMP --value ""\ -s "//_:revhistory/revisionTMP" --type elem --name revnumber --value "Online" \ etc. This works. Is my solution robust? Commented Nov 1, 2021 at 19:49
  • XSLT transformation would be the best way to transform one XML into another. Commented Nov 1, 2021 at 20:32

1 Answer 1

0

Did you include the option -L to actually edit the file?

From xmlstarlet ed --help:

-L (or --inplace) - edit file inplace

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.