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

« Newer Snippets
Older Snippets »
Showing 11-20 of 55 total

Passing an XSL param from one template to another

   1  
   2  <?xml version="1.0" encoding="ISO-8859-1"?>
   3  <xsl:stylesheet version="1.0"
   4  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   5  
   6  <xsl:variable name="xx">
   7    <html>
   8    <body>
   9    <xsl:call-template name="show_title">
  10      <xsl:with-param name="title" />
  11    </xsl:call-template>
  12    </body>
  13    </html>
  14  </xsl:variable>
  15  
  16  <xsl:template name="show_title" match="/">
  17    <xsl:param name="title" />
  18    <xsl:for-each select="catalog/cd">
  19      <p>Title: <xsl:value-of select="$title" /></p>
  20    </xsl:for-each>
  21  </xsl:template>
  22  
  23  </xsl:stylesheet>

source: XSLT <xsl:param> Element [w3schools.com]

Using XSLT to generate SVG

This XSL file is intended to be used as an SVG XSL template file which displays SVG content using Gorg (XSLT back-end processor).
   1  
   2  <?xml version="1.0" encoding="UTF-8"?>
   3  
   4  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   5  
   6  <xsl:output encoding="UTF-8"
   7              method="xml"
   8              indent="yes"/>
   9              
  10    <xsl:template match="mainpage">
  11    <svg xmlns="http://www.w3.org/2000/svg" width="100%"
  12                  xmlns:xlink="http://www.w3.org/1999/xlink" >
  13  
  14      <g>
  15      <text font-size="12pt" x="50" y="50" id="t2" stroke="olive"><xsl:value-of select="title"/></text>
  16      </g>
  17  
  18    </svg>
  19    </xsl:template>
  20  </xsl:stylesheet>
  21  

Reference: 'using XSLT to generate SVG' http://snurl.com/24pwo [carto.net]

Format Ruby code in HTML

This code uses the Ruby gem 'syntax' to create an XML file containing the HTML tags around the code, which is then transformed into an HTML file. The working example was first built using coding examples from Howto format ruby code for blogs [wolfman.com] and Formatting Ruby and HTML code for blog posting [blogspot.com]

   1  
   2  #!/usr/bin/ruby
   3  
   4  #file: ruby2html.rb 
   5  
   6  require 'rubygems'
   7  require 'syntax/convertors/html'
   8  require 'projxslt' # <- this is my own class to do an XSLT transform 
   9  require 'rexml/document'
  10  include REXML
  11  
  12  class Ruby2Html
  13    def initialize(rubyfile, htmlfile)
  14      code = File.read(rubyfile)
  15      convertor = Syntax::Convertors::HTML.for_syntax "ruby"
  16      code_html = convertor.convert(code)
  17      
  18      tempfile = '../temp/ruby2html.xml'
  19      xslfile = '../ruby2html/ruby2html.xsl'
  20      save_file(tempfile, code_html)
  21      
  22      px = Projxslt.new(tempfile, xslfile)
  23      buffer = px.transform()
  24      save_file(htmlfile, buffer)
  25      
  26    end
  27    
  28    def save_file(filename, buffer)
  29      file = File.new(filename, 'w')
  30      file.puts buffer
  31      file.close
  32    end
  33  end
  34  
  35  if __FILE__ == $0
  36    r2h = Ruby2Html.new('ruby2html.rb', '../temp/ruby2html.html')
  37    puts 'completed'
  38  end

file: ruby2html.xsl
   1  
   2  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   3                  xmlns="http://www.w3.org/1999/xhtml"
   4                  version="1.0">
   5  
   6    <xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
   7            doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
   8            encoding="ISO-8859-1"/> 
   9  
  10  	<xsl:template match="/">
  11  	<xsl:element name="html">
  12  
  13          <head>
  14            <title>Sample code</title>
  15            <link rel="stylesheet" type="text/css" href="ruby2html.css" />
  16  	</head>
  17  
  18  	<body>
  19            <div id="wrap">
  20            <xsl:apply-templates />
  21            </div>
  22  	</body> 
  23  
  24  	</xsl:element>
  25  	</xsl:template>
  26  
  27  	<xsl:template match="pre">
  28  	  <xsl:copy-of select="."/>
  29  	</xsl:template>
  30  
  31  </xsl:stylesheet>

Here's the output from the formatted Ruby HTML code [twitxr.com]

