XSLT or XSL Transformations is a W3C Specification that defines a language based on XML syntax to transform XML documents. It has evolved from early XSL (Extensible Stylesheet Language) standard, and later XSLT was proposed and accepted as a separate standard for XML data transformation. The original XSL standard is now known as XSL-FO (Formatting Objects).
Check out 50 XSLT Tips and XPath/XSLT Quiz.
Tip: Check out MSXSL.EXE Command Line Transformation Utility. The download contains the complete source code. Click here to learn more about this utility.
Tip: Be sure to read the article XSLT in MSXML by Stuart Celarier.
Tip: Got a XSLT question? Check XSLT FAQ.
XSLT offers three programming models: Exemplars, Procedural, & Declarative. XSLT is really a very powerful language and can be used for various purposes, as simple as transforming XML document from one format to the other, and as complex as code-generation, Web Services response generation, etc.
Tip: Try some XSLT tools (including XSLT Samples Viewer, MSXSL.EXE Command Line Transformation Utility, Internet Explorer Tools for Validating XML and Viewing XSLT Output, and XSL Debugger).
Tip: Try out our Online XSL(T) Tester.
MSXML supports XSLT 1.0.
Tip: Chapter 7 (Styling XML Content) and Chapter 10 (Transformations on the Server) in the book XML Application Development with MSXML 4.0 covers MSXML XSLT features in great details.
- MSXML 3.0 XSLT Documentation
- MSXML 3.0 XSLT Reference
- MSXML 4.0 XSLT Documentation
- MSXML 4.0 XSLT Reference
Tip: MSXML 4.0 completely removes support for old XSL namespace (http://www.w3.org/TR/WD-xsl).
Let's look at how to apply XSLT stylesheet using MSXML 4.0:
First, let's look at source XML & XSL documents.
XML Document (cust.xml) |
XSLT Stylesheet (cust.xsl) |
<?xml version="1.0"?>
<customers>
<row CustomerID="ALFKI" CompanyName="Alfreds Futterkiste"/>
</customers>
|
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="/">
<customers>
<xsl:for-each select="/customers/row">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</customers>
</xsl:template>
</xsl:stylesheet>
|
VBScript (ASP Page)
<%
Option Explicit
Dim objXML, objXSL
Set objXML = Server.CreateObject("Msxml2.DOMDocument.4.0")
Set objXSL = Server.CreateObject("Msxml2.DOMDocument.4.0")
objXML.async = False
objXSL.async = False
'If you are loading remote XML document, remember to call
'objXML.setProperty "ServerHTTPRequest", true
'See http://support.microsoft.com/default.aspx?scid=kb;EN-US;q281142 for details
objXML.load Server.MapPath("cust.xml")
'If you are loading remote XSL document, remember to call
'objXSL.setProperty "ServerHTTPRequest", true
objXSL.load Server.MapPath("cust.xsl")
Response.Write objXML.transformNode (objXSL)
'Also try:
'objXML.transformNodeToObject objXSL, Response
%>
The above code uses the "Attributes-to-Elements" stylesheet (cust.xsl) and sends the following transformed output to the client:
<?xml version="1.0"?>
<customers>
<row>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
</row>
</customers>
Check out XML Developer's Reference Guide for an example of passing parameters to XSLT stylesheet.
MSXML XSLT Resources Around the Web
|