SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to show XML links in web page?

    Hi I wonder if anyone can help. I have an RSS XML file with 2 items:



    Code:
    <rss version="2.0">
    <channel>
    <title>Skills for OU Study</title>
    <link>http://www.open.ac.uk/skillsforstudy</link>
    <language>en-us</language>
    <description>Helps you develop the study skills you need for your OU course, and get the confidence you need to succeed.</description>
    
    <item>
      <title>Mobile stats tool</title>
      <link>http://www.open.ac.uk/skillsforstudy/using-maths-and-statistics.php</link>
      <description> Get to grips with averages and graph types with the <a href="test.php">mobile</a> stats tool for your mobile phone.</description>
      <pubDate>Mon, 13 Oct 2008 09:00:00 GMT</pubDate>
      <author>ss-ldt@open.ac.uk (Learning Design &amp; Technology)</author>
      <guid isPermaLink="true">http://www.open.ac.uk/skillsforstudy/using-maths-and-statistics.php</guid>  
      <category>Using maths and statistics</category>  
    </item>
    
    
    
    <item>
      <title>Keep to schedule</title>
      <link>http://www.open.ac.uk/skillsforstudy/helpful-time-management-tips.php</link>
      <description>Learn how to keep a grip on time and keep to schedule.</description>
      <pubDate>Mon, 13 Oct 2008 09:00:00 GMT</pubDate>
      <author>ss-ldt@open.ac.uk (Learning Design &amp; Technology)</author>
      <guid isPermaLink="true">http://www.open.ac.uk/skillsforstudy/helpful-time-management-tips.php</guid>  
      <category>Develop effective study strategies</category>  
    </item>
    
    </channel>
    </rss>
    This works fine but I want it to appear in a web page so I used this XSL file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    
    <xsl:template match="channel">    
         <ul><xsl:apply-templates select="item" /></ul>      
    </xsl:template>
    
    
    <xsl:template match="item">    
        <li><xsl:value-of select="description"/></li>         
    </xsl:template>
    </xsl:stylesheet>
    And this code on the home page:

    PHP Code:
    <?php 
    // Load the XML source
    $xml = new DOMDocument;
    $xml->load('rss.xml');

    $xsl = new DOMDocument;
    $xsl->load('rss.xsl');

    // Configure the transformer
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl); // attach the xsl rules

    echo $proc->transformToXML($xml);  
    ?>

    This works OK and both item descriptions are shown as list items on the home page. However I really want to show links in the description (list items) but when I put them in to the RSS file they are ignored. Is there anyway I can do this?

    Thanks
    Steven

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what did you do to test it, because it should work:
    Code:
    <xsl:template match="item">    
        <li>
          <a href="{link}" alt="{description}" title="{description}">
             <xsl:value-of select="description"/>
          </a>
         </li>         
    </xsl:template>
    Maybe you did not know it, but when you want to fetch the value of a node into an attribute of another, you replace the "<xsl:value-of />" with "{node}"

  3. #3
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip. I didn't want to make the whole description a link, so I made the title a link instead:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    
    <xsl:template match="channel">    
         <ul><xsl:apply-templates select="item" /></ul>      
    </xsl:template>
    
    
    <xsl:template match="item">    
        <li>
        <xsl:apply-templates select="title" /> <br />
        <xsl:apply-templates select="description" /> <br />    
        </li>         
    </xsl:template> 
    
    <xsl:template match="title">    
       <a>
       <xsl:attribute name="href"><xsl:value-of select="../link"/></xsl:attribute>
       <xsl:value-of select="."/>
       </a>       
    </xsl:template>
    
    
    <xsl:template match="description">    
        <xsl:value-of select="."/>        
    </xsl:template> 
    
    </xsl:stylesheet>

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, is it working now ?

  5. #5
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah - its working well - but what i'd really like to do is just have the first 3 links on the home page and the rest on an archive page - know how to do this?

    Steven

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rather than using xsl:aply-templates, use an xsl:for, and limit it to the 3 first elements.
    For that, you will need to convert your <xsl:template match=""> to <xsl:template name="">

    And no need to overuse the <xsl apply-templates>, for something as simple as this
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="channel">
        <xsl:for-each select="item[position() < 4]">
          <xsl:call-template name="item"/> 
        </xsl:for-each> 
      </xsl:template>
      
      <xsl:template name="item">
          <li>
            <a>
              <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
              <xsl:value-of select="title"/>
            </a>
            <xsl:value-of select="description"/>
          </li>
      </xsl:template>
      
    </xsl:stylesheet>

  7. #7
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, great idea but I can't get it to work. This is what I have got:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlnssl="http://www.w3.org/1999/XSL/Transform">


    Code:
    <xsl:template match="channel">    
         <ul>
          <xsl:for-each select="item[position() < 4]">
          <xsl:call-template name="item"/> 
          </xsl:for-each>
        </ul>      
    </xsl:template>
    
    
    <xsl:template name="item">    
        <li>
        <xsl:apply-templates select="title" /> <br />
        <xsl:apply-templates select="description" /> <br />    
        </li>         
    </xsl:template> 
    
    <xsl:template match="title">    
       <a>
       <xsl:attribute name="href"><xsl:value-of select="../link"/></xsl:attribute>
       <xsl:value-of select="."/>
       </a>       
    </xsl:template>
    
    
    <xsl:template match="description">    
        <xsl:value-of select="."/>        
    </xsl:template> 
    
    </xsl:stylesheet>
    Any ideas?

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, my bad, it's what you get when you don't test...
    1) The root element of the xml is not <channel>, but <rss>
    2) select="item[position() < 4]" is bound to fail, because of "<". the correct syntax is select="channel/item[position() &lt; 4]"

    That being changed, I now have:
    rss's xml:
    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet href="1.xsl" type="text/xsl"?>
    <rss version="2.0">
      <channel>
        <title>Skills for OU Study</title>
        <link>http://www.open.ac.uk/skillsforstudy</link>
        <language>en-us</language>
        <description>Helps you develop the study skills you need for your OU course, and get the confidence you need to succeed.</description>
        
          <item>
            <title>Mobile stats tool</title>
            <link>http://www.open.ac.uk/skillsforstudy/using-maths-and-statistics.php</link>
            <description> Get to grips with averages and graph types with the <a href="test.php">mobile</a> stats tool for your mobile phone.</description>
            <pubDate>Mon, 13 Oct 2008 09:00:00 GMT</pubDate>
            <author>ss-ldt@open.ac.uk (Learning Design &amp; Technology)</author>
            <guid isPermaLink="true">http://www.open.ac.uk/skillsforstudy/using-maths-and-statistics.php</guid>  
            <category>Using maths and statistics</category>  
          </item>
          
          
          
          <item>
            <title>Keep to schedule</title>
            <link>http://www.open.ac.uk/skillsforstudy/helpful-time-management-tips.php</link>
            <description>Learn how to keep a grip on time and keep to schedule.</description>
            <pubDate>Mon, 13 Oct 2008 09:00:00 GMT</pubDate>
            <author>ss-ldt@open.ac.uk (Learning Design &amp; Technology)</author>
            <guid isPermaLink="true">http://www.open.ac.uk/skillsforstudy/helpful-time-management-tips.php</guid>  
            <category>Develop effective study strategies</category>  
          </item>
        
      </channel>
    </rss>
    and as xsl (saved under 1.xsl, in the same directory than the xml):
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="/rss">
        <xsl:for-each select="channel/item[position() &lt; 4]">
        <html><body>
          <ul>
            <xsl:call-template name="item"/> 
          </ul>
        </body></html>
        </xsl:for-each> 
      </xsl:template>
      
      <xsl:template name="item">
          <li>
            <a>
              <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
              <xsl:value-of select="title"/>
            </a>
            <xsl:value-of select="description"/>
          </li>
      </xsl:template>
    
    </xsl:stylesheet>
    and it displays ok in both IE and firefox when I drop the xml file on the browser window.


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
  •