Referemce: Syntax Manual [rubyforge.org]

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
   1  
   2  <?xml version="1.0"?>
   3  
   4  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   5        
   6      <xsl:template match="dir">
   7      <div id="articles">
   8        <ul>
   9        <xsl:apply-templates select="records/file[@type='rb']"/>
  10        </ul>
  11      </div>
  12      </xsl:template>
  13      
  14      <xsl:template match="records/file[@type='rb']">
  15        <li><xsl:value-of select="."/></li>
  16      </xsl:template>
  17      
  18  </xsl:stylesheet>

output:
   1  
   2  <div id='articles'>
   3    <ul>
   4      <li>projxmlhelper.rb</li>
   5      <li>feedpopulated.rb</li>
   6      <li>squrl_handler.rb</li>
   7      <li>password_handler.rb</li>
   8      <li>category.rb</li>
   9      <li>gwd.rb</li>
  10    </ul>
  11  </div>
  12  


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

   1  
   2  <?xml version="1.0"?>
   3  
   4  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   5        
   6      <xsl:template match="dir">
   7      <div id="articles">
   8        <ul>
   9        <xsl:apply-templates select="records/file"/>
  10        </ul>
  11      </div>
  12      </xsl:template>
  13      
  14      <xsl:template match="records/file">
  15        <xsl:if test="@type=$type">
  16          <li><xsl:value-of select="."/></li>
  17        </xsl:if>
  18      </xsl:template>
  19      
  20  </xsl:stylesheet>

A simple XSLT example

Produce a list of filenames using XML and XSLT

file: dir.xsl
   1  
   2  <?xml version="1.0"?>
   3  
   4  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   5        
   6      <xsl:template match="dir">
   7      <div id="articles">
   8        <ul>
   9        <xsl:apply-templates select="records/file"/>
  10        </ul>
  11      </div>
  12      </xsl:template>
  13      
  14      <xsl:template match="records/file">
  15        <li><xsl:value-of select="."/></li>
  16      </xsl:template>
  17      
  18  </xsl:stylesheet>


file: dir.xml
   1  
   2  <dir>
   3    <summary>
   4      <directory>./</directory>
   5    </summary>
   6    <records>
   7      <file type='xml'>mjournal.xml</file>
   8      <file type='rb'>projxmlhelper.rb</file>
   9      <file type='rb'>feedpopulated.rb</file>
  10      <file type='rb'>squrl_handler.rb</file>
  11      <file type='cgi'>snurl.cgi</file>
  12      <file type='cgi'>dynalert.cgi</file>
  13      <file type='rb'>password_handler.rb</file>
  14      <file type='rb'>category.rb</file>
  15      <file type='rb'>gwd.rb</file>
  16      <file type='cgi'>new-journal-entry.cgi</file>
  17    </records>
  18  </dir>


then transforming the XML with the command 'xsltproc dir.xsl dir.xml' produces the following:
output:
   1  
   2  <div id='articles'>
   3    <ul>
   4      <li>mjournal.xml</li>
   5      <li>projxmlhelper.rb</li>
   6      <li>feedpopulated.rb</li>
   7      <li>squrl_handler.rb</li>
   8      <li>snurl.cgi</li>
   9      <li>dynalert.cgi</li>
  10      <li>password_handler.rb</li>
  11      <li>category.rb</li>
  12      <li>gwd.rb</li>
  13      <li>new-journal-entry.cgi</li>
  14    </ul>
  15  </div>

Save a Ruby source text file to XML.

This code will output a text file as an XML file which can later be transformed into an HTML file using a back-end XSLT processor called Gorg.

   1  
   2  #!/usr/bin/ruby 
   3  
   4  #file: rubytxt2xml.rb
   5  
   6  require 'rexml/document'
   7  include REXML
   8  
   9  class RubyTxt2XML
  10    
  11    def rubytxt2xml(h)
  12      h[:infilepath] = './' if h[:infilepath].nil?
  13      h[:outfilepath] = './' if h[:outfilepath].nil?
  14      h[:xmlfile] = h[:sourcefile][/^(.*)\.\w+$/,1] + '.xml' if h[:xmlfile].nil?
  15      buffer = File.new(h[:infilepath] + h[:sourcefile],'r').read
  16      
  17      doc = Document.new
  18      doc.add_element('ruby_txt')
  19      body = Element.new('source')
  20      body.text = CData.new(buffer)
  21      doc.root.add_element(body)
  22      
  23      file = File.new(h[:outfilepath] + h[:xmlfile],'w')
  24      file.puts doc
  25      file.close
  26      
  27    end
  28  end
  29  
  30  if __FILE__ == $0
  31    rt2x = RubyTxt2XML.new
  32    rt2x.rubytxt2xml(:sourcefile  => 'rubytxt2xml.rb')
  33  end

output:
   1  
   2  <ruby_txt>
   3    <source>
   4      <![CDATA[
   5        #!/usr/bin/ruby 
   6  
   7        #file: rubytxt2xml.rb
   8  
   9        require 'rexml/document'
  10        include REXML
  11  
  12        class RubyTxt2XML
  13          
  14          def rubytxt2xml(h)
  15            h[:infilepath] = './' if h[:infilepath].nil?
  16            h[:outfilepath] = './' if h[:outfilepath].nil?
  17            h[:xmlfile] = h[:sourcefile][/^(.*)\.\w+$/,1] + '.xml' if h[:xmlfile].nil?
  18            buffer = File.new(h[:infilepath] + h[:sourcefile],'r').read
  19            
  20            doc = Document.new
  21            doc.add_element('ruby_txt')
  22            body = Element.new('source')
  23            body.text = CData.new(buffer)
  24            doc.root.add_element(body)
  25            
  26            file = File.new(h[:outfilepath] + h[:xmlfile],'w')
  27            file.puts doc
  28            file.close
  29            
  30          end
  31        end
  32  
  33        if __FILE__ == $0
  34          rt2x = RubyTxt2XML.new
  35          rt2x.rubytxt2xml(:sourcefile  => 'rubytxt2xml.rb')
  36        end
  37      ]]>
  38    </source>
  39  </ruby_txt>

Using XSLT to convert a value to upper or lowercase

This code was copied from XSLT Case Conversion Solution [topxml.com]

Firstly lets place all the letters of the alphabet, lower case and upper case in variables.
   1  <xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
   2  <xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

To then convert our data to upper case we use the translate method, which replaces all the lower case characters with upper case.
   1  <xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/>

Lower Case Transformation

The lower case transformation is basically the same:
   1  <xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>

Converting XHTML to XML

Based on the code from 'Convert from HTML to XML with HTML Tidy', this code will read an xhtml file and extract text to gallery.xml as instructed by xhtml2xml.xml

   1  
   2  #!/usr/bin/ruby
   3    
   4    require 'tidy'
   5    require 'projxslt'
   6    
   7    FILE_PATH = "../"
   8    
   9    class Xhtml2Xml
  10      def convert()
  11        project = 'xhtml2xml'
  12        filein = 'xhtml2xml.xml'
  13        filehtml = 'gallery.html'
  14        filexml = 'gallery_xhtml.xml'
  15        xslfile_temp = 'gallery.xsl'
  16        xslfile = 'xhtml2xml.xsl'
  17        fileout = 'gallery.xml'
  18        tidy_config = 'tidy.txt'
  19        
  20        project_path = FILE_PATH + project + '/'
  21        tidy_config_path = project_path + tidy_config
  22        filein_path = project_path + filein
  23        filehtml_path = project_path + filehtml
  24        filexml_path = project_path + filexml
  25        xslfile_temp_path = project_path + xslfile_temp
  26        xslfile_path = project_path + xslfile
  27        fileout_path = project_path + fileout
  28        
  29        Tidy.path = '/usr/lib/libtidy.so'
  30  
  31        file = File.new(filehtml_path,'r')
  32        buffer = file.read
  33        xml = Tidy.open(:show_warnings=>true) do |tidy|
  34          tidy.options.output_xml = true
  35          tidy.load_config(tidy_config_path)
  36          puts tidy.options.show_warnings
  37          xml = tidy.clean(buffer)
  38          puts tidy.errors
  39          puts tidy.diagnostics
  40          xml
  41        end
  42        
  43        #strip out the html document type declaration and save the file
  44        html_declaration = xml[/<!([^>]*>){2}/]
  45        save_file(filexml_path, xml.gsub(html_declaration,'<html>'))    
  46        transform(filein_path, xslfile_path, xslfile_temp_path)
  47        transform(filexml_path, xslfile_temp_path, fileout_path)
  48        
  49      end
  50      
  51      def transform(xml_filepath, xsl_filepath, save_filepath)
  52        pxsl = Projxslt.new(xml_filepath, xsl_filepath)
  53        outfile = pxsl.transform
  54        save_file(save_filepath, outfile)
  55      end
  56      
  57      def save_file(filepath, buffer)
  58        file = File.new(filepath,'w') 
  59        file.puts buffer
  60        file.close
  61      end    
  62    end
  63    
  64    if __FILE__ == $0
  65      h2x = Xhtml2Xml.new()
  66      h2x.convert()
  67    end

