SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: XSL help

  1. #1
    Froot r gewd SubKamran's Avatar
    Join Date
    May 2002
    Location
    North Star State
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XSL help

    How do I put in a link and image that needs to grab the data from an XML tag in a XSL stylesheet?

    Also, what are some good books or sites on XSL for beginners?
    Last edited by SubKamran; Mar 17, 2003 at 08:51.
    "Sometimes little is more."
    Kamran A
    Web Dev/Designer
    Keyboard not found: Please Press F1 to Continue

  2. #2
    Bangarang! Karloff's Avatar
    Join Date
    Mar 2003
    Location
    Manchester, United Kingdom
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SubKamran
    How do I put in a link and image that needs to grab the data from an XML tag in a XSL stylesheet?


    Wasn't quite sure what you meant. I have attached a sample link collection that is held in an XML file links.xml and the complimentary XSL named transform.xsl. There is also an image folder that merely holds a few images.

    You will see that I included internal and external links plus different styling and href targeting for these link types. Basically, you can let your imagination run free there e.g. have XSL sort your links by type, alphabetically etc. with prefixing appropriate headers for each type. You can also add categories and and and... there is so much neat stuff you could but I left out so you have some fun too :-)

    NOTE: My attached sample works identical on IE 6 and Mozilla 1.3 final release. haven't tried it elsewhere.

    links.xml
    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="transform.xsl"?>
    <linkcollection>
     <summary>
      <heading>Sample Link List</heading>
      <description>Shows how to use XSL to transform an XML link collection.</description>
      <author>Karloff :: Karl Offenberger</author>
     </summary>
     <links>
      <link type="internal">
       <name>Resources</name>
       <description>Links to resources on search engine optimization</description>
       <source>http://www.yourdomain.com/links/resources</source>
       <image source="emptyspacer.gif" width="1" height="30" />
      </link>
      <link type="internal">
       <name>Articles</name>
       <description>Articles about search agents.</description>
       <source>http://www.yourdomain.com/links/articles</source>
       <image source="emptyspacer.gif" width="1" height="30" />
      </link>
      <link type="external">
       <name>Google</name>
       <description>What more do you need than Google?</description>
       <source>http://www.google.com/</source>
       <image source="google.gif" width="77" height="30" />
      </link>
      <link type="external">
       <name>AltaVista</name>
       <description>A former contender for the topspot. Now rather defunct.</description>
       <source>http://www.altavista.com/</source>
       <image source="altavista.gif" width="77" height="30" />
      </link>
      <link type="external">
       <name>Looksmart</name>
       <description>Yet another one.</description>
       <source>http://www.looksmart.com/</source>
       <image source="looksmart.gif" width="151" height="30" />
      </link>
      <link type="external">
       <name>Alltheweb</name>
       <description>Powered by FAST!</description>
       <source>http://www.alltheweb.com/</source>
       <image source="alltheweb.gif" width="133" height="30" />
      </link>
      <link type="external">
       <name>Yahoo</name>
       <description>Search engine, directory, portal - whatever?</description>
       <source>http://www.yahoo.com/</source>
       <image source="yahoo.gif" width="136" height="30" />
      </link>
     </links>
    </linkcollection>




    transform.xsl
    Code:
    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0"
    	  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
    <html>
    <head>
    <title><xsl:value-of select="//summary/heading"/></title>
    <style>
    h1 {font: bold 16pt Verdana; color: #333333;}
    p {font: 11pt Verdana; color: #333333;}
    p.author {font: 9pt Verdana; color: #999999; margin-left: 155px;}
    img.linklist-image {border:none;}
    a.linklist {font: 11pt Verdana;}
    </style>
    </head>
    <body>
    <h1><xsl:value-of select="//summary/heading"/></h1>
    <p><xsl:value-of select="//summary/description"/></p>
    <table>
    <xsl:for-each select="//links/link">
     <tr>
      <td align="right">
       <a>
       <xsl:attribute name="title">
    	<xsl:value-of select="name" />
       </xsl:attribute>
       <xsl:attribute name="href">
    	<xsl:value-of select="source" />
       </xsl:attribute>
       <img class="linklist-image">
    	<xsl:attribute name="src">
    	 images/<xsl:value-of select="image/@source" />
    	</xsl:attribute>
    	<xsl:attribute name="width">
    	 <xsl:value-of select="image/@width" />
    	</xsl:attribute>
    	<xsl:attribute name="height">
    	 <xsl:value-of select="image/@height" />
    	</xsl:attribute>
       </img>
       </a>
      </td>
      <td>
       <a class="linklist">
       <xsl:attribute name="title">
    	<xsl:value-of select="name" />
       </xsl:attribute>
       <xsl:attribute name="href">
    	<xsl:value-of select="source" />
       </xsl:attribute>
       <xsl:attribute name="style">
    	<xsl:choose>
    	 <xsl:when test="@type = 'external'">
    	  color:#FF9900;
    	 </xsl:when>
    	 <xsl:otherwise>
    	  color:#3399FF;
    	 </xsl:otherwise>
    	</xsl:choose>
       </xsl:attribute>
       <xsl:if test="@type = 'external'">
    	<xsl:attribute name="target">_blank</xsl:attribute>
       </xsl:if>
       <xsl:value-of select="description" />
       </a>
      </td>
     </tr>
    </xsl:for-each>
    </table>
    <p class="author"><b>Author: </b><xsl:value-of select="//summary/author"/></p>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>




    Quote Originally Posted by SubKamran
    Also, what are some good books or sites on XSL for beginners?
    Personally, I do not like books that explain things in steps or make promises such as '... in 24 days'. I simply go for reference style books or advanced books. I never bought a book on XSL though I had a few pointers from WROX's 'Professional XML' 1st edition.

    Other than that I have only used MSDN and the MSXML 4.0 help (which is also downloadable and HIGHLY recommendable).


    Hope this can get you started. Enjoy,
    Attached Files Attached Files
    Last edited by Karloff; Mar 17, 2003 at 18:48.
    Karl


    I'm desperately trying to figure out why Kamikaze pilots wore helmets. - George Carlin

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also remember that XHTML does not support the TARGET attribute as HTML does.... though there are alternatives, Kevin Yank done a very recent article on this, so have a look for it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •