Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Double the value of all numbers that match a line search in an XML file?

    Help wanted · · · – – – · · ·
    3
    3
    269
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • computeteen5
      computeteen5 last edited by

      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?

      Ekopalypse dinkumoil 2 Replies Last reply Reply Quote 0
      • Ekopalypse
        Ekopalypse @computeteen5 last edited by Ekopalypse

        @computeteen5

        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.

        1 Reply Last reply Reply Quote 1
        • dinkumoil
          dinkumoil @computeteen5 last edited by dinkumoil

          @computeteen5

          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.

          1 Reply Last reply Reply Quote 3
          • First post
            Last post
          Copyright © 2014 NodeBB Forums | Contributors