file: xhtml2xml.xml
   1  
   2  <root element="gallery">
   3    <summary>
   4      <field element="title" xpath="head/title"/>
   5    </summary>
   6    <record xpath="body/center/table/tr/td" element="photo">
   7      <field xpath="font/br[3]/preceding-sibling::text()[1]" element="title"></field>
   8      <field xpath="/html/body/table/tr/td[2]/font/br[3]/preceding-sibling::text()[1]" element="date"></field>
   9      <field xpath="font/br[1]/preceding-sibling::text()[1]" element="image"></field>
  10      <field xpath="font/br[2]/preceding-sibling::text()[1]" element="description"></field>
  11    </record>
  12  </root>

file:xhtml2xml.xsl (transforms the file xhtml2xml.xml to file gallery.xsl)
   1  
   2  <?xml version="1.0"?>
   3  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   4    <xsl:template match="root">
   5      <xsl:variable name="colon"><xsl:text>:</xsl:text></xsl:variable>
   6      
   7      <xsl:element name="xsl:stylesheet">
   8        <xsl:attribute name="xmlns{$colon}xsl">
   9          <xsl:text>http://www.w3.org/1999/XSL/Transform</xsl:text>
  10        </xsl:attribute>
  11        <xsl:attribute name="version">
  12          <xsl:text>1.0</xsl:text>
  13        </xsl:attribute><xsl:text>
  14        </xsl:text>
  15  
  16  <xsl:element name="xsl:output">
  17    <xsl:attribute name="method">
  18      <xsl:text>xml</xsl:text>
  19    </xsl:attribute>
  20    <xsl:attribute name="indent">
  21      <xsl:text>yes</xsl:text>
  22    </xsl:attribute>
  23  </xsl:element><xsl:text>
  24  
  25  </xsl:text>
  26  
  27  <xsl:element name="xsl:template">
  28        <xsl:attribute name="match">
  29          <xsl:text>html</xsl:text>
  30        </xsl:attribute><xsl:text>
  31  </xsl:text>
  32        <xsl:element name="{@element}">
  33        <xsl:apply-templates select="summary"/>
  34  
  35        <xsl:element name="xsl{$colon}for-each">
  36          <xsl:attribute name="select">
  37            <xsl:value-of select="record/@xpath"/>
  38          </xsl:attribute><xsl:text>
  39      </xsl:text>              
  40  
  41    <xsl:for-each select="record/field">
  42      <xsl:element name="xsl:variable">
  43        <xsl:attribute name="name">
  44          <xsl:value-of select="@element"/>
  45        </xsl:attribute>
  46        <xsl:attribute name="select">
  47          <xsl:value-of select="@xpath"/>
  48        </xsl:attribute>
  49      </xsl:element><xsl:text>
  50      </xsl:text>
  51    </xsl:for-each>
  52  <xsl:text>
  53      </xsl:text>
  54  
  55          <xsl:element name="{record/@element}">
  56         <xsl:for-each select="record/field">
  57                <xsl:element name="{@element}"><xsl:text>
  58          </xsl:text>
  59              <xsl:element name="xsl:value-of">
  60                <xsl:attribute name="select"><xsl:text>normalize-space($</xsl:text>
  61                  <xsl:value-of select="@element"/>
  62                  <xsl:text>)</xsl:text>                
  63                </xsl:attribute>
  64            </xsl:element>  <xsl:text>
  65        </xsl:text>
  66            </xsl:element>
  67  
  68      </xsl:for-each>
  69  </xsl:element><xsl:text>
  70    </xsl:text>
  71  </xsl:element><xsl:text>
  72  </xsl:text>
  73   
  74    </xsl:element>
  75  </xsl:element> <!-- template match -->
  76  </xsl:element> <!-- gallery -->
  77    </xsl:template> 
  78  
  79  
  80  <xsl:template match="summary/field"><xsl:text>
  81  </xsl:text>
  82        <xsl:element name="xsl:element">
  83          <xsl:attribute name="name">
  84            <xsl:value-of select="@element"/>
  85          </xsl:attribute><xsl:text>
  86  </xsl:text>
  87          <xsl:element name="xsl:value-of">
  88            <xsl:attribute name="select">
  89              <xsl:value-of select="@xpath"/>
  90            </xsl:attribute><xsl:text>
  91  </xsl:text>
  92          </xsl:element><xsl:text>
  93  </xsl:text>
  94        </xsl:element><xsl:text>
  95  </xsl:text>
  96  </xsl:template>
  97  </xsl:stylesheet>

