kvnmck18 Posted March 24, 2008 Share Posted March 24, 2008 There are so many ways to transform XML... this question goes to boen -What's your suggestion for transforming this xml: <XML> <nodes att="Class1"> <node1>Info here</node1> <node2>Info here</node2> <node3>Info here</node3> </nodes> <nodes att="Class2"> <node1>Info here</node1> <node2>Info here</node2> <node3>Info here</node3> </nodes></XML> into this: <XML> <node class="Class1" node1="Info here" node2="Info here" node3="Info here"/> <node class="Class2" node1="Info here" node2="Info here" node3="Info here"/> </XML> and saving it out w+ (php 5)...You always have some easy trick. Link to comment Share on other sites More sharing options...
boen_robot Posted March 24, 2008 Share Posted March 24, 2008 There's ICQ and PMs you know. At least when I'm around that is. You're among the few people from which I don't mind being personally bothered with XSLT issues .Anyway... transformToURI() is just perfect for the job. It's the most efficient, and easiest way to execute a transformation and save its result to a file. The PHP below also includes the XSLT I'd use, and saves the result as "result.xml" in the same folder: <?php$xslt = new XSLTProcessor;$xslt->importStylesheet(@DOMDocument::loadXML('<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output indent="no"/><xsl:template match="/*"> <xsl:copy> <xsl:apply-templates/> </xsl:copy></xsl:template><xsl:template match="/*/*"> <node class="{@att}"> <xsl:for-each select="*"> <xsl:attribute name="{local-name(.)}" namespace="{namespace-uri(.)}"><xsl:apply-templates/></xsl:attribute> </xsl:for-each> </node></xsl:template></xsl:stylesheet>'));$xslt->transformToURI(@DOMDocument::loadXML('<XML> <nodes att="Class1"> <node1>Info here</node1> <node2>Info here</node2> <node3>Info here</node3> </nodes> <nodes att="Class2"> <node1>Info here</node1> <node2>Info here</node2> <node3>Info here</node3> </nodes></XML>'), 'result.xml');?> (if you store the XML and XSLT in files, you'll see those are actually only three lines of PHP code)As a side note, if you're working with really large XMLs, you might consider using the XMLReader class in combination with a simpler XSLT, or an XMLReader/XMLWriter duo, but that's hard to maintain (change), so again - only if you're working with very large (think 50MB+) XMLs. Otherwise, the performance benefit is just not worth the maintainance trouble. Link to comment Share on other sites More sharing options...
kvnmck18 Posted March 24, 2008 Author Share Posted March 24, 2008 That's pretty much what I was using but not using the "transformToURI"It was just a XSL parse with the xml and save out.That's a nice function though cuts some lines out. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.