Link a audio file in XML and able to download it later. Href codes to write in XSL

Hi i would like to know what is the xsl tags/codes to write to extract out the file name n hyperlink it so it is able to be download later.

this is the codes in xml file :
<media file='C:\Documents and Settings\student\Desktop\media elements\media\audio.mp3'>test media</media>

the codes i wrote in xsl file :
<xsl:template match="media">
<html>
<a href="@file">
<xsl:value-of />
</a>
</html>
</xsl:template>


Working Codes if i hard coded the file path name
<xsl:template match="media">
<html>
<a href="C:\Documents and Settings\student\Desktop\media elements\media\TheBoredphucks_ZoeTay.mp3">
<xsl:value-of />
</a>
</html>
</xsl:template>


how i am able to achieve this without hardcoding the file path name?i tried <a href="<xsl:value-of select="@file"/>"> but there's error.
tearzAsked:
Who is Participating?
 
anderson22Connect With a Mentor Commented:
The XML/XSL transformation is working.  A link is being produced that has the form of:

<A href="C:\Desktop\audio.mp3">MY AUDIO </A>

If this is not correct, I will help you but it is not within scope of the current question.  Currently, you have XSL file executing the following code:

<xsl:template match="media">
<a>
<xsl:attribute name="href">
<xsl:value-of select="@file"/>
</xsl:attribute>
<xsl:value-of />
</a>
</xsl:template>
</xsl:stylesheet>

This is successfully outputting the code from above.  This is not hardcoded, but dynamic.  I will post exactly what is being outputted to the browser by the transformation:


<TABLE borderColor=#0033ff width="75%" align=center border=1>
<TBODY>
<TR borderColor=#00ccff>
<TD bgColor=#99ccff colSpan=2>
<DIV align=center>
<H1><FONT size=+4><B><FONT
color=#003366>Memo</FONT></B></FONT></H1></DIV></TD></TR></TBODY></TABLE>
<TABLE borderColor=#0033ff width="75%" align=center border=1>
<TBODY>
<TR borderColor=#00ccff>
<TD width="12%" bgColor=#9999ff>
<DIV align=left><FONT face="Times New Roman, Times, serif"
size=4>Subject</FONT></DIV></TD>
<TD colSpan=2><FONT face="Times New Roman, Times, serif"
size=4>S</FONT></TD></TR></TBODY></TABLE>
<TABLE borderColor=#0033ff width="75%" align=center border=1>
<TBODY>
<TR borderColor=#00ccff>
<TD width="12%" bgColor=#9966cc>
<DIV align=left><FONT face="Times New Roman, Times, serif"
size=4>To</FONT></DIV></TD>
<TD colSpan=2><FONT face="Times New Roman, Times, serif"
size=4>S</FONT></TD></TR></TBODY></TABLE>
<TABLE borderColor=#0033ff width="75%" align=center border=1>
<TBODY>
<TR borderColor=#00ccff>
<TD width="12%" bgColor=#9999ff>
<DIV align=left><FONT face="Times New Roman, Times, serif"
size=4>From</FONT></DIV></TD>
<TD colSpan=2><FONT face="Times New Roman, Times, serif"
size=4>S</FONT></TD></TR></TBODY></TABLE>
<TABLE borderColor=#0033ff width="75%" align=center border=1>
<TBODY>
<TR borderColor=#00ccff>
<TD width="12%" bgColor=#9966cc>
<DIV align=left><FONT face="Times New Roman, Times, serif"
size=4>Date</FONT></DIV></TD>
<TD colSpan=2><FONT face="Times New Roman, Times, serif" size=4>Thursday, March
20, 2003</FONT></TD></TR></TBODY></TABLE>
<TABLE borderColor=#0033ff width="75%" align=center border=1>
<TBODY>
<TR borderColor=#00ccff>
<TD width="12%" bgColor=#9999ff>
<DIV align=left><FONT face="Times New Roman, Times, serif"
size=4>Body</FONT></DIV></TD>
<TD colSpan=2><FONT face="Times New Roman, Times, serif"
size=4>S</FONT></TD></TR></TBODY></TABLE>
<TABLE borderColor=#0033ff width="75%" align=center border=1>
<TBODY>
<TR borderColor=#00ccff>
<TD width="12%" bgColor=#9999ff>
<DIV align=left><FONT face="Times New Roman, Times, serif"
size=4>Image</FONT></DIV></TD>
<TD colSpan=2><IMG src="C:\Desktop\mobile office pix\memo.jpg">
</TD></TR></TBODY></TABLE><A href="C:\Desktop\audio.mp3">MY AUDIO </A>

-rca
0
 
anderson22Commented:
This one took me forever to find:

<xsl:template match="media">
<a>
<xsl:attribute name="href">
<xsl:text>href:</xsl:text>
<xsl:value-of select="file" />
</xsl:attribute>
<xsl:value-of select="file" />
</a>

-rca
0
 
tearzAuthor Commented:
The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Keyword xsl:text may not be used in namespace http://www.w3.org/TR/WD-xsl.
 
i tried to follow urs but it seems cant work.

<xsl:template match="media">
<a>
<xsl:attribute name="href">
<xsl:text>href:</xsl:text>
<xsl:value-of select="@file" />
</xsl:attribute>
<xsl:value-of />
</a>
</xsl:template>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
tearzAuthor Commented:
ive changed my codes to this

<xsl:template match="media">
<a>
<xsl:attribute name="href">
file:
<xsl:value-of select="@file" />
</xsl:attribute>
<xsl:value-of />
</a>
</xsl:template>

but when i click on d hyperlink, it says page not found.
0
 
anderson22Commented:
Why are you using an @ character?

-rca
0
 
tearzAuthor Commented:
the reason im using @ is because in my XML document i have :

<media file='C:\Documents and Settings\student\Desktop\tearz\media\audio.mp3'>test media</media>

i did the same for my image and the xsl is able to extract out the image.
XML codes :
<image file='C:\Documents and Settings\student\Desktop\mobile office pix\memo.jpg'/>

XSL codes :
<xsl:template match="image">
<img>
<xsl:attribute name="src">
<xsl:value-of select="@file"/>
</xsl:attribute>
</img>
</xsl:template>
0
 
anderson22Commented:
What html is the xsl file spitting out right now?

-rca
0
 
tearzAuthor Commented:
it is able to link but when i click on the hyperlink,it said page not found
0
 
anderson22Commented:
Could you post the exact html, just so we can get a rought view of exactly what is being generated for the html anchor link?

-rca
0
 
tearzAuthor Commented:
This is my whole xml and xsl codes.When i view in IE,my audio should be able to be download.

Kindly cut n paste in notepad and save it as memo.xml


XML CODES : - saved as memo.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="memo.xsl"?>
<nm>
<header>
<subject>S</subject>
<to>S</to>
</header>
<newmemo>
<from>S</from>
<date>Thursday, March 20, 2003</date>
<body>S</body>
<image file='C:\Desktop\mobile office pix\memo.jpg'/>
<media file='C:\Desktop\audio.mp3'>MY AUDIO<media>
</newmemo>
</nm>

=====================================
XSL codes saved as memo.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">

<HTML>
<table width="75%" border="1" align="center" bordercolor="#0033FF">
<tr bordercolor="#00CCFF">
<td colspan="2" bgcolor="#99CCFF">
<div align="center">
<h1><font size="+4"><b><font color="#003366">Memo</font></b></font></h1>
</div>
</td>
</tr>
</table>
<xsl:apply-templates select="//header"/>
<xsl:apply-templates select="//newmemo"/>
</HTML>
</xsl:template>
<xsl:template match="header">
<xsl:apply-templates select="subject" />
<xsl:apply-templates select="to" />
</xsl:template>
<xsl:template match="to">
<table width="75%" border="1" align="center" bordercolor="#0033FF">
<tr bordercolor="#00CCFF">
<td width="12%" bgcolor="#9966CC">
<div align="left"><font size="4" face="Times New Roman, Times, serif">To</font></div>
</td>
<td colspan="2"><font size="4" face="Times New Roman, Times, serif"><xsl:value-of /></font></td>
</tr>
</table>
</xsl:template>
<xsl:template match="date">
<table width="75%" border="1" align="center" bordercolor="#0033FF">
<tr bordercolor="#00CCFF">
<td width="12%" bgcolor="#9966CC">
<div align="left"><font size="4" face="Times New Roman, Times, serif">Date</font></div>
</td>
<td colspan="2"><font size="4" face="Times New Roman, Times, serif"><xsl:value-of /></font></td>
</tr>
</table>
</xsl:template>
<xsl:template match="newmemo">
<xsl:apply-templates select="from" />
<xsl:apply-templates select="date"/>
<xsl:apply-templates select="body" />
<xsl:apply-templates select="image" />
<xsl:apply-templates select="media" />
</xsl:template>
<xsl:template match="subject">
<table width="75%" border="1" align="center" bordercolor="#0033FF">
<tr bordercolor="#00CCFF">
<td width="12%" bgcolor="#9999FF">
<div align="left"><font size="4" face="Times New Roman, Times, serif">Subject</font></div>
</td>
<td colspan="2"><font size="4" face="Times New Roman, Times, serif"><xsl:value-of /></font></td>
</tr>
</table>
</xsl:template>
<xsl:template match="from">
<table width="75%" border="1" align="center" bordercolor="#0033FF">
<tr bordercolor="#00CCFF">
<td width="12%" bgcolor="#9999FF">
<div align="left"><font size="4" face="Times New Roman, Times, serif">From</font></div>
</td>
<td colspan="2"><font size="4" face="Times New Roman, Times, serif"><xsl:value-of /></font></td>
</tr>
</table>
</xsl:template>

