Double the value of all numbers that match a line search in an XML file?
-
So, I have an XML file that contains various variables I want to modify. Specifically, there are multiple lines like this:
<BasePay>55</BasePay>
<BasePay>88</BasePay>
<BasePay>24</BasePay>I want to double the value of these numbers on all lines that start with <BasePay>, so it becomes like this.
<BasePay>110</BasePay>
<BasePay>176</BasePay>
<BasePay>48</BasePay>Is there a way to do this without manually changing each value?
-
Is there a way to do this without manually changing each value?
What you try to achieve can be done with regular expressions but it is cumbersome as regex doesn’t have mathematical functions,
therefore you need to write a replacement for nearly every number you want to double.In my opinion, the correct answer to this question is:
Learn a scripting/programming language and use it to manipulate the values.
XML has been specifically designed to make it easier to parse the data and
manipulate it and I guess most of the programming languages do have
some native support to read and write xml data. -
You could use the XML Tools plugin and some XSLT code to transform your document.
The following XSLT code creates a copy of the input XML file but doubles the value of the BasePay nodes’ text:
<?xml version="1.0"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" omit-xml-declaration="no"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="BasePay"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:value-of select="text() * 2"/> </xsl:copy> </xsl:template> </xsl:transform>Save this code as an *.xsl file and open your XML file with Notepad++. I assume that you have already installed the XML Tools plugin.
While your XML file is opened in the active tab, go to
(menu) Plugins -> XML Tools -> XSL Transformation. In the dialog popping up click on the...button, select your XSL file from above and click the Transform button.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login