Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Display a filtered list using XSLT

Following on from the post A simple XSLT example [dzone.com], this code lists all files which have the type 'rb' (equivalent to ls *.rb).

file: dir.xsl
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      
    <xsl:template match="dir">
    <div id="articles">
      <ul>
      <xsl:apply-templates select="records/file[@type='rb']"/>
      </ul>
    </div>
    </xsl:template>
    
    <xsl:template match="records/file[@type='rb']">
      <li><xsl:value-of select="."/></li>
    </xsl:template>
    
</xsl:stylesheet>

output:
<div id='articles'>
  <ul>
    <li>projxmlhelper.rb</li>
    <li>feedpopulated.rb</li>
    <li>squrl_handler.rb</li>
    <li>password_handler.rb</li>
    <li>category.rb</li>
    <li>gwd.rb</li>
  </ul>
</div>



*update 11:58am 26-Feb*
Here's a filter I will be using in my projects

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      
    <xsl:template match="dir">
    <div id="articles">
      <ul>
      <xsl:apply-templates select="records/file"/>
      </ul>
    </div>
    </xsl:template>
    
    <xsl:template match="records/file">
      <xsl:if test="@type=$type">
        <li><xsl:value-of select="."/></li>
      </xsl:if>
    </xsl:template>
    
</xsl:stylesheet>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS