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 

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