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-4 of 4 total  RSS 

Converting text to SVG

First of all the Ruby code reads the text, splits it into an array and then saves it in XML format.
require 'rexml/document'
include REXML

a = "Open source is a development method for software that harnesses the power of distributed peer 
review and transparency of process. The promise of open source is better quality, higher reliability, 
more flexibility, lower cost, and an end to predatory vendor lock-in.".split(' ') 

doc = Document.new
doc.add_element('text')
oline = Element.new('line')

char_count = 0
a.each do |word|
  
  oword = Element.new('word')
  oword.text = word.to_s
  oword.add_attribute('length',char_count)
  oline << oword 
  char_count += word.length
  
  if char_count > 50
    doc.root << oline
    oline = Element.new('line')
    char_count = 0
  end
end
doc.root << oline
puts char_count
puts doc

Here's a sample of the XML created
<text>
  <line>
    <word length='0'>Open</word><word length='4'>source</word><word length='10'>is</word>
    <word length='12'>a</word><word length='13'>development</word><word length='24'>method</word>
    <word length='30'>for</word><word length='33'>software</word><word length='41'>that</word>
    <word length='45'>harnesses</word>
  </line>
  <line>
    <word length='0'>the</word><word length='3'>power</word>
    <word length='8'>of</word><word length='10'>distributed</word><word length='21'>peer</word>
    <word length='25'>review</word><word length='31'>and</word><word length='34'>transparency</word>
    <word length='46'>of</word><word length='48'>process.</word>
  </line>
  <line>
    <word length='0'>The</word>
    <word length='3'>promise</word><word length='10'>of</word><word length='12'>open</word>
    <word length='16'>source</word><word length='22'>is</word><word length='24'>better</word>
    <word length='30'>quality,</word><word length='38'>higher</word><word length='44'>reliability,</word>
  </line>
  <line>
    <word length='0'>more</word><word length='4'>flexibility,</word><word length='16'>lower</word>
    <word length='21'>cost,</word><word length='26'>and</word><word length='29'>an</word>
    <word length='31'>end</word><word length='34'>to</word><word length='36'>predatory</word>
    <word length='45'>vendor</word>
  </line>
  <line>
    <word length='0'>lock-in.</word>
  </line>
</text>

The XML is then transformed to SVG using the following XSL file
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output encoding="UTF-8"
            method="xml"
            indent="yes"/>
            
  <xsl:template match="text">
    <svg xmlns="http://www.w3.org/2000/svg" width="100%"
                  xmlns:xlink="http://www.w3.org/1999/xlink" >
      <g id="sketch" class="sketch">
        <xsl:apply-templates select="line"/>
      </g>
    </svg>
  </xsl:template>
  
  <xsl:template match="line">
    <xsl:variable name="xfactor">12</xsl:variable>
    <xsl:variable name="yfactor">20</xsl:variable>
    <xsl:variable name="pos" select="position()"></xsl:variable>
    <xsl:apply-templates select="word">
      <xsl:with-param name="xfactor" select="$xfactor"></xsl:with-param>
      <xsl:with-param name="y" select="$pos * $yfactor + 10"></xsl:with-param>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="word">
    <xsl:param name="xfactor"/>
    <xsl:param name="y"/>
    <text xmlns="http://www.w3.org/2000/svg" font-size="12pt" x="{@length * $xfactor +10}" y="{$y}" id="t1"><xsl:value-of select="."/></text>
  </xsl:template>
</xsl:stylesheet>

The final SVG output [twitxr.com] shows 5 lines of text with each word as a separate SVG text element.

Passing an XSL param from one template to another

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="xx">
  <html>
  <body>
  <xsl:call-template name="show_title">
    <xsl:with-param name="title" />
  </xsl:call-template>
  </body>
  </html>
</xsl:variable>

<xsl:template name="show_title" match="/">
  <xsl:param name="title" />
  <xsl:for-each select="catalog/cd">
    <p>Title: <xsl:value-of select="$title" /></p>
  </xsl:for-each>
</xsl:template>

