www.fireflysoftware.com

Replace XML Attribute Values


Suppose you've got a "stock portfolio" in the form of an XML file and, for whatever reason, you wanted to zero all stock price data for XYZ Corporation.  Doesn't exactly sound like something you can do just by piping text does it?  Here's how it's done using TEXTools...  

First, the XML "portfolio":

<stock_quotes>

   <stock_quote>
      <symbol>XYZ</symbol>
      <date>12/16/1999</date>
      <time>4:40PM</time>
      <price type="ask" value="109.1875"/>
      <price type="open" value="108"/>
      <price type="high" value="109.6875"/>
      <price type="low" value="105.75"/>
      <change>+2.1875</change>
      <volume>7050200</volume>
   </stock_quote>

   <stock_quote>
      <symbol>ABC</symbol>
      <date>12/16/1999</date>
      <time>4:01PM</time>
      <price type="ask" value="113.6875"/>
      <price type="open" value="109.25"/>
      <price type="high" value="115"/>
      <price type="low" value="108.9375"/>
      <change>+5.25</change>
      <volume>64282200</volume>
   </stock_quote>

   ...

</stock_quotes>

Now, the pipe:

; Isolate the company's data:

IsolateLines '<symbol>XYZ</symbol>' '</stock_quote>'

   ; Further isolate its price data:

   IsolateLinesByPos '<price' 2

      ; Split the price values onto separate lines so
      ; that they can be easily manipulated:

      ReplStr 'value="' '#0d#0avalue="' 

      ; Add a line number to record current line order:

      InsLineNo /w3 /z

      ; Group the lines to be edited together in 
      ; preparation for isolation:

      GroupLines 'value=' 4 9

      ; Isolate the price value lines:

      IsolateLinesByPos 'value=' 4

         ; Now, edit them:

         ShiftChars 11 '"/>'
         InsStr 11 '0.00'
      EndIsolate 

      ; Sort the lines back to their original order
      ; and remove the added line numbers:

      SortLines
      DelChars 1 3 

      ; Re-join the price value lines:

      JoinLines 2
   EndIsolate
EndIsolate

To run, copy the above data into TEXTools' input text memo and the pipe instructions into TEXTools' pipe memo and click run.  Afterwards, XYZ Corporation's stock price data will all be zeroed and ABC company's data will be left intact.  If you haven't already guessed, isolation blocks are the key to doing a selective update like this.  You can read about isolation blocks in TEXTools online help.  This example is also included in the TEXTools install package under the "Sample Pipes" folder.  See also the sample pipe, "Replace XML Attribute Value", which updates only a single attribute value.

 

[Home] [Contact Us] [Downloads] [Purchase/Register]

Copyright © 2005 Firefly Software