Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding Attributes to all XML Nodes

Familiar Sight
 
Join Date: Apr 2006
Posts: 133
#1: Jan 21 '08
Howdy,

I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set them as an incremental 1-x values down the page.

This is the current code i am using to edit specific nodes. Im trying to use DOM, but it doesn't have to be. I just need it to work.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $content = $_POST['content'];
  4. $id = $_POST['id'];
  5. $node = $_POST['node'];
  6. $explode = explode("_",$node);
  7.  
  8. $dom=new DOMDocument();
  9. $dom->load('sample.xml');
  10. $dom->formatOutput = true;
  11. //echo $dom->saveXML(); // show before file
  12. $allnodes = $dom->getElementsByTagName($explode[0]);
  13. foreach ($allnodes as $nodes) {
  14.     if ($nodes->nodeName==$explode[0] and $nodes->getAttribute('id')==$id) { //
  15.         $nodes->setAttribute($explode[1],$content);
  16.     }
  17. }
  18. $dom->save('sample.xml');
  19. ?>
  20.  
This is the XML file im working with. **NOTE** the missing *id* attributes of the 2nd *child01* node.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <parent option01="other info" option02="other info" id="1">
  4.     <child01 option01="child01 option01" option02="child01 option02" id="2">
  5.         <child02 option01="child02 option01" option02="child02 option02" id="3">
  6.             <text option01="text option01" option02="text option02" id="4">
  7.                 <![CDATA[<b>Ma quande lingues coalesce</b>]]>
  8.             </text>
  9.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="5">image 1</image>
  10.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="6">image 2</image>
  11.         </child02>
  12.         <child02 option01="child02 option01" option02="child02 option02" id="7">
  13.             <text option01="text option01" option02="text option02" id="8">
  14.                 <![CDATA[Lorem ipsum dolor sit amet.]]>
  15.             </text>
  16.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="9">image 1</image>
  17.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02" id="10">image 2</image>
  18.         </child02>
  19.     </child01>
  20.     <child01 option01="child01 option01" option02="child01 option02">
  21.         <child02 option01="child02 option01" option02="child02 option02">
  22.             <text option01="text option01" option02="text option02">
  23.                 <![CDATA[Epsum factorial non deposit quid pro quo hic escorol.]]>
  24.             </text>
  25.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
  26.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
  27.         </child02>
  28.         <child02 option01="child02 option01" option02="child02 option02">
  29.             <text option01="text option01" option02="text option02">
  30.                 <![CDATA[Li Europan lingues es membres del sam familie.]]>
  31.             </text>
  32.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 1</image>
  33.             <image option01="Li Europan lingues 01" option02="Li Europan lingues 02">image 2</image>
  34.         </child02>
  35.     </child01>
  36. </parent>
  37.  



Moderator
 
Join Date: Mar 2006
Posts: 1,103
#2: Jan 21 '08

re: Adding Attributes to all XML Nodes


Have an addition to what I said in the other forum:

It would be easier to do this in xslt:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="*">
  2.   <xsl:copy>
  3.     <xsl:copy-of select="@*[not (name() = 'id')]"/>
  4.     <xsl:attribute name="id" select="count(preceding::*) +1"/>
  5.   </xsl:copy>
  6. </xsl:template>
  7.  
The hard part might be setting this up in php if you're not familiar with xslt. For that look here:
http://www.tonymarston.net/php-mysql/xsl.html#a6
Reply