</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).
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output encoding="UTF-8"
            method="xml"
            indent="yes"/>
            
  <xsl:template match="mainpage">
  <svg xmlns="http://www.w3.org/2000/svg" width="100%"
                xmlns:xlink="http://www.w3.org/1999/xlink" >

    <g>
    <text font-size="12pt" x="50" y="50" id="t2" stroke="olive"><xsl:value-of select="title"/></text>
    </g>

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


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

Transforming an XML file into an XSL file

This XSL uses the XML for creating another XSL file which renders a web page containing records for a specific project, but I have found the projects I am working on use similar page layouts (ie. menu, body(articles), inputs, and buttons), so it makes sense to re-use a generic template.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="recordx">
    <xsl:variable name="colon"><xsl:text>:</xsl:text></xsl:variable>
    <xsl:element name="xsl:stylesheet">
      <xsl:attribute name="xmlns{$colon}xsl">
        <xsl:text>http://www.w3.org/1999/XSL/Transform</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="version">
        <xsl:text>1.0</xsl:text>
      </xsl:attribute>
      <xsl:element name="xsl:template">
      <xsl:attribute name="match">
        <xsl:value-of select="summary/project"/><xsl:text>page</xsl:text>
      </xsl:attribute>
      <xsl:copy-of select="menu/*"/>
      <xsl:copy-of select="articles/*"/>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match">
          <xsl:value-of select="summary/project"/><xsl:text>page/inputs</xsl:text>
      </xsl:attribute>
      <xsl:element name="div">
        <xsl:element name="dl"> 
          <xsl:for-each select="records/fields">
            <xsl:if test="c='true'">
              <xsl:element name="xsl:apply-templates">
                <xsl:attribute name="select"><xsl:copy-of select="field"/></xsl:attribute>
              </xsl:element>
            </xsl:if>
          </xsl:for-each>
        </xsl:element>
      </xsl:element>
      </xsl:element>      
      <xsl:call-template name="inputx" />      
    </xsl:element>
  </xsl:template>
  
  <xsl:template name="inputx">
    <xsl:apply-templates select="records/fields"/>
  </xsl:template>
  
  <xsl:template match="records/fields">
    <xsl:if test="c='true'">
    <xsl:element name="xsl:template">
      <xsl:attribute name="match"><xsl:value-of select="field"/></xsl:attribute>
        <dt>
          <xsl:element name="label">
            <xsl:attribute name="for"><xsl:text>title</xsl:text></xsl:attribute>
            <xsl:element name="xsl:value-of">
              <xsl:attribute name="select"><xsl:text>@title</xsl:text></xsl:attribute>
            </xsl:element>
          </xsl:element>
        </dt>
        <dd>
          <xsl:element name="input">
            <xsl:attribute name="type"><xsl:text>text</xsl:text></xsl:attribute>
            <xsl:attribute name="id"><xsl:text>title</xsl:text></xsl:attribute>
            <xsl:attribute name="size"><xsl:text>{@size}</xsl:text></xsl:attribute>            
          </xsl:element>
        </dd>
    </xsl:element>
    </xsl:if>
  </xsl:template>
  
</xsl:stylesheet>


... and here's the XML which is used by the XSL above.

<recordx>
<summary><project>books</project><parent_element>book_entry</parent_element></summary>
<menu>
  <div id="gboxes">
    <ul>
      <li><a href="/empsearch/devsearch.xml?id=all">home</a></li> 
      <li><a href="/main/development_blog.xml">development</a></li>
    </ul>
  </div>
</menu>
<articles>
  <div id="articles">
    <h1>www<xsl:value-of select="@title" /></h1>
    <div>
      <xsl:apply-templates select="inputs"></xsl:apply-templates>
    </div>
    <div>
      <xsl:apply-templates select="buttons"></xsl:apply-templates>
    </div>
    <span id="in1"></span>
  </div> 
</articles>
<records>
<fields id='17647'><field>author</field><c>true</c><r>true</r><u>true</u></fields>
<fields id='17675'><field>subject</field><c>true</c><r>true</r><u>false</u></fields>
<fields id='17699'><field>rating</field><c/><r/><u/></fields>
<fields id='17700'><field>last_modified</field><c/><r/><u/></fields>
</records>
</recordx>


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