1

Need help, I have a XML request (request.xml) which I use to generate response using curl, below is the tag:

<StartDt>20150615</StartDt>

Can I make this date dynamic, so that, whenever I send my request it always take the future date (3 weeks from now)?

Below command line gives me a desired date:

date -d 'now + 3 weeks' +%Y%m%d 
Jahid
  • 21,542
  • 10
  • 90
  • 108
Somu
  • 11
  • 1

1 Answers1

1

To do this in a way that's fully robust, install XMLStarlet and pipe your XML document through:

xmlstarlet ed -u //StartDt -v "$(date -d 'now + 3 weeks' +%Y%m%d)" <request.xml

To break that down:

  xmlstarlet ed -u //StartDt -v "$(date -d 'now + 3 weeks' +%Y%m%d)"
# ^^^^^^^^^^ ^^ ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# |          |  |            \- value to substitute when expression is matched
# |          |  \- XPath expression to match content to edit
# |          \- edit subcommand
# \- generic tool for editing, selecting from, &c. XML.

//StartDt is XPath to match all elements named StartDt, wherever they are in the document. XPath is a quite expressive language (though libxml, the library serving the backend here, only supports the 1.0 version) -- so you can quite straightforwardly filter further.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Nice tool! My best approach would have been `xsltproc`, but this would be much more verbose compared to `xmlstarlet`. – hek2mgl Jun 15 '15 at 23:33