I’m learning that people really want to manipulate XML files with their IBM UrbanCode Deploy servers! We get so many questions about the Update XML With XPath automation step (both through the forum and through direct contact) that I wrote an article about some common things that come up when people want to edit XML files as part of their deployment automation: Using the Update XML with Xpath step.
… and we still get a ton of XPath questions!
The most common use case seems to be a server configuration that needs an edit when a new application or version of an application gets deployed. However, this step can edit just about any XML file, though there are some gotchas and idiosyncrasies that are listed in the article. (I might go into some detail about those in a later post.)
The basics are pretty simple: first, you give the step the name and location of an XML file or files. Then you give it some rules for changes to make in the XML file. To tell the step to change some text, for example, you give it an XPath string that points to the text, then a little text “arrow” (->), and then the new text.
For example, here’s some sample XML from the article:
<?xml version="1.0" encoding="UTF-8"?> <testFile> <myData> <data name="filePath" value="/opt/sampleDirectory"/> <data name="textData">Here is some text.</data> <data att1="one" att2="two" name="attributes" value="something"/> </myData> </testFile>
To change an attribute value, you use an XPath string that points to that attribute, and then you put in the new text. For example, to change the “/opt/sampleDirectory” to “/usr/otherDirectory”, put this in the Replace with text field:
To change the text of a node, refer to it as “text()”, for example:
//testFile/myData/data[@name='textData']/text()->Some new text content.
Run both of those operations and the XML looks like this:
<?xml version="1.0" encoding="UTF-8"?> <testFile> <myData> <data name="filePath" value="/usr/otherDirectory"/> <data name="textData">Some new text content.</data> <data att1="one" att2="two" name="attributes" value="something"/> </myData> </testFile>
There are also steps to add and remove XML nodes.
Got an unusual file that you need to make changes to as part of your deployment automation? Keep the XPath questions coming in the forum and we’ll answer as many as we can.