This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XSLT
The XSLT Example: Using different axes
The XSLT Example: Creating a summary of author sales for a publisher
The XSLT Example: Creating a summary of all Apollo flights
The XSLT Example:
The XSLT Element: xsl:with-param
The XSLT Element: xsl:when
The XSLT Element: xsl:variable
The XSLT Element: xsl:value-of
The XSLT Element: xsl:transform
The XSLT Element: xsl:text
The XSLT Element: xsl:template
The XSLT Element: xsl:stylesheet
The XSLT Element: xsl:strip-space
The XSLT Element: xsl:sort
The XSLT Element: xsl:processing-instruction
The XSLT Element: xsl:preserve-space
The XSLT Element: xsl:param
The XSLT Element: xsl:output
The XSLT Element: xsl:otherwise
The XSLT Element: xsl:number
<< XQuery
.NET and XML >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :2386

 
[XSLT Reference] [XPath Reference]

Example: Generating a new stylesheet

As XSLT stylesheets are formulated in XML and the result of an XSLT transformation can be any XML document, wouldn't it be interesting to generate stylesheets on the fly using another XSLT stylesheet? But how would you do that? How do you output an xsl:template element without causing syntax errors in the first stylesheet? After all, a template cannot hold another template.
This is where the slightly obscure element xsl:namespace-alias comes to the rescue. It allows us to use elements of a namespace A in our stylesheet, but just before the result tree is written to the result document, all nodes with namespace a are changed to another namespace (B).
When you try to generate a stylesheet, you would just create elements using a dummy namespace (dummy:template). Then, using xsl:namespace-alias, we wouls specify the in the output, this dummy namespace should be converted to the XSLT namespace (http://www.w3.org/1999/XSL/Transform).
In this example, we generate a stylesheet that can transform a piece of code like <inputfield type="date"><day>22</day><month>3</month><year>1971</year></inputfield> to something like where the names of the months come from the source document of the first transformation. This allows you to keep even some of the content bits that appear inside the stylesheet in a separate XML document, merging them as you need them. In this case, we used the spanish content, but we may have many.
XSLT elements used:xsl:stylesheet xsl:template xsl:value-of xsl:namespace-alias xsl:attribute xsl:for-each
XML source:<?xml version="1.0"?> <months language="es"> <month number="1">enero</month> <month number="2">febrero</month> <month number="3">marcho</month> <month number="4">avril</month> <month number="5">mayo</month> <month number="6">junio</month> <month number="7">julio</month> <month number="8">agosto</month> <month number="9">setembre</month> <month number="10">octubre</month> <month number="11">noviembre</month> <month number="12">diciembre</month> </months>
XSLT code:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:outputxsl="dummy-uri"> <xsl:namespace-alias stylesheet-prefix="outputxsl" result-prefix="xsl"/> <xsl:template match="/" xml:space="preserve"> <outputxsl:stylesheet version="1.0"> <outputxsl:template match="inputfield[@type='date']"> <input size="2" name="day"> <outputxsl:attribute name="value"> <outputxsl:value-of select="day/text()"/> </outputxsl:attribute> </input> <select name="month"> <xsl:for-each select="//months/month"> <option> <outputxsl:attribute name="value"> <xsl:value-of select="@number"/> </outputxsl:attribute> <outputxsl:if xml:space="default"> <xsl:attribute name="test">month/text() = <xsl:value-of select="@number"/></xsl:attribute> <outputxsl:attribute name="SELECTED"/> </outputxsl:if> <xsl:value-of select="text()"/> </option> </xsl:for-each> </select> <input size="4" name="year"> <outputxsl:attribute name="value"> <outputxsl:value-of select="year/text()"/> </outputxsl:attribute> </input> </outputxsl:template> </outputxsl:stylesheet> </xsl:template> </xsl:stylesheet>
Resulting XML output:
saxon
xt
xalan
msxml3
<?xml version="1.0" encoding="utf-8"?> <outputxsl:stylesheet xmlns:outputxsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <outputxsl:template match="inputfield[@type='date']"> <input size="2" name="day"> <outputxsl:attribute name="value"> <outputxsl:value-of select="day/text()"/> </outputxsl:attribute> </input> <select name="month"> <option> <outputxsl:attribute name="value"> 1 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 1"><outputxsl:attribute name="SELECTED"/></outputxsl:if> enero </option> <option> <outputxsl:attribute name="value"> 2 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 2"><outputxsl:attribute name="SELECTED"/></outputxsl:if> febrero </option> <option> <outputxsl:attribute name="value"> 3 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 3"><outputxsl:attribute name="SELECTED"/></outputxsl:if> marcho </option> <option> <outputxsl:attribute name="value"> 4 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 4"><outputxsl:attribute name="SELECTED"/></outputxsl:if> avril </option> <option> <outputxsl:attribute name="value"> 5 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 5"><outputxsl:attribute name="SELECTED"/></outputxsl:if> mayo </option> <option> <outputxsl:attribute name="value"> 6 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 6"><outputxsl:attribute name="SELECTED"/></outputxsl:if> junio </option> <option> <outputxsl:attribute name="value"> 7 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 7"><outputxsl:attribute name="SELECTED"/></outputxsl:if> julio </option> <option> <outputxsl:attribute name="value"> 8 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 8"><outputxsl:attribute name="SELECTED"/></outputxsl:if> agosto </option> <option> <outputxsl:attribute name="value"> 9 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 9"><outputxsl:attribute name="SELECTED"/></outputxsl:if> setembre </option> <option> <outputxsl:attribute name="value"> 10 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 10"><outputxsl:attribute name="SELECTED"/></outputxsl:if> octubre </option> <option> <outputxsl:attribute name="value"> 11 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 11"><outputxsl:attribute name="SELECTED"/></outputxsl:if> noviembre </option> <option> <outputxsl:attribute name="value"> 12 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 12"><outputxsl:attribute name="SELECTED"/></outputxsl:if> diciembre </option> </select> <input size="4" name="year"> <outputxsl:attribute name="value"> <outputxsl:value-of select="year/text()"/> </outputxsl:attribute> </input> </outputxsl:template> </outputxsl:stylesheet> <?xml version="1.0" encoding="utf-8"?> <outputxsl:stylesheet version="1.0" xmlns:outputxsl="http://www.w3.org/1999/XSL/Transform"> <outputxsl:template match="inputfield[@type='date']"> <input size="2" name="day"> <outputxsl:attribute name="value"> <outputxsl:value-of select="day/text()"/> </outputxsl:attribute> </input> <select name="month"> <option> <outputxsl:attribute name="value"> 1 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 1"><outputxsl:attribute name="SELECTED"/></outputxsl:if> enero </option> <option> <outputxsl:attribute name="value"> 2 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 2"><outputxsl:attribute name="SELECTED"/></outputxsl:if> febrero </option> <option> <outputxsl:attribute name="value"> 3 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 3"><outputxsl:attribute name="SELECTED"/></outputxsl:if> marcho </option> <option> <outputxsl:attribute name="value"> 4 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 4"><outputxsl:attribute name="SELECTED"/></outputxsl:if> avril </option> <option> <outputxsl:attribute name="value"> 5 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 5"><outputxsl:attribute name="SELECTED"/></outputxsl:if> mayo </option> <option> <outputxsl:attribute name="value"> 6 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 6"><outputxsl:attribute name="SELECTED"/></outputxsl:if> junio </option> <option> <outputxsl:attribute name="value"> 7 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 7"><outputxsl:attribute name="SELECTED"/></outputxsl:if> julio </option> <option> <outputxsl:attribute name="value"> 8 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 8"><outputxsl:attribute name="SELECTED"/></outputxsl:if> agosto </option> <option> <outputxsl:attribute name="value"> 9 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 9"><outputxsl:attribute name="SELECTED"/></outputxsl:if> setembre </option> <option> <outputxsl:attribute name="value"> 10 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 10"><outputxsl:attribute name="SELECTED"/></outputxsl:if> octubre </option> <option> <outputxsl:attribute name="value"> 11 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 11"><outputxsl:attribute name="SELECTED"/></outputxsl:if> noviembre </option> <option> <outputxsl:attribute name="value"> 12 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 12"><outputxsl:attribute name="SELECTED"/></outputxsl:if> diciembre </option> </select> <input size="4" name="year"> <outputxsl:attribute name="value"> <outputxsl:value-of select="year/text()"/> </outputxsl:attribute> </input> </outputxsl:template> </outputxsl:stylesheet> <?xml version="1.0" encoding="UTF-8"?> <outputxsl:stylesheet xmlns:outputxsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <outputxsl:template match="inputfield[@type='date']"> <input size="2" name="day"> <outputxsl:attribute name="value"> <outputxsl:value-of select="day/text()"/> </outputxsl:attribute> </input> <select name="month"> <option> <outputxsl:attribute name="value"> 1 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 1"><outputxsl:attribute name="SELECTED"/></outputxsl:if> enero </option> <option> <outputxsl:attribute name="value"> 2 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 2"><outputxsl:attribute name="SELECTED"/></outputxsl:if> febrero </option> <option> <outputxsl:attribute name="value"> 3 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 3"><outputxsl:attribute name="SELECTED"/></outputxsl:if> marcho </option> <option> <outputxsl:attribute name="value"> 4 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 4"><outputxsl:attribute name="SELECTED"/></outputxsl:if> avril </option> <option> <outputxsl:attribute name="value"> 5 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 5"><outputxsl:attribute name="SELECTED"/></outputxsl:if> mayo </option> <option> <outputxsl:attribute name="value"> 6 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 6"><outputxsl:attribute name="SELECTED"/></outputxsl:if> junio </option> <option> <outputxsl:attribute name="value"> 7 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 7"><outputxsl:attribute name="SELECTED"/></outputxsl:if> julio </option> <option> <outputxsl:attribute name="value"> 8 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 8"><outputxsl:attribute name="SELECTED"/></outputxsl:if> agosto </option> <option> <outputxsl:attribute name="value"> 9 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 9"><outputxsl:attribute name="SELECTED"/></outputxsl:if> setembre </option> <option> <outputxsl:attribute name="value"> 10 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 10"><outputxsl:attribute name="SELECTED"/></outputxsl:if> octubre </option> <option> <outputxsl:attribute name="value"> 11 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 11"><outputxsl:attribute name="SELECTED"/></outputxsl:if> noviembre </option> <option> <outputxsl:attribute name="value"> 12 </outputxsl:attribute> <outputxsl:if xml:space="default" test="month/text() = 12"><outputxsl:attribute name="SELECTED"/></outputxsl:if> diciembre </option> </select> <input size="4" name="year"> <outputxsl:attribute name="value"> <outputxsl:value-of select="year/text()"/> </outputxsl:attribute> </input> </outputxsl:template> </outputxsl:stylesheet> <?xml version="1.0" encoding="UTF-16"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="inputfield[@type='date']"> <input size="2" name="day"> <xsl:attribute name="value"> <xsl:value-of select="day/text()" /> </xsl:attribute> </input> <select name="month"> <option> <xsl:attribute name="value"> 1 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 1"><xsl:attribute name="SELECTED" /></xsl:if> enero </option> <option> <xsl:attribute name="value"> 2 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 2"><xsl:attribute name="SELECTED" /></xsl:if> febrero </option> <option> <xsl:attribute name="value"> 3 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 3"><xsl:attribute name="SELECTED" /></xsl:if> marcho </option> <option> <xsl:attribute name="value"> 4 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 4"><xsl:attribute name="SELECTED" /></xsl:if> avril </option> <option> <xsl:attribute name="value"> 5 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 5"><xsl:attribute name="SELECTED" /></xsl:if> mayo </option> <option> <xsl:attribute name="value"> 6 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 6"><xsl:attribute name="SELECTED" /></xsl:if> junio </option> <option> <xsl:attribute name="value"> 7 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 7"><xsl:attribute name="SELECTED" /></xsl:if> julio </option> <option> <xsl:attribute name="value"> 8 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 8"><xsl:attribute name="SELECTED" /></xsl:if> agosto </option> <option> <xsl:attribute name="value"> 9 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 9"><xsl:attribute name="SELECTED" /></xsl:if> setembre </option> <option> <xsl:attribute name="value"> 10 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 10"><xsl:attribute name="SELECTED" /></xsl:if> octubre </option> <option> <xsl:attribute name="value"> 11 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 11"><xsl:attribute name="SELECTED" /></xsl:if> noviembre </option> <option> <xsl:attribute name="value"> 12 </xsl:attribute> <xsl:if xml:space="default" test="month/text() = 12"><xsl:attribute name="SELECTED" /></xsl:if> diciembre </option> </select> <input size="4" name="year"> <xsl:attribute name="value"> <xsl:value-of select="year/text()" /> </xsl:attribute> </input> </xsl:template> </xsl:stylesheet>

 

Note: You can now download the full content of the XPath reference, the XSLT reference and the DOM reference in one PDF document. This document contains the implementation tables, internal links to navigate through the references, a full linked table of contents. You can use free text search through the whole reference or print as a nicely formatted document. Download here...

 

© 2000 Teun Duynstee. Shown on TopXML.com. Information used from XSLT and XPath recommendations © W3C and MSDN documentation © Microsoft.
Do you have comments, additions or suggestions, mail me.

Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP