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

drefty

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

MathOps: simple math operations in xslt

cfMathOps.xml:
   1  
   2  <?xml version="1.0" ?>
   3  <?xml-stylesheet type="text/xsl" href="cfMathOps.xsl" ?>
   4  <!--
   5  ### begin_: file metadata
   6      ### <region-file_info>
   7      ### main:
   8      ###   - name : cfMathOps.xml
   9      ###     desc : xml data file to use with cfMathOps.xsl
  10      ###     date : created="Thu 2005-12-01 11:57:38"
  11      ###     last : lastmod="Thu 2005-12-01 11:57:41"
  12      ###     lang    : xslt
  13      ###     tags    : xml xslt math cfMathOps
  14      ### </region-file_info>
  15      -->
  16  <numbers>
  17    <x>4</x>
  18    <y>3.2</y>
  19    <z>11</z>
  20  </numbers>


cfMathOps.xsl
   1  
   2  <xsl:stylesheet
   3      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   4  <!--
   5  ### begin_: file metadata
   6      ### <region-file_info>
   7      ### main:
   8      ###   - name : cfMathOps.xsl
   9      ###     desc : quick example of math operations using xslt
  10      ###     date : created="Thu 2005-12-01 11:57:38"
  11      ###     last : lastmod="Thu 2005-12-01 11:57:41"
  12      ###     lang    : xslt
  13      ###     tags    : xml xslt math hello cfMathOps
  14      ### </region-file_info>
  15      -->
  16  
  17  <xsl:output method="html" omit-xml-declaration="yes"/>
  18  
  19  <xsl:template match="numbers">
  20  <h2>math operations from xslt</h2>
  21      <br />  A. 4 + 3.2        = <xsl:value-of select="x + y"/>
  22      <br />  B. 3.2 - 4        = <xsl:value-of select="y - x"/>
  23      <br />  C. 4 * 3.2        = <xsl:value-of select="x * y"/>
  24      <br />  D. 11/3.2         = <xsl:value-of select="z div y"/>
  25      <br />  E. 4 + 3.2 * 11   = <xsl:value-of select="x+y*z"/>
  26      <br />  F. (4 + 3.2) * 11 = <xsl:value-of select="(x+y)*z"/>
  27      <br />  G. 11 mod 4       = <xsl:value-of select="z mod x"/>
  28      <br />  H. 4 + 3.2 + 11   = <xsl:value-of select="sum(*)"/>
  29      <br />  I. floor(3.2)     = <xsl:value-of select="floor(y)"/>
  30      <br />  J. ceiling(3.2)   = <xsl:value-of select="ceiling(y)"/>
  31      <br />  K. round(3.2)     = <xsl:value-of select="round(y)"/>
  32      <br />  L. 11 + count(*)  = <xsl:value-of select="11+count(*)"/>
  33      <br />  M. 11 + "hello"                 = <xsl:value-of select="z + 'hello'"/>
  34      <br />  N. 3.2 + string-length("3.2")   = <xsl:value-of select="y + string-length(y)"/>
  35  </xsl:template>
  36  
  37  </xsl:stylesheet>

ForLoop: a simple for loop in xslt

   1  
   2  <?xml version="1.0" encoding="ISO-8859-1"?>
   3  <!--
   4  ### begin_: file metadata
   5      ### <region-file_info>
   6      ### main:
   7      ###   - name : ForLoop: a simple for loop in xslt
   8      ###     desc : |
   9      ###         Do a simple for loop in xslt displaying hello world.
  10      ###         Call this from any source xml file.
  11      ###         It works independently of the data in the xml.
  12      ###     date : created="Thu 2005-12-01 11:30:52"
  13      ###     last : lastmod="Thu 2005-12-01 11:30:57"
  14      ###     lang    : xslt
  15      ###     tags    : xml xslt loop for hello_world
  16      ### </region-file_info>
  17      -->
  18  <xsl:stylesheet version="1.0"
  19      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  20      <xsl:output method="html"/>
  21  
  22  <xsl:template match="/">
  23  
  24  <html>
  25  <head><title>Say Hello Ten Times!</title></head>
  26  <body>
  27      <b>I am going to say hello Ten Times!</b>
  28  <!-- begin_: Send_Loop_To_HTML -->
  29      <xsl:call-template name="for.loop">
  30       <xsl:with-param name="i">1</xsl:with-param>
  31       <xsl:with-param name="count">10</xsl:with-param>
  32      </xsl:call-template>
  33  </body>
  34  </html>
  35  
  36  </xsl:template>
  37  
  38  
  39  <!--begin_: Define_The_Output_Loop -->
  40    <xsl:template name="for.loop">
  41  
  42     <xsl:param name="i"      />
  43     <xsl:param name="count"  />
  44  
  45     <!--begin_: Line_by_Line_Output -->
  46     <xsl:if test="$i &lt;= $count">
  47        <br /> <b><xsl:value-of select="$i" />.</b>Hello world!
  48     </xsl:if>
  49  
  50     <!--begin_: RepeatTheLoopUntilFinished-->
  51     <xsl:if test="$i &lt;= $count">
  52        <xsl:call-template name="for.loop">
  53            <xsl:with-param name="i">
  54                <xsl:value-of select="$i + 1"/>
  55            </xsl:with-param>
  56            <xsl:with-param name="count">
  57                <xsl:value-of select="$count"/>
  58            </xsl:with-param>
  59        </xsl:call-template>
  60     </xsl:if>
  61  
  62    </xsl:template>
  63  </xsl:stylesheet>

XMLPrettyPrint: simple xml pretty print in perl

   1  
   2  ### begin_: file metadata
   3      ### <region-file_info>
   4      ### main:
   5      ###   - name    : XMLPrettyPrint: simple xml pretty print in perl
   6      ###     desc    : use perl with XML::Twig library to print indented xml
   7      ###     date    : created="Thu 2005-12-01 11:08:15"
   8      ###     last    : lastmod="Thu 2005-12-01 11:22:34"
   9      ###     lang    : perl
  10      ###     tags    : perl xml indent formatted pretty string cfPrettyPrint
  11      ### </region-file_info>
  12  
  13  ### begin_: init perl
  14      use strict;
  15      use warnings;
  16      use XML::Twig;
  17  
  18  ### begin_: init vars
  19      my  $sXML  = join "", (<DATA>);
  20  
  21      ### init params
  22      my  $params = [qw(none nsgmls nice indented record record_c)];
  23      my  $sPrettyFormat  = $params->[3] || 'none';
  24  
  25  ### begin_: process
  26      my  $twig= new XML::Twig;
  27      $twig->set_indent(" "x4);
  28      $twig->parse( $sXML );
  29      $twig->set_pretty_print( $sPrettyFormat );
  30      $sXML      = $twig->sprint;
  31  
  32  ### begin_: output
  33      print $sXML;
  34  
  35  ### begin_: sample data
  36      1;
  37      __END__
  38  <table><tr age="35" >
  39  <fname>Homer</fname>
  40  <lname>Simpson</lname></tr>
  41  <tr age="33" >
  42  <fname>Barney</fname>
  43  <lname>Rubble</lname></tr>
  44  <tr age="29" >
  45  <fname>Betty</fname>
  46  <lname>Rubble</lname></tr></table>
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS