kvnmck18 Posted August 29, 2007 Share Posted August 29, 2007 Some of you familiar with previous XML updaters might remember this: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="input"/> <xsl:template match="/*"> <xsl:element name="{name()}"> <xsl:copy-of select="*"/> <xsl:value-of select="$input" disable-output-escaping="yes" /> </xsl:element> </xsl:template></xsl:stylesheet> I want to take the above code a use it so the Input value created by the PHP to be "inserted" into the "correct spot" of the xml.For example: <theXML><part><node name="bksdfjks" data="dfsdfsdf"/><node name="bksdfsadsadjks" data="ffgfdfsfgdfsgf"/><node name="bksdsadsadfjks" data="541hgbfsgdf"/><node name="bfjks" data="d343245fsdf"/></part><andhere><node name="sadsadks" data="dsadsadsadf"/>HOW TO INSERT THE "INPUT" HERE?</andhere></theXML> How do I make it so $input goes into "theXML/andhere"? Link to comment Share on other sites More sharing options...
boen_robot Posted August 29, 2007 Share Posted August 29, 2007 There isn't an easy and efficient way to do this with XSLT. I mean, the way that this is made is so that the position is "hard coded". The only easy way I can think of is with DOM, but that's not available in PHP4, at least not with the same ease.In PHP5, if you know you have <node/> elements in the <andhere/> element and you want to add it into the <node/> element with a certain name, you could easily do it like so: <?php$xmlFile = 'xml.xml';//Some code that generates $input as a raw XML data (i.e. $input = '<node/>')$dom = new DOMDocument;$dom->load($xmlFile);$xpath = new DOMXPath($dom);$xpath->query('/theXML/andhere/node[@name = "sadsadks"]')->item(0)->createDocumentFragment()->appendXML($input);//Done. Now just to replace the file$dom->save($xmlFile);?> And just adjust the XPath expression (in query()) to match whatever node you want to append raw XML data to.Of course, if you have PHP5 to begin with, you'll probably be better of using create*() functions, as they create nodes in a "secure" fashion (no need for htmlentities()). A few appenChild() calls at the appropriate level are all it takes to actually put them. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.