XML
  Home arrow XML arrow Page 4 - XSL Basics (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
XML

XSL Basics (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2001-08-31


    Table of Contents:
  • XSL Basics (part 2)
  • Mercury Rising
  • The Number Game
  • A Node By Any Other Name
  • Looping The Loop
  • Sorting Things Out
  • Be Cool
  • Flavour Of The Month
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    XSL Basics (part 2) - A Node By Any Other Name
    ( Page 4 of 9 )

    XSLT also comes with a bunch of instructions designed to help authors create nodes in the result tree using XSLT instructions, rather than literal character data. These come in particularly handy when elements, attributes, PIs or text strings need to be generated dynamically.

    In order to illustrate this, let's consider a small snippet from the stock data you just saw:

    <?xml version="1.0"?> <portfolio> <stock> <symbol>HHDT</symbol> <oprice>100</oprice> <cprice>25</cprice> </stock>

    <stock> <symbol>NHSJ</symbol> <oprice>20</oprice> <cprice>1000</cprice> </stock> </portfolio>
    Now, let's suppose that, for some twisted reason of my own, I wanted to convert this source tree into the following result tree:

    <portfolio> <HHDT oprice="100" cprice="25" />

    <NHSJ oprice="20" cprice="1000" />

    </portfolio>
    Here's how I could accomplish it with XSLT:

    <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:element name="portfolio"> <xsl:apply-templates /> </xsl:element> </xsl:template> <xsl:template match="//stock"> <xsl:element name="{symbol}"> <xsl:attribute name="oprice"> <xsl:value-of select="oprice" /> </xsl:attribute> <xsl:attribute name="cprice"> <xsl:value-of select="cprice" /> </xsl:attribute> </xsl:element> </xsl:template> </xsl:stylesheet>
    The <xsl:element> instruction is used to create a new element in the result tree; the name of the element to be created is specified via the "name" attribute. In case the element name needs to be generated dynamically - as in the above example, where the element name depends on the stock symbol - an expression can be used, enclosed within curly braces.

    In the example above, the elements to be created are "empty" elements - that is, they do not enclose any content. In case I wanted to add some content between the opening and closing tags, I would use the following instruction:

    <xsl:element name="{symbol}"> I rock! </xsl:element>
    If you take a close look at the first template rule above, you'll see that I've actually used this technique when creating the outermost "portfolio" element.

    Once an element has been created, it may be necessary to assign it some attributes - this is accomplished via the <xsl:attribute> instruction. The syntax here is similar to that of <xsl:element>, although <xsl:attribute> instructions must always be contained within either literal or dynamically-created parent elements.

    As you may have guessed, XSLT doesn't just restrict you to creating elements and attributes dynamically - you can also create text nodes, PIs and comments, using the <xsl:text>, <xsl:processing-instruction> and <xsl:comment> instructions respectively. Take a look at the following examples, which demonstrate how this works:

    <xsl:template match="/node"> <xsl:comment> Superman was here! </xsl:comment> </xsl:template> <xsl:template match="/node"> <xsl:text> Mary had a little lamb </xsl:text> </xsl:template> <xsl:template match="/node"> <xsl:processing-instruction name="bite_me" /> </xsl:template>
    Obviously, text, comments and PIs can also be generated dynamically, as previously demonstrated with elements and attributes.

     
     
    >>> More XML Articles          >>> More By icarus, (c) Melonfire
     

       

    XML ARTICLES

    - Flex Array Collection Sort and Filtering
    - The Flex Tree Control
    - Flex List Controls
    - Working with Flex and Datagrids
    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek