468,505 Members | 1,827 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

XSLT href link

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>
--
Per Johansson
Systems developer
http://per.johansson.name/

Sep 20 '05 #1
5 16451

Per Johansson wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 20 '05 #2
Per Johansson wrote:
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."
Feb 3 '06 #3
Why don't you use the <xsl:attributeelement.
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:output 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 :
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>
Oct 14 '06 #4
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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Oct 14 '06 #5
Yes, you are right.
My understanding of the question was wrong.
Sorry !

Joe Kesselman a écrit :
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.
Oct 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Sergio del Amo | last post: by
6 posts views Thread by Pete | last post: by
2 posts views Thread by FrankStallone | last post: by
3 posts views Thread by Grant Harmeyer | last post: by
7 posts views Thread by Harolds | last post: by
2 posts views Thread by viral612 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.