[XML-XSLT] Style Sheets

XSL(Extensible Stylesheet Language) is a language for expressing stylesheets.  This article focuses on how the stylesheet is structured.

 

1. Embedded Sytlesheets

A stylesheet can be embedded in the document to be processed. An XML processing instruction (PI) “xml-stylesheet” is used for this purpose. It has two attributes:

  • type : “text/xsl”
  • href : “#idOfSheet”

<?xml-stylesheet type=”text/xsl” href=”#idOfSheet” ?>

<?xml-stylesheet type="text/xsl" href="#myStyle" ?>
<xsl:stylesheet xmlns:xsl="..." version="1.0" id="myStyle" >

 

2. Referenced Sytlesheets

An XML document can include a reference to a stylesheet. The reference is detected by the XSLT processor. An XML processing instruction (PI) “xml-stylesheet” is also used for this purpose.

  • type : “text/xsl”
  • href : “URL reference”

<?xml-stylesheet type=”text/xsl” href=”URL reference” ?>

<?xml-stylesheet type="text/xsl" href="http://www.mySite.com/myStyle.xsl" ?>

 

3. Runtime Sytlesheet Selection

You can pass the XML document and the stylesheet to the processor separately.

c:xsl> msxsl myDoc.xml myStyle.xsl -o Result.xml

 

4. Structure of XSLT Stylesheet

  • XML declaration
  • Document element: <stylesheet> or <transform>
  • <output> element
  • Templates

 

5. XML Declaration

Because an XSLT stylesheet is an XML document, it needs an XML declaration.

<?xml version="1.0" ?>

 

6. stylesheet Element

The document element of the stylesheet can be <stylesheet> element or <transform> element in the namespace “http://www.w3.org/1999/XSL/Transform&#8221;.

There is no difference between them, but stylesheet element is more popular. The stylesheet element needs to declare the XSLT namespace and its version.

In many cases, you need to specify 2 namespaces:

  • default namespace: target
  • xsl namespace

For example, if you want to convert an XML document to  XHTML document:

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

 

7. Controlling Output Types & Formats

The <xsl:output> element (top-level element) controls how the result tree is serialized on output. It has several attribute :

  • method : controls fundamental formating characteristics that are required for XML, HTML, and text output (“xml”, “html” or “text”)
  • media-type : the content type of the output (“text/xml”, “text/html”, or “text/plain”)
  • encoding : the character encoding (“UTF-8” – default, “ISO-8859-1” …)
  • indent : the processor add whitespace to the output to make it more readble (“yes, “no”)
  • cdata-section-element : lists elements whose text node child should be wrapped in CDATA sections
  • omit-xml-declaration : (“yes”, “no”)
  • standalone : the output XML declaration’s standalone pseudo-attribute value – (“yes”, “no”)
  • doctype-public , doctype-system : the public and system identifiers for the result, which are included in a DOCTYPE declaration.
<xsl:output method="xml" media-type="text/xml" />
<xsl:output indent="yes" cdata-section="script style" />

 

8. Defining Templates

Inside the <xsl:stylesheet> document element, XSLT stylesheet are made up of a number of templates. A template matches a particular part of the Source XML document and processes it.

<xsl:template match="book">
  ........
</xsl:template>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s