Newbie question about a hyperlink

P

Paul Johnston

Hi
Just started playing with xml and xslt and have a problem.
I have a xml file which I want to contain a hyperlink to a resouce.
I convert the xml into html for displaying in a web browser using an
xsl file.
I have replaced the < and > in the link with &lt; and &gt;
Could someone give me a pointer as to getting the link to work
The value-of select simply puts the string there, it

Below is a section of the xml file (URL Modified) :)

<word>
<adj>
<persian>????</persian>
<english>habitable</english>
<transcription>abad</transcription>
<audio>&lt;a
href="http://www.mystuff/audio/habitable.mp3"&gt;Link&lt;/a&gt;</audio>
<page>709</page>
</adj>
</word>



And here a section of the xsl file

<table border="2">
<tr>Adjectives</tr>
<tr bgcolor="#9acd20">
<th align="left">Persian</th>
<th align="left">English</th>
<th align="left">Transliteration</th>
<th align="left">Audio</th>
<th align="left">Page</th>
</tr>
<xsl:for-each select="corpus/word/adj">
<xsl:sort select="persian"/>
<tr>
<td>
<xsl:value-of select="persian"/>
</td>
<td>
<xsl:value-of select="english"/>
</td>
<td>
<xsl:value-of select="transcription"/>
</td>
<td>
<xsl:value-of select="audio"/>
</td>
<td>
<xsl:value-of select="page"/>
</td>
</tr>
</xsl:for-each>
</table>


TIA Paul
 
P

p.lepin

Paul said:
Just started playing with xml and xslt and have a
problem. I have a xml file which I want to contain a
hyperlink to a resouce. I convert the xml into html for
displaying in a web browser using an xsl file.
I have replaced the < and > in the link with &lt; and
&gt;

I'd say that's a pretty bad (or inelegant, at least)
solution. Storing HTML markup verbatim in your XML files
does sound like a particularly good idea to me. What you
really need is URL and link text.
<word>
<adj>
<persian>????</persian>
<english>habitable</english>
<transcription>abad</transcription>
<audio>&lt;a
href="http://www.mystuff/audio/habitable.mp3"&gt;Link&lt;/a&gt;
</audio>
<page>709</page>
</adj>
</word>

If you have control over the format of your XML files, I'd
recommend replacing that with something like:

<audio>
<link href="http://example.org/habitable.mp3">
Link
</link>
</audio>

(You could also move the link text to an attribute, or, if
it's purely presentational, don't mention it in the XML at
all.)
<table border="2">
<tr>Adjectives</tr>
<tr bgcolor="#9acd20">
<th align="left">Persian</th>
<th align="left">English</th>
<th align="left">Transliteration</th>
<th align="left">Audio</th>
<th align="left">Page</th>
</tr>
<xsl:for-each select="corpus/word/adj">

for-each! Anathema! Anathema! Smacks of imperative
programming.
<xsl:sort select="persian"/>
<tr>
<td>
<xsl:value-of select="persian"/>
</td>
<td>
<xsl:value-of select="english"/>
</td>
<td>
<xsl:value-of select="transcription"/>
</td>
<td>
<xsl:value-of select="audio"/>

If you decide to follow my advice, this would become:

<xsl:element name="a">
<xsl:attribute
name="href"><xsl:value-of
select="audio/link/@href"/></xsl:attribute><xsl:value-of
select="audio/link"/>
</xsl:element>

....or something like that.
</td>
<td>
<xsl:value-of select="page"/>
</td>

I'd rewrite all of this using templates: define templates
<english> etc. elements said:
</tr>
</xsl:for-each>
</table>

Hope this helps.
 
P

Paul Johnston

I'd say that's a pretty bad (or inelegant, at least)
solution. Storing HTML markup verbatim in your XML files
does sound like a particularly good idea to me. What you
really need is URL and link text.


If you have control over the format of your XML files, I'd
recommend replacing that with something like:

<audio>
<link href="http://example.org/habitable.mp3">
Link
</link>
</audio>

(You could also move the link text to an attribute, or, if
it's purely presentational, don't mention it in the XML at
all.)


for-each! Anathema! Anathema! Smacks of imperative
programming.


If you decide to follow my advice, this would become:

<xsl:element name="a">
<xsl:attribute
name="href"><xsl:value-of
select="audio/link/@href"/></xsl:attribute><xsl:value-of
select="audio/link"/>
</xsl:element>

...or something like that.


I'd rewrite all of this using templates: define templates


Hope this helps.

Many Thanks
As said learning :)

Paul
 
A

Andy Dingley

Paul said:
Just started playing with xml and xslt and have a problem.
I have a xml file which I want to contain a hyperlink to a resouce.
I convert the xml into html for displaying in a web browser using an
xsl file.
I have replaced the < and > in the link with &lt; and &gt;

XSLT process XML input into output results as XML or non-XML. However
the input _must_ be XML, and the processing capabilities of XSLT and
XPath are focussed on working with XML.

When you translate < to &lt; you're moving away from XML. It's still an
XML document, but the data structure you have contained within here is
no longer accessible _as_ XML -- you've turned it into a plain string.
So this either isn't a good solution, or it's a good solution that has
also forced you away from XSLT / XPath and towards some other XML
processing tool (such as DOM and a scripting language).

You could also do it with pure XSLT and XPath. If you want to do this
sort of XPath string handling, then it's all described in the O'Reilly
"XSLT Cookbook" and may &deity; have mercy on your soul! You really
don't want to go down that route....

If you want an easy life with XSLT, keep your markup in XML. This would
suggest using namespaces and your content must obviously also be
balanced XML fragments. If you're needing to work with HTML though,
this can be troublesome - HTML is a SGML app, not XML and so XML
well-formedness can't be assumed. You might well force things into
XHTML, but that has its own problems when you come to publish it. The
solution RSS took was to encode HTML (as you've done), but this then
rules out XSLT for most useful processing of it.

You might find some useful discussion of this on http://diveintomark.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,006
Messages
2,570,265
Members
46,860
Latest member
JeremiahCo

Latest Threads

Top