Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess and update attribute value in XML using XPath and PHP
    primarykey
    data
    text
    <p>this is an extension to my previous post today which can be found here: <a href="https://stackoverflow.com/questions/11099208/xpath-attribute-value-check-with-a-variable-passed-using-php">Click Here</a></p> <p>Now I want to access another attribute based upon the id attribute and change that as well. Here is my modified xml file:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;document&gt; &lt;item id="a12sd"&gt; &lt;name&gt;James&lt;/name&gt; &lt;age years="25"/&gt; &lt;pdf&gt;0023.pdf&lt;/pdf&gt; &lt;/item&gt; &lt;item id="rdf23"&gt; &lt;name&gt;Alex&lt;/name&gt; &lt;age years="35"/&gt; &lt;pdf&gt;0178.pdf&lt;/pdf&gt; &lt;/item&gt; &lt;item id="2we34"&gt; &lt;name&gt;Tom&lt;/name&gt; &lt;age years="25"/&gt; &lt;pdf&gt;0886.pdf&lt;/pdf&gt; &lt;/item&gt; &lt;item id="123de"&gt; &lt;name&gt;Robby&lt;/name&gt; &lt;age years="28"/&gt; &lt;pdf&gt;1239.pdf&lt;/pdf&gt; &lt;/item&gt; &lt;/document&gt; </code></pre> <p>I have been able to update the tags using this code:</p> <pre><code>$id = "a12sd"; $xml = simplexml_load_file('items.xml'); $itemsList = $xml-&gt;xpath('/document/item[@id = "a12sd"]'); $itemsList[0]-&gt;name = "Arnold"; $xml-&gt;asXml("items.xml"); </code></pre> <p>Now I need to access the attribute age for the id="a12sd" and update the age to say: 26</p> <p>Any idea how I can reach that attribute for given id and change that value.</p> <p>Note: before changing the value the user knows only the "id" value, so based upon id value I want to change years attribute for that particular id.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload