?
Solved

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

Posted on 2003-03-21
16
Medium Priority
?
285 Views
Last Modified: 2013-11-19
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.
0
Comment
Question by:tearz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 4

Expert Comment

by:anderson22
ID: 8185184
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
 

Author Comment

by:tearz
ID: 8192606
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
 

Author Comment

by:tearz
ID: 8192713
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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 4

Expert Comment

by:anderson22
ID: 8197970
Why are you using an @ character?

-rca
0
 

Author Comment

by:tearz
ID: 8199966
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
 
LVL 4

Expert Comment

by:anderson22
ID: 8203188
What html is the xsl file spitting out right now?

-rca
0
 

Author Comment

by:tearz
ID: 8207871
it is able to link but when i click on the hyperlink,it said page not found
0
 
LVL 4

Expert Comment

by:anderson22
ID: 8207964
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
 

Author Comment

by:tearz
ID: 8208009
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
 
LVL 4

Expert Comment

by:anderson22
ID: 8208173
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
 

Author Comment

by:tearz
ID: 8208197
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
 

Author Comment

by:tearz
ID: 8208354
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
 
LVL 4

Accepted Solution

by:
anderson22 earned 100 total points
ID: 8209950
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
 

Author Comment

by:tearz
ID: 8214960
tanx alot for your help! :)
0
 

Author Comment

by:tearz
ID: 8214962
Ur very helpful and constantly giving me pointers.
0
 
LVL 4

Expert Comment

by:anderson22
ID: 8215201
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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question