<xsl:template match="body">
<table width="75%" border="1" align="center" bordercolor="#0033FF">
<tr bordercolor="#00CCFF">
<td width="12%" bgcolor="#9999FF">
<div align="left"><font size="4" face="Times New Roman, Times, serif">Body</font></div>
</td>
<td colspan="2"><font size="4" face="Times New Roman, Times, serif"><xsl:value-of /></font></td>
</tr>
</table>
</xsl:template>

<xsl:template match="image">
<table width="75%" border="1" align="center" bordercolor="#0033FF">
<tr bordercolor="#00CCFF">
<td width="12%" bgcolor="#9999FF">
<div align="left"><font size="4" face="Times New Roman, Times, serif">Image</font></div>
</td>
<td colspan="2">
<img>
<xsl:attribute name="src">
<xsl:value-of select="@file"/>
</xsl:attribute>
</img>
</td>
</tr>
</table>
</xsl:template>

<xsl:template match="media">
<a>
<xsl:attribute name="href">
<xsl:value-of select="@file"/>
</xsl:attribute>
<xsl:value-of />
</a>
</xsl:template>
</xsl:stylesheet>
0
 
anderson22Commented:
First, the following line is missing a "/" in the ending tag:
<media file='C:\Desktop\audio.mp3'>MY AUDIO<media>

Now, running the code will produce the following html for the audio file link:
<A href="C:\Desktop\audio.mp3">MY AUDIO </A>

I don't see anything wrong with this, other than this has to be run on the machine where the audio file is located.

If the audio.mp3 is on the client machine (the machine viewing the xml file not serving it) then this seems fine.

-rca
0
 
tearzAuthor Commented:
but i have to use xsl file to extract my data in my xml.but i can extract it out but i cant download the file.

if i hardcoded my xsl media codes to :

<xsl:template match="media">
<html>
<a href="C:\Desktop\audio.mp3">
<xsl:value-of />
</a>
</html>
</xsl:template>


the audio can be downloaded.im having problems on how shld i pass the <xsl:value-of select="@file"/> in <a href="...">
so that my audio file can be downloaded.

<xsl:value-of select="@file"/> reference to my audio pathname on my desktop which is
C:\Desktop\audio.mp3

i really appreciate your kind help.Thank You :)
0
 
tearzAuthor Commented:
but i have to use xsl file to extract my data in my xml.but i can extract it out but i cant download the file.

if i hardcoded my xsl media codes to :

<xsl:template match="media">
<html>
<a href="C:\Desktop\audio.mp3">
<xsl:value-of />
</a>
</html>
</xsl:template>


the audio can be downloaded.im having problems on how shld i pass the <xsl:value-of select="@file"/> in <a href="...">
so that my audio file can be downloaded.

<xsl:value-of select="@file"/> reference to my audio pathname on my desktop which is
C:\Desktop\audio.mp3

i really appreciate your kind help.Thank You :)
0
 
tearzAuthor Commented:
tanx alot for your help! :)
0
 
tearzAuthor Commented:
Ur very helpful and constantly giving me pointers.
0
 
anderson22Commented:
No Problem

BTW, if you ever happen to use ASP you can make this very dynamic.  A typical website will store articles using xml.  This would have some basic structure:

CharlieBrown.xml
----------------
<xml...>
<article>
    <head>
        <publisher>Charlie Brown</publisher>
        <date>3/25/2003</date>
    </head>
    <paragraph>
        Charlie Brown plays baseball...
    </paragraph>
    <paragraph>
        Snoopy is Charlie Brown's dog...
    </paragraph>
</article>

You don't necessarily want to bind this document to the same XSL style sheet everytime you display the article.  You would want to transform this dynamically at run-time.  Well, using ASP and XML parser (all provided by Microsoft) you can do this with the following code:

showarticle.asp
----------------
<%@ LANGUAGE = VBScript %>
<%
Dim sourceFile, styleFile, source, style

'Set the source and style sheet locations here.
sourceFile = Server.MapPath("simple.xml")
styleFile = Server.MapPath("simple.xsl")
 
'Load the XML.
Set source = Server.CreateObject("MSXML2.DOMDocument.4.0")
source.async = false
source.load( sourceFile )

'Load the XSLT.
Set style = Server.CreateObject("MSXML2.DOMDocument.4.0")
style.async = false
style.load( styleFile )
Response.Write( source.transformNode(style) )
%>


Now you can see something that can be extremely powerful.  You can provide links on your page for all of the articles (which you might store reference to in a database).  A sample link might be:

<a href="showarticle.asp?articleid=10">Charlie Brown</a>

Now, you want to make showarticle.asp a generic page that has no files defined, rather, it will accept input and determine the files to load.  This page will accept the articleid (which might reference the xml file name from a database, etc. or you can pass the actual xml file name instead) and allow you to dynamically transform the document.

Using this, the xml file is dynamically named using a database and you can even store the stylesheet name with it.  So all you have to do to change how the document is displayed is to create a new style sheet and change your reference to this in the database.

Hope this wasn't too confusing or overwhelming, but this hits on one of the most sound techniques utilized all over the web (not to mention what has made the XML/XSL model so popular).

-rca
0
All Courses

From novice to tech pro — start learning today.