ameliabob Posted July 8, 2008 Share Posted July 8, 2008 The examples in the schools show the transforms being applied to XSL and XML data files residing on disk. Is there a way to apply an XSL file to an XML that has been loaded into memory? Link to comment Share on other sites More sharing options...
boen_robot Posted July 8, 2008 Share Posted July 8, 2008 Depends. What language do you use?The main idea is to load the XSLT's DOM tree into memory, and use that tree (residing in memory) for the transformation. You can create a new DOM tree which would by definition reside in memory, but this means it will be created on demand.For example, you can have the XML file: <root>sample</root> and to do a transformation in PHP, you can do: <?php$xml = new DOMDocument;$xml->load('xmlFile.xml');$proc = new XSLTProcessor;$proc->importStylesheet(DOMDocument::load('stylesheet.xsl'));echo $proc->transformToXML($xml);?> or as said, construct the DOM tree from PHP itself, like: <?php$xml = new DOMDocument;$xml->appendChild($xml->createElement('root', 'sample'));$proc = new XSLTProcessor;$proc->importStylesheet(DOMDocument::load('stylesheet.xsl'));echo $proc->transformToXML($xml);?> But the tecniques are many and vary from one language to another. DOM is usually the only common dominator - if you understand how it works in one language, you'd be able to easily migrate it to another. Link to comment Share on other sites More sharing options...
ameliabob Posted September 19, 2008 Author Share Posted September 19, 2008 Depends. What language do you use?The main idea is to load the XSLT's DOM tree into memory, and use that tree (residing in memory) for the transformation. You can create a new DOM tree which would by definition reside in memory, but this means it will be created on demand.For example, you can have the XML file:<?phpfunction LoadFile($fname){$xml=simplexml_load_file($fname);return($xml);}function Xform($fname, $tfile ){return($fname.transformNode( $tfile); // generates the error below}$myXML= Loadfile( "data.xml" );Xform($myXML,"showme.xsl");?> I have obviously left out all of the error checking etc. When I run this I get the error:"Call to undefined function transformNode() in ...and it point to the line with the comment Link to comment Share on other sites More sharing options...
boen_robot Posted September 20, 2008 Share Posted September 20, 2008 Again. What language is this in?PHP I assume? If so, your syntax is totally weird (in particular, the line which triggers the error - PHP doesn't use "." to access class methods but instead uses "->"), and yes, PHP doesn't have a "transformNode()" function, as the error message clearly says.And yes, to do an XSLT transformation in PHP, you must load the XML and XSLT as DOMDocument objects. If you want to work with SimpleXML, you can do so, but once it's time for transformation, use dom_import_simplexml() to convert the SimpleXML object into a DOMDocument object. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.