title: How to compare elements from an XML and add new node with 3 or more possible results using PHP?
body: <p>I have an XML list of products and I need to assign weight on a new node to every product based on the category of each product. For example:</p>
  
  <p>I have the following XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;products&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 1&lt;/name&gt;
      &lt;category&gt;CATEGORY 1&lt;/category&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 2&lt;/name&gt;
      &lt;category&gt;CATEGORY 2&lt;/category&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 3&lt;/name&gt;
      &lt;category&gt;CATEGORY 3&lt;/category&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 4&lt;/name&gt;
      &lt;category&gt;CATEGORY 4&lt;/category&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 5&lt;/name&gt;
      &lt;category&gt;CATEGORY 5&lt;/category&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 6&lt;/name&gt;
      &lt;category&gt;CATEGORY 6&lt;/category&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 7&lt;/name&gt;
      &lt;category&gt;CATEGORY 7&lt;/category&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 8&lt;/name&gt;
      &lt;category&gt;CATEGORY 8&lt;/category&gt;
    &lt;/item&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>I have tried using this code using my logic, but is not working:</p>
  
  <pre><code>&lt;?php
  
  //CODE...
  
  $weight = $xml-&gt;xpath("//item[category]");
  foreach ( $weight as $value )    {
      $value-&gt;weight = ($value-&gt;category=='CATEGORY 1' or $value-&gt;category=='CATEGORY 2' or $value-&gt;category=='CATEGORY 3')? 1:2 and ($value-&gt;category=='CATEGORY 4' or $value-&gt;category=='CATEGORY 5')? 7:2 and ($value-&gt;category=='CATEGORY 6' or $value-&gt;category=='CATEGORY 7')? 10:2;
  }
  
  //MORE CODE...
  
  ?&gt;
  </code></pre>
  
  <p>As you can see I have been trying to assign a weight of "1 Kg" if the products are on the categories 1, 2 and 3, "7 Kg" if they are on categories 4 and 5 and finally "10 Kg" if they are on categories 6 and 7 but if they don't match any of that categories set the weight to "2 Kg" to get a result similar to this one:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;products&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 1&lt;/name&gt;
      &lt;category&gt;CATEGORY 1&lt;/category&gt;
      &lt;weight&gt;1&lt;/weight&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 2&lt;/name&gt;
      &lt;category&gt;CATEGORY 2&lt;/category&gt;
      &lt;weight&gt;1&lt;/weight&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 3&lt;/name&gt;
      &lt;category&gt;CATEGORY 3&lt;/category&gt;
      &lt;weight&gt;1&lt;/weight&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 4&lt;/name&gt;
      &lt;category&gt;CATEGORY 4&lt;/category&gt;
      &lt;weight&gt;7&lt;/weight&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 5&lt;/name&gt;
      &lt;category&gt;CATEGORY 5&lt;/category&gt;
      &lt;weight&gt;7&lt;/weight&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 6&lt;/name&gt;
      &lt;category&gt;CATEGORY 6&lt;/category&gt;
      &lt;weight&gt;10&lt;/weight&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 7&lt;/name&gt;
      &lt;category&gt;CATEGORY 7&lt;/category&gt;
      &lt;weight&gt;10&lt;/weight&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;name&gt;PRODUCT 8&lt;/name&gt;
      &lt;category&gt;CATEGORY 8&lt;/category&gt;
      &lt;weight&gt;2&lt;/weight&gt;
    &lt;/item&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>I have the feeling that this is pretty simple but my logic has not yielded results. Any help will be appreciated.</p>