Jump to content

Recommended Posts

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?

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.

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.

 

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?

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 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');

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>

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.