I am looking to try and change a XML output so that the element structure changes and some CDATA becomes an attribute rather than an <element>
Given the XML stack.xml:
<root>
<item>
<name>name</name>
<type>Type</type>
<dateMade>Datemade</dateMade>
<desc>Desc</desc>
</item>
....(more Items)...
</root>
I would like to change the XML output to stacksaved.xml:
<root>
<item>
<name>name</name>
<Itemtype type="Type">
<Itemdate dateMade="Datemade">
<desc>Desc</desc>
</Itemdate>
<Itemtype>
</item>
....(next item)....
</root>
So far my PHP DOM looks like this:
<?php
//create and load
$doc = new DOMDocument();
$doc->load('stack.xml');
$types=$doc->getElementsByTagName("type");
foreach ($types as $type)
{
$attribute=$doc->getElementsByTagName("type");
$doc->getElementsByTagName("type").setAttribute("$attribute");
}
$doc->save('stacksaved.xml'); //save the final results into xml file
?>
I keep getting the error: Fatal error: Call to undefined function setAttribute() and the document is not saved or edited in anyway. I am really new to DOM/PHP and would greatly appreciate any advice!
How would I go about changing the child structure and the element to the desired output?
Thanks as always for the read!
EDIT: Parfait gave a great explanation and showed the great power of XSLT but I am trying to get this to run using pure php only as a learning exercise for php/DOM. Can anyone help with converting this using PHP only?
DOMXpath::evaluate()to fetch data from the source document.