bdee1 Posted January 16, 2009 Share Posted January 16, 2009 i am using simpleXML to interact with an xml file for my app. I am basically usign the xml file instead of a database to store links, and information about them. those links will be displayed on the main page of my app. my xml looks is structured like: <?xml version="1.0" encoding="utf-8"?> <fileContents> <users> <user id="1"> <username>user1</username> <password>user1</password> <FirstName>Joe</FirstName> <LastName>Blow</LastName> </user> </users> <linkCollection> <linkGroup id="1" title="Photoshop"> <linkItem id="2"> <title>PSD Tuts</title> <description>PSDTUTS is a blog/Photoshop site made to house and showcase some of the best Photoshop tutorials around. We publish tutorials that not only produce great graphics and effects, but explain in a friendly, approachable manner.</description> <url>http://www.psdtuts.com</url> </linkItem> </linkGroup> </linkCollection> </fileContents> the first part (<users>) just stored login information the second part <linkCollection> contains the links and their info. i am able to easily loop through these and dump the info to the screen, but i would like to be able to display them on the screen sorted by the ID attribute of the <linkItem> nodes. how would i go about doing that? is there some kind of sort function i could use? Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/ Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 seems like there really should be a simple way to do do this. Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738620 Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 No. You can store them in an array by the id then do a key sort on that array. It will be 2 loops, but with flat files that is how it has to be done. Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738627 Share on other sites More sharing options...
effigy Posted January 16, 2009 Share Posted January 16, 2009 Have you tried XSLT? Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738629 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 No. You can store them in an array by the id then do a key sort on that array. It will be 2 loops, but with flat files that is how it has to be done. could you show me what that might look like? Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738630 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 Have you tried XSLT? have heard of it but don't know much about it - how woudl that work? Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738633 Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 Have you tried XSLT? Never even heard of it till now. Have you tried XSLT? have heard of it but don't know much about it - how woudl that work? http://www.w3schools.com/xsl/ Google is your friend. Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738637 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 ok so xslt is basically a stylesheet for XML files. according to that tutorial, if i wanted to display the contents of my xml file in a certain order i would need to create a xsl stylesheet for it, modify the xml file to point to the xsl stylesheet and then point the browser directly to the xml file. but here's the complication - i am not JUST displaying the contents of the xml file, i am also displaying login links and that sort of stuff which woudl require php. so if i am pointing the browser directly to the xml file, i cant include php right? how would that work? sorry - i am (obviously) brand new to this. Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738642 Share on other sites More sharing options...
effigy Posted January 16, 2009 Share Posted January 16, 2009 You don't have to modify the XML. See example #2 here and the example here. If you have trouble, please post a larger XML sample and your code. Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738646 Share on other sites More sharing options...
bdee1 Posted January 16, 2009 Author Share Posted January 16, 2009 ok so in the first example - the function is taking an xml file (sample.xml) and applying the xsl stylesheet (sample.xsl) to create result.xml. but does it actually save result.xml as a file back to the filesystem? or is it as an object or something? Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738650 Share on other sites More sharing options...
effigy Posted January 16, 2009 Share Posted January 16, 2009 Example #1 Using the xslt_process() to transform an XML file and a XSL file to a new XML file Example #2 Using the xslt_process() to transform an XML file and a XSL file to a variable containing the resulting XML data Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-738734 Share on other sites More sharing options...
bdee1 Posted January 19, 2009 Author Share Posted January 19, 2009 ok i see what you're saying... but i there are a couple more small pieces i don't quite understand... 1) the xsl stylesheet file has a bunch of html in it. all i want to do is sort the xml nodes into the correct order so do i need the xml? i'm not quite sure how that woudl work. 2) in example 2 it returns the transformed xml file into the $result variable. how would i then go about parsing that? would i treat $result in that case the same as if i had done the following to read in an actual file? $result = simplexml_load_file('../data/file.xml'); Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-740411 Share on other sites More sharing options...
bdee1 Posted January 19, 2009 Author Share Posted January 19, 2009 Example #1 Using the xslt_process() to transform an XML file and a XSL file to a new XML file Example #2 Using the xslt_process() to transform an XML file and a XSL file to a variable containing the resulting XML data ok so i have created an xslt file based on the example that displays the LinkItem info and sorts it by the linkItem id attribute. but it displays it as an html table... how would i modify it so it actually generates a sorted xml file instead of an html file? <?xml version="1.0" encoding="ISO-8859-1"?><!-- DWXMLSource="file.xml" --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <xsl:for-each select="fileContents/linkCollection/linkGroup"> <h2>My <xsl:value-of select="@title"/> Links</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Description</th> <th>URL</th> </tr> <xsl:for-each select="linkItem"> <xsl:sort select="@id"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="description"/></td> <td><xsl:value-of select="url"/></td> </tr> </xsl:for-each> </table> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet> Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-740439 Share on other sites More sharing options...
effigy Posted January 19, 2009 Share Posted January 19, 2009 I cannot get XSLT to function on my PHP install at work. I'm also rusty. See if this helps or other items here. Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-740450 Share on other sites More sharing options...
bdee1 Posted January 19, 2009 Author Share Posted January 19, 2009 what i am not understanding is that any examples i see are not outputting an xml file, they are outputting some kind of formatted text. so based on the php samples where they take an xml file and apply the xslt stylesheet and return an xml file... i am not sure hwo to accomplish that if the xslt does not return xml. seems like there's something i am missing here. Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-740465 Share on other sites More sharing options...
effigy Posted January 19, 2009 Share Posted January 19, 2009 Do you want your original XML to remain exactly the same, aside from the sorting factor? In XSLT you can control the format--it can be whatever you want. If you want to leave the original XML structure the same, but make a few changes, you use an identity transform. Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-740482 Share on other sites More sharing options...
bdee1 Posted January 19, 2009 Author Share Posted January 19, 2009 yes that is exactly what i am lookign to do... how can i do that? Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-740506 Share on other sites More sharing options...
effigy Posted January 19, 2009 Share Posted January 19, 2009 Look through the Google results I linked. Perhaps this one? Quote Link to comment https://forums.phpfreaks.com/topic/141111-displaying-items-from-an-xml-file-in-a-certain-order/#findComment-740524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.