Having difficulty making hyperlinks work in XML doc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobClack
    New Member
    • Mar 2007
    • 13

    Having difficulty making hyperlinks work in XML doc

    I'm quite new to xml and xsl, so this could be quite simple. I have an xml file with some data in it and an xsl file to process it into a web page. What doesn't work is hyperlinks. I've read endless pages about how xlink works, but for me it just doesn't. No errors, just plain text and no hyperlinks. I'm running Firefox 2.0.0.3 but have also tried in IE7, getting exactly the same result.
    Here is some sample xml, lifted from w3schools and slightly massaged. I hope someone can point out where I'm going wrong.

    Thanks
    Rob

    [html]<?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="bookstore .xsl"?>

    <bookstore xmlns:xlink="ht tp://www.w3.org/1999/xlink">

    <book>
    <description xlink:type="sim ple" xlink:href="../RPS/ComingUp.xml">
    Text1
    </description>
    </book>

    <book>
    <description
    xlink:type="sim ple"
    xlink:href="htt p://www.cam.ac.uk"
    xlink:show="new ">Text2</description>
    </book>

    </bookstore>[/html]

    and some corresponding xsl

    [html]<?xml version="1.0" encoding="ISO-8859-1"?>

    <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <html>

    <head>
    <meta http-equiv="Content-Language" content="en-gb"></meta>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>
    <title>Bookstor e</title>
    <link rel="stylesheet " type="text/css" href="../rps.css"></link>

    </head>

    <body>
    <xsl:for-each select="booksto re/book">
    <xsl:value-of select="descrip tion"/><br/>
    </xsl:for-each>
    </body>
    </html>
    </xsl:template>

    </xsl:stylesheet>[/html]

    Waning: Please use html or code tags for source code. Thanks, moderator.
    Last edited by dorinbogdan; Mar 27 '07, 06:53 AM. Reason: added html tags
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #2
    Welcome to TheScripts TSDN...

    Try this code:
    [html]<?xml version="1.0" encoding="ISO-8859-1"?>

    <xsl:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
    xmlns:xlink="ht tp://www.w3.org/1999/xlink"
    exclude-result-prefixes="xlink " >

    <xsl:template match="/">
    <html>

    <head>
    <meta http-equiv="Content-Language" content="en-gb"></meta>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>
    <title>Bookstor e</title>
    <link rel="stylesheet " type="text/css" href="../rps.css"></link>


    </head>

    <body>
    <xsl:for-each select="booksto re/book">
    <xsl:if test="descripti on[@xlink:type = 'simple' and @xlink:href]">
    <a href="{descript ion/@xlink:href}">< xsl:value-of select="descrip tion"/></a><br />
    </xsl:if>

    </xsl:for-each>
    </body>
    </html>
    </xsl:template>

    </xsl:stylesheet>[/html]

    For more details see Building XLink Applications with XSLT.

    Comment

    • RobClack
      New Member
      • Mar 2007
      • 13

      #3
      Thank you for your advice. Sadly, when I run your code, I just get a blank page. When I removed the if test, the page showed with hyperlinks, which is definite progress. Unfortunately the links just point back to the xml file, not to the URL I specified in the xlink:href attribute, viz "file:///E:/www/TryOut/book2.xml" instead of, for example, "http://www.cam.ac.uk".
      Thanks
      Rob

      Comment

      • dorinbogdan
        Recognized Expert Contributor
        • Feb 2007
        • 839

        #4
        I tested on IE7 and FireFox 2.0.
        Clicking on Text2 link has open http://www.cam.ac.uk/ site.

        What browser are you using?

        Comment

        • RobClack
          New Member
          • Mar 2007
          • 13

          #5
          I copied some example code from the Building XLink Applications with XSLT document you referred me to, and at last I see something that works. Hooray! It can be done! Sadly, I'm too stupid to be able to work out how to use the working stuff in my code. I've tried reading about apply-templates, but so far I don't really "get" it.

          Comment

          • dorinbogdan
            Recognized Expert Contributor
            • Feb 2007
            • 839

            #6
            Regarding XSL syntax and reference see this link.
            And please post any related questions.

            Comment

            • RobClack
              New Member
              • Mar 2007
              • 13

              #7
              I'm using Firefox 2.0.0.3 running on Windows XP SP2. When I hover the mouse pointer over the link the status bar shows the file:///E:..blah url I referred to above. Clicking it does nothing, since the URL is clearly just pointing back to the source xml doc for the page.

              Comment

              • dorinbogdan
                Recognized Expert Contributor
                • Feb 2007
                • 839

                #8
                Please post your final XSL code.
                Also, try to test it on a different machine.
                I'm working on W2003 and it was fine.

                Comment

                • RobClack
                  New Member
                  • Mar 2007
                  • 13

                  #9
                  I'll try this out when I get home - my home PC is marginally more up to date than this one here at work! As I said somewhere above, this version puts up hyperlinks, but they just point back to the xml source file.

                  If I can get this version to fly that'll be good, since the next thing I want to do is embed the whole lot in a table, which I can easily see how to do. With templates, it may well be the way forward, but so far I can't see how to do it.
                  Thanks

                  xml code
                  Code:
                  <?xml version="1.0" encoding="ISO-8859-1"?>
                  <?xml-stylesheet type="text/xsl" href="book2.xsl"?>
                  
                  <bookstore xmlns:xlink="http://www.w3.org/1999/xlink/">
                  
                  <book>
                    <description 
                      xlink:type="simple" 
                      xlink:href="../RPS/ComingUp.xml"
                      xlink:show="replace">Text1
                    </description>
                  </book>
                  
                  <book>
                    <description
                      xlink:type="simple"
                      xlink:href="http://www.cam.ac.uk"
                      xlink:show="replace">Text2
                    </description>
                  </book>
                  
                  </bookstore>
                  xsl code
                  Code:
                  <?xml version="1.0" encoding="ISO-8859-1"?> 
                  <xsl:stylesheet version="1.0" 
                      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                      xmlns:xlink="http://www.w3.org/1999/xlink" 
                      exclude-result-prefixes="xlink" > 
                  
                  <xsl:template match="/">  
                  <html>  
                  <head>  
                    <meta http-equiv="Content-Language" content="en-gb"></meta>  
                    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>  
                    <title>Bookstore</title>  
                    
                    <link rel="stylesheet" type="text/css" href="../rps.css"></link>  
                  </head>  
                  
                  <body>  
                    <xsl:for-each select="bookstore/book">  
                        <a href="{description/@xlink:href}">
                          <xsl:value-of select="description"/></a><br />  
                    </xsl:for-each>  
                  </body>  
                  </html> 
                  </xsl:template> 
                  </xsl:stylesheet>

                  Comment

                  • dorinbogdan
                    Recognized Expert Contributor
                    • Feb 2007
                    • 839

                    #10
                    Strange behavior there.

                    I'm curious what you get if use:
                    [html] <a href="{descript ion/@xlink:href}">
                    <xsl:value-of select="descrip tion/@xlink:href"/></a><br />
                    [/html]

                    Comment

                    • RobClack
                      New Member
                      • Mar 2007
                      • 13

                      #11
                      I changed the code as you suggest and got a completely blank page. So I stuck xxx on either side of the "value-of select" tag and got xxx xxx. I think the url being generated is a single blank space and under those circumstances there's something in the browser or xslt (or some other magic!) that substututes in the path to the xml file.

                      It would be good if I could find a way to see the html that's been generated, but View/Page Source just shows the original xml.

                      Comment

                      • dorinbogdan
                        Recognized Expert Contributor
                        • Feb 2007
                        • 839

                        #12
                        Hmm, try to replace the "../RPS/ComingUp.xml" href in XML with an http://www.yahoo.com, just for test.
                        I think that if an error occured here (due to invalid path), the for-each failed to complete.

                        Comment

                        • RobClack
                          New Member
                          • Mar 2007
                          • 13

                          #13
                          Sorry, no joy. Actually, if it works for you but not for me then it might, as you suggested earlier, be something to do with the environment. This may not be relevant, but I noticed that if I run it in IE7 it fills in the path to the xml file directory, but not the filename itself, so when I click the link, it opens a File Open dialogue box. (yes, the spelling confirms I'm in the UK ;-)

                          Comment

                          • dorinbogdan
                            Recognized Expert Contributor
                            • Feb 2007
                            • 839

                            #14
                            Ok, but what about the last suggestion? That is, to replace the "../RPS/ComingUp.xml" href in XML with a valid web address, i.e. "http://www.yahoo.com". Just for test.
                            Thanks.

                            Comment

                            • RobClack
                              New Member
                              • Mar 2007
                              • 13

                              #15
                              One day, I promise, I'll learn to communicate unambiguously!
                              I replaced the reference to a local file with http://www.yahoo.com as you suggested, but it made no difference. The text inside the <a></a> tags translates to a single blank (or possibly unprintable) character, so nothing appears. If I pre- and postfix the content with, say, xxx, then the link appears, xxx xxx but just points to the xml file.

                              Comment

                              Working...