Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT href link

Reply
Thread Tools

XSLT href link

 
 
Per Johansson
Guest
Posts: n/a
 
      09-20-2005
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/

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-20-2005

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/
 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      02-03-2006
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."
 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      10-14-2006
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
 
Reply With Quote
 
Alexandre Drolet
Guest
Posts: n/a
 
      10-15-2006
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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput 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>

 
Reply With Quote
 
Alexandre Drolet
Guest
Posts: n/a
 
      10-15-2006
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.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASE HREF and A HREF="#" onclick="..." Vincent van Beveren Javascript 2 07-06-2006 08:33 AM
href="javascript:func()" vs href="#" onclick="javascript:func()" CRON HTML 24 06-20-2006 08:05 PM
onClick method question (this.href and document.location.href) yogesh.bhardwaj@gmail.com Javascript 2 02-03-2005 02:38 PM
difference between location.href and window.location.href? saiho.yuen Javascript 3 09-14-2004 06:51 PM
Problem: Setting MSIE iframe innerHTML change relative href/src to absolute href/src Soren Vejrum Javascript 4 07-05-2003 01:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57