Click to See Complete Forum and Search --> : Using XSL: Create links from fields within XML file


lunalu
12-01-2008, 02:54 PM
Hello all,

I am new to XSLT and have been trying to figure out a solution to the following issue...

Here is a summary of what I would like to do:
- Create an xsl file that generates links from specific entries in the xml file.
- Those links, when clicked, will open up a new browser window and display only the data for that entry in the xml file.

I have been able to create links that point to anchors within the document but that is not what I want.

Here is a cdcatalog xml file I am using as an example:

<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>

</catalog>


What I would like to see in Xhtml is a page with a list like the one below:

* Empire Burlesque
* Hide your heart
* Greatest Hits
* Still got the blues
* Eros



..where the above are links to the specific CDs in the catalog (opened in a new browser window when the link is clicked).

For example: Clicking on "Empire Burlesque" would display a new window containing...

Empire Burlesque
Artist: Bob Dylan
Year: 1985
Price: 10.90

...and nothing else.


Any idea on how I can go about accomplishing this using xslt?

Thank you.

Regards,
Lu.

rpgfan3233
12-01-2008, 06:46 PM
That's not too difficult with the right tools...

You would need to make use of the xsl:param and xsl:with-param elements. Unfortunately, the XSLT recommendation doesn't define exactly how to pass parameters to the XSLT parser, which means that you would need to use an extra language with the ability to handle both XML and XSLT. For example, I personally use Python programming in combination with the lxml module. Obviously lxml does the work. If the links contained something like:
<a href="cdcatalog.py?AlbumTitle=Empire Burlesque" target="_blank">
then I would use Python to process the URL, retrieve the value of AlbumTitle and pass that as a parameter when I transformed it using XSLT. Of course, you don't need to use Python. PHP, for example, could also be used. That's the easy way of doing it.

The more difficult way is to create nearly identical files for each album title, something that is reminiscent of being able to use only HTML when no CGI, PHP, etc. is available, and you have similar content for various pages. Then instead of linking to "cdcatalog.py?AlbumTitle=Empire Burlesque", you would link to something like "Empire Burlesque - Bob Dylan.xml" for each item. ^_^

I hope this helps, though I realize it sounds like it is really crazy and really difficult. If you have the ability to use something like Python or PHP as a server-side language, then it becomes considerably easier since you only have three documents - the server-side document that does the work, the XML document and the XSLT document.

lunalu
12-03-2008, 05:40 PM
Hello rpgfan3233,

Thanks for your reply on this. :)

The application I am looking to apply these ideas on does not utilize a server; so I can't go the PHP or Python route as you mentioned. And it doesn't look like I can use xsl and xml alone to accomplish this task (without creating multiple files for each album title, as you said)... *sigh*.

I got a little more thinking to do on this issue.
Thank you again, rpgfan, for the feedback on this.

~Lu

ptay89
12-11-2008, 09:43 PM
In my xml file i have the url stored as:

<url>http://www.google.com</url>

and in my xsl file i put:

<a href="#{url}"><xsl:value-of select="url"/></a>

This gives a link, with the text of 'http://www.google.com'

I then found that if i put:

<td><a href="#{url}">link to url</a>

This gives a link with the text of 'link to url'

so i assume if you store an extra attribute of url for each of your cds, containing the website, you could then use...

<a href="#{url}"><xsl:value-of select="title"/></a>

or something similar to that

Hope thats helpful
Phil

ptay89
12-11-2008, 09:45 PM
scrap that, you want <a href="{url}"><xsl:value-of select="title"/></a>

with url being in the xml, on the same level as title