XSLT href link

Discussion in 'XML' started by Per Johansson, Sep 20, 2005.

  1. 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/
    Per Johansson, Sep 20, 2005
    #1
    1. Advertising

  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>


    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/
    Martin Honnen, Sep 20, 2005
    #2
    1. Advertising

  3. 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."
    Joe Kesselman, Feb 3, 2006
    #3
  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
    Joe Kesselman, Oct 14, 2006
    #4
  5. 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 :
    > 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>
    Alexandre Drolet, Oct 15, 2006
    #5
  6. 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.
    >
    Alexandre Drolet, Oct 15, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    200,708
    Adrienne Boswell
    Jun 20, 2006
  2. Soren Vejrum
    Replies:
    4
    Views:
    579
    Lasse Reichstein Nielsen
    Jul 5, 2003
  3. saiho.yuen
    Replies:
    3
    Views:
    422
    kaeli
    Sep 14, 2004
  4. Replies:
    2
    Views:
    463
  5. Vincent van Beveren

    BASE HREF and A HREF="#" onclick="..."

    Vincent van Beveren, Jul 6, 2006, in forum: Javascript
    Replies:
    2
    Views:
    314
    Vincent van Beveren
    Jul 6, 2006
Loading...

Share This Page