output: gallery.xml (this file is the product of gallery_xhtml.xml and gallery.xsl)
   1  
   2  <?xml version="1.0"?>
   3  <gallery>
   4    <title>Journey to Windsor</title>
   5    <photo>
   6      <title>Windsor Castle</title>
   7      <date>July 2003</date>
   8      <image>dscn0824.jpg</image>
   9      <description>
  10        A bright, red mailbox inside the castle. It seems oddly familiar in an historic setting.
  11      </description>
  12    </photo>
  13  </gallery>

Formatting a date in XSLT

This template formats a date ie 060208171320 -> 06-Feb-08T17:13:20

   1  
   2    <xsl:template name="FormatDate">
   3      <xsl:param name="DateTime" />
   4      <!-- new date format 2006-01-14T08:55:22 -->
   5      <xsl:variable name="mo">
   6        <xsl:value-of select="substring($DateTime,3,2)" />
   7      </xsl:variable>
   8      <xsl:variable name="day">
   9        <xsl:value-of select="substring($DateTime,5,2)" />
  10      </xsl:variable>
  11      <xsl:variable name="year">
  12        <xsl:value-of select="substring($DateTime,1,2)" />
  13      </xsl:variable>
  14      <xsl:variable name="hh">
  15        <xsl:value-of select="substring($DateTime,7,2)" />
  16      </xsl:variable>
  17      <xsl:variable name="mm">
  18        <xsl:value-of select="substring($DateTime,9,2)" />
  19      </xsl:variable>
  20      <xsl:variable name="ss">
  21        <xsl:value-of select="substring($DateTime,11,2)" />
  22      </xsl:variable>
  23      <xsl:if test="(string-length($day) &lt; 2)">
  24        <xsl:value-of select="0"/>
  25      </xsl:if>
  26      <xsl:value-of select="$day"/>
  27      <xsl:value-of select="'-'"/>
  28      <xsl:choose>
  29        <xsl:when test="$mo = '01'">Jan</xsl:when>
  30        <xsl:when test="$mo = '02'">Feb</xsl:when>
  31        <xsl:when test="$mo = '03'">Mar</xsl:when>
  32        <xsl:when test="$mo = '04'">Apr</xsl:when>
  33        <xsl:when test="$mo = '05'">May</xsl:when>
  34        <xsl:when test="$mo = '06'">Jun</xsl:when>
  35        <xsl:when test="$mo = '07'">Jul</xsl:when>
  36        <xsl:when test="$mo = '08'">Aug</xsl:when>
  37        <xsl:when test="$mo = '09'">Sep</xsl:when>
  38        <xsl:when test="$mo = '10'">Oct</xsl:when>
  39        <xsl:when test="$mo = '11'">Nov</xsl:when>
  40        <xsl:when test="$mo = '12'">Dec</xsl:when>
  41      </xsl:choose>
  42      <xsl:value-of select="'-'"/>
  43  
  44      <xsl:value-of select="$year"/>
  45      <xsl:value-of select="'T'"/>
  46      <xsl:value-of select="$hh"/>
  47      <xsl:value-of select="':'"/>
  48      <xsl:value-of select="$mm"/>
  49      <xsl:value-of select="':'"/>
  50      <xsl:value-of select="$ss"/>
  51    </xsl:template>


copied from http://snipr.com/1z833 [geekswithblogs.net] and modified to suit the date input format I used.

Substring functions in XSLT

Examples of string functions (substring,substring-after, and substring-before) in XSLT.
   1  
   2  substring("ready1234",2,4)
   3  => eady 
   4  substring("ready1234", 4)
   5  => dy1234
   6  
   7  substring-after("ready1234","y")
   8  => 1234
   9  
  10  substring-before("ready1234","1")
  11  => ready


reference: X Lab: String functions [zvong.org]
« Newer Snippets
Older Snippets »
Showing 11-20 of 55 total