XML
  Home arrow XML arrow Page 3 - An Introduction to XSLT
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

An Introduction to XSLT
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2004-09-20

    Table of Contents:
  • An Introduction to XSLT
  • Create Elements to Apply the Transformations
  • Another Template
  • Discussion of the Output
  • Page Masters
  • Block Level Elements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    An Introduction to XSLT - Another Template


    (Page 3 of 6 )

    We start another template that looks for children of the <article> element. We'll now use another XSL element, the for-each element:

    <xsl:for-each select="headline">
      <B><xsl:value-of/></B><BR/>
    </xsl:for-each>

    This template looks for elements that match <headline> and then copies their content, makes them bold and adds a line-break. We then need another for-each template to match any <story> patterns:

    <xsl:for-each select="story">
      <xsl:value-of/><BR/>
    </xsl:for-each> 
     
    This time, we don't want the contents made bold, but we can still include a line-break. Now we just close the template and the root element:

      </xsl:template>
    </xsl:stylesheet>

    That's it, as far as the stylesheet goes. Save the file as style.xls in the same folder as your XML file. For reference, the whole stylsheet should look like this:

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
      <xsl:template match="/">
        <Html>
     <Head>
       <Title>News</Title>
     </Head>
     <Body>
       <CENTER><H1>Todays News</H1></CENTER><BR/>
       <xsl:apply-templates select="news/article"/>
     </Body>
        </Html>
      </xsl:template>
      <xsl:template match="article">
        <xsl:for-each select="headline">
     <B><xsl:value-of/></B><BR/>
        </xsl:for-each>
        <xsl:for-each select="story">
          <xsl:value-of/><BR/>
        </xsl:for-each> 
      </xsl:template>
    </xsl:stylesheet>

    Before this will actually work, you need to link the XLST file to the existing XML file, to do this, open the original XML file (news.xml if you're following on from the previous article) and add the following line of code directly below the XML declaration at the top:

    <?xml-stylesheet type="text/xsl" href="style.xsl"?>

    Now save this file with the name transformedNews.xml and then open it in a browser.  You should see a page with the Today's News heading at the top of the page, and the two news stories with their associated headings below this.  If you open the XSL file, it will look very similar to a flat XML file, with the declaration and tags in blue and the elements in red.  Additionally, XSLT elements will appear in a nice shade of purple. 

    More XML Articles
    More By Dan Wellman


       · What is it with XML authors that can't use basic HTML correctly?Mixed case HTML...
       · excellent point, i know what you mean by this valid stuff...i see it from many...
       · Hi,Yes Dev ur right. It happens when u program it without going through its...
     

    XML ARTICLES

    - Using Regions with XSL Formatting Objects
    - Using XSL Formatting Objects
    - More Schematron Features
    - Schematron Patterns and Validation
    - Using Schematron
    - Datatypes and More in RELAX NG
    - Providing Options in RELAX NG
    - An Introduction to RELAX NG
    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT