Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 8th, 2005, 03:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default XSLT for links

Hello,

What I want do is have a series of paragraphs like this:

<Content>
  <Paragraph>This is my content<Paragraph>
  <Paragraph>This is my content <Link href="http://p2p.wrox.com">Forums</Link><Paragraph>
</Content>

I want to render the paragraphs, and the links within the paragraphs. This is what I came up with XSLT so far:

<xsl:template match="Content">
    <xsl:for-each select="Paragraph">
        <xsl:if test="position() &gt; 1">
            <br/><br/>
        </xsl:if>

        <xsl:value-of select="."/>
    </xsl:for-each>
</xsl:template>

But I can't figure out how to render the links. Any ideas?

Brian
__________________
Brian
 
Old February 9th, 2005, 06:25 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The following could be slimmed down a bit but is easy to alter if your content becomes more complicated. I changed from a 'for-each' to an apply-templates' but that's really a question of style really. The default template for text nodes which just copies them to the output is used for the main content, anything that needs special treatment has its own template.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head><title>Paragraphs</title></head>
      <body>
        <xsl:apply-templates select="Content"/>    
      </body>
    </html>    
  </xsl:template>

  <xsl:template match="Content">    
    <xsl:apply-templates select="Paragraph"/>  
  </xsl:template>

  <xsl:template match="Paragraph">
    <xsl:if test="position() != 1">
      <br/><br/>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="Link">
    <a href="@href"><xsl:value-of select="text()"/></a>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old February 9th, 2005, 02:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Thanks, I'll have to try that. So the <xsl:apply-templates/> in the Paragraph template knows to check for whatever template for the element it has come across, such as the paragraph content or any inner elements?

Brian
 
Old February 10th, 2005, 07:32 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It processes all children of the current node.

--

Joe (Microsoft MVP - XML)
 
Old February 10th, 2005, 08:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

<xsl:template match="/"> (at the top)

This is the only problem that I have. It won't recognize the root element with this, instead, I have to have a separate element for the root (named article). How can I do that...

Brian
 
Old February 10th, 2005, 08:38 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Change it to:
Code:
<xsl:template match="/article">
--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox - XSLT - document.links has no properties tripecac XSLT 5 April 27th, 2007 12:35 PM
Links that disappear Surre ASP.NET 1.0 and 1.1 Professional 8 February 2nd, 2007 11:15 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
Issues with Links bmains Forum and Wrox.com Feedback 3 September 6th, 2004 03:15 PM
Broken Links kilika Forum and Wrox.com Feedback 1 September 15th, 2003 10:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.