views:

127

answers:

2

I have a lot of files with data to convert to percentages using basic math functions:

<param id="1" name="otb" value="0.160"/>
<param id="2" name="blm" value="-0.210"/>
<param id="3" name="mep" value="-0.010"/>
<param id="4" name="plc" value="-0.100"/>

Each id get's it's own equation:

  1. (n-(-.3))/2.3*100
  2. (n-(-.8))/3.3*100
  3. (n-(-.5))/1.5*100
  4. (n-(.1))/1.1*100

So I get:

otb=8 blm=20 mep=24 plc=0

What would be a good way to run all these files through... regex and php? Any quick and dirty code out there? :D

+1  A: 

As the file seems to be in XML format, I suggest you try the PHP simplexml library. The documentation can be found here.

You can then access the XML tree simply by accessing the magic properties of the XML object:

$xml = simplexml_load_file('your/path/to/your/file');

foreach ($xml->param as $param)
{
    $id = $param['id'];
    $name = $param['name'];
    $value = $param['value'];

    // do your calculations...
}
Franz
Or a simple transformation with an XML stylesheet. :-)
Workshop Alex
@Bravo Charly: Did this work?
Franz
@Franz: Looks good so far... still reading the documentation. I'm by no means a coder, ha.
Bravo Charlie
A: 

Stylesheet magic:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"&gt;
  <xsl:output indent="yes" standalone="yes" omit-xml-declaration="yes" method="xml"/>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:variable name="Values" select="@*[(name(..)='param') and ((name(.)='value'))]"/>
      <xsl:variable name="NonValues" select="@*[. != $Values]"/>
      <xsl:apply-templates select="$NonValues" mode="NonValues"/>
      <xsl:apply-templates select="$Values" mode="Values"/>
      <xsl:choose>
        <xsl:when test="*">
          <xsl:apply-templates select="*"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="@*" mode="Values">
    <xsl:attribute name="value"><xsl:variable name="n" select="."/><xsl:choose><xsl:when test="../@id=1"><xsl:value-of select="(($n - (-0.3)) div 2.3) * 100"/></xsl:when><xsl:when test="../@id=2"><xsl:value-of select="(($n - (-0.8)) div 3.3) * 100"/></xsl:when><xsl:when test="../@id=3"><xsl:value-of select="(($n - (-0.5)) div 1.5) * 100"/></xsl:when><xsl:when test="../@id=4"><xsl:value-of select="(($n - (0.1)) div 1.1) * 100"/></xsl:when><xsl:otherwise><xsl:value-of select="."/></xsl:otherwise></xsl:choose></xsl:attribute>
  </xsl:template>
  <xsl:template match="@*" mode="NonValues">
    <xsl:copy>
      <xsl:value-of select="(.)*2"/>pp
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

If you can transform the original XML with this stylesheet, you will get a new XML with calculated results. It is a bit complex but basically the code is processing all elements and child elements. For each element, it splits the attributes up in values that need to be converted and other values. It copies every element, every child element and every attribute, except the value attributes. The value attributes are processed and given another value. (But you can also just add the original value, if you want to keep it.)

Workshop Alex