XSLT href link

M

Martin Honnen

Per said:
Is it possible to use XSLT to automatically create href links while it
formats an XML document? That is, if it finds "http://me.us/" in a
text, it adds <a href="http://me.us/">http://me.us/</a>

XSLT can certainly process text nodes, use the XPath string functions to
parse strings and can also certainly create result elements like <a
href> elements. But string processing functions in XPath 1.0 are rather
weak so finding URLs in a text and do that well is going to need a lot
of coding.
XPath 2.0 used in XSLT 2.0 has more and more powerful string processing
functions and regular expression support so there it could be easier and
shorter to implement that.
 
J

Joe Kesselman

Per said:
Is it possible to use XSLT to automatically create href links while it
formats an XML document? That is, if it finds "http://me.us/" in a
text, it adds <a href="http://me.us/">http://me.us/</a>

Possible, yes. Easy, no.

XSLT doesn't have much in the way of built-in ability to search within
text; it uses XPath, which mostly focuses on document structure.

But XSLT has enough capability that you can build this out of simpler
tools. I don't have an example on hand, but take a look at some of the
string manipulation techniques documented in the XSLT FAQ website
(http://www.dpawson.co.uk/xsl/xslfaq.html) and they should give you some
ideas on how to approach this. Basically, you can try to use
substring-after() together with some logic to find the URIs, and
recursion in place of iteration to work your way through the source text.

The other solution, if your XSLT implementation allows it, is to call
out to an extension function written in another language. That's going
to be less portable, but may be easier, and depending on the exact
details of your processor and the other language may yield better
performance.

Caveat: Not everything that looks like a URI is intended to be a URI, so
code that tries to add the anchor elements automatically is going to
guess wrong on occasion. As browsers have demonstrated, that usually
isn't fatal... but it's better to have this marked up in the source
document rather than relying on "by guess and by golly."
 
J

Joe Kesselman

The problem, as I understood it, wasn't creating the link. It was
finding a URI buried within arbitrary text and turning *that* into a link.

That requires more work to parse through the text hunting for things
that look like URIs, to break the text up into chunks, and to wrap the
anchor reference element around the middle chunk (the URI)... and to
then repeat this on the following chunk in case there are other URIs
therein.

Can be done (within some sloppy limits), it's just a bit ugly. Which is
why I pointed to the examples of string manipulation.

As your example demonstrated, it's a lot easier when you start with
something that already has the URI broken out via proper semantic markup.
 
A

Alexandre Drolet

Why don't you use the <xsl:attribute> element.
http://www.w3schools.com/xsl/el_attribute.asp

Here is an example.

1) First, the XML document :

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="NameOfTheXSLTFile.xsl"?>
<websites>
<website>
<name>Yahoo</name>
<url>http://www.yahoo.com/</url>
</website>
</websites>




2) Second, the XSLTransformation (the XSLT file) :

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="html"/>
<xsl:template match="/">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1" />
</head>
<body>


<xsl:apply-templates/>

</body>
</html>


</xsl:template>

<xsl:template match="website">
<a>
<xsl:attribute name="href">
<xsl:value-of select="url" />
</xsl:attribute>

<xsl:value-of select="name"/>
</a>

</xsl:template>

</xsl:stylesheet>


I have tested it on IE 6 and firefox 1.5.0.7. And it works well. It
makes a link to Yahoo website.






Per Johansson a écrit :
 
A

Alexandre Drolet

Yes, you are right.
My understanding of the question was wrong.
Sorry !

Joe Kesselman a écrit :
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top