xslt and xml Transformation

WHAT IS XSLT :

Before Lurning the XSLT, XML and its Transformation we should aware about the base of the things.

What is a Markup Language?

When we format any type of Text for Printing or Writing, or displayed on Screen of computer or TV , we need to distnigh between the text itself and the instruction for printing the text, markup is the instruction for the text.

We can say that markup indicate the information about text,like color, font family, their size etc.

HTML
Hypertext markup language is the language of the Web. All Web pages are written in HTML. HTML defines the way that images, multimedia, and text are displayed in Web browsers. It includes tags to connect your documents (hypertext) and make your Web documents interactive (forms).

HTML is a defined standard markup language. That standard was developed by the World Wide Web Consortium (W3C). It is based upon SGML (Standard Generalized Markup Language). It is a language that uses tags to define the structure and some style of your text. Tags are defined by the < and > characters.

But HTML is no longer the most current standard for Web development. As HTML was developed it got more and more complicated and the style and content tags combined into one language. Eventually, the W3C decided that there was a need for a separation between the style of a Web page and the content. A tag that defines the content alone, such as <h1>, would remain in HTML while, tags that define style, such as <font>, are deprecated in the latest version of HTML in favor of style sheets.

XML
The eXtensible Markup Language is the language that the new version of HTML is based on. XML is also based off of SGML. It is less strict than SGML, but provides the extensibility to create various different languages as they relate to your business. XML is a language for writing markup languages. For example, if you are working on genealogy, you might create tags using XML to define the <father>, <mother>, <daughter>, and <son> in your documents. There are also several standardized languages already created with XML: MathML for defining mathematics, SMIL for working with multimedia, and XHTML.

XHTML
XHTML is HTML 4.0 redefined to meet the XML standard. There aren’t a lot of major differences between HTML and XHTML:

  • XHTML is written in lower case. While HTML tags can be written in UPPER case, MiXeD case, or lower case, to be correct, XHTML tags must be all lower case.
  • All XHTML tags must have an end tag. Tags with only one tag, such as <hr> and <img> need a closing slash (/) at the end of the tag: <hr />
  • All attributes must be quoted in XHTML. Many editors remove the quotes around attributes, but they are required for correct XHTML.
  • XHTML requires that tags are nested correctly. If you open a bold (<b>) tag and then an italics (<i>) tag, you must close the italics tag (</i>) before you close the bold (</b>).
  • XHTML attributes must have a name and a value. Attributes that are stand-alone in HTML must be declared with values as well, for example, the <hr> attribute noshade would be written noshade=”noshade”.

XSLT – Transformation

How to transform XML into XHTML using XSLT.

How to declare [XSL] Style Sheet

The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.

Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used!

The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:

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

or:

<xsl:transform version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>

To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.

The xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version=”1.0″.

Lets take one simple xml document to understand the actual things.

We want to transform the following XML document (“kkpractice.xml”) into XHTML:

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>

Viewing XML Files in Firefox and Internet Explorer: Open the XML file (typically by clicking on a link) – The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and – signs), select “View Page Source” or “View Source” from the browser menu.

Viewing XML Files in Netscape 6: Open the XML file, then right-click in XML file and select “View Page Source”. The XML document will then be displayed with color-coded root and child elements.

Viewing XML Files in Opera 7: Open the XML file, then right-click in XML file and select “Frame” / “View Source”. The XML document will be displayed as plain text.

Let’s Create an XSL Style Sheet:-

Then we can create an XSL Style Sheet (“kkpractice.xsl”) with a transformation template:

<?xml version=”1.0″ encoding=”ISO-8859-1″?>

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

<xsl:template match=”/”>
<html>
<body>
<h2>My CD Collection</h2>
<table border=”1″>
<tr bgcolor=”#9acd32″>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select=”catalog/cd”>
<tr>
<td><xsl:value-of select=”title”/></td>
<td><xsl:value-of select=”artist”/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

How to link XSL Style Sheet to the XML Document:

To associate the xsl with any xml document we need to add the xsl style sheet reference in our xml document. Please see in kkpractice.xml. below.

The following highlighted line we need to add in our xml document to associated with xsl.

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<?xml-stylesheet href=”kkpractice.xsl”?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>

And both the .xml and .xsl file should be same location. And we also need the xslt complaint browser for it.

If you have an XSLT compliant browser it will nicely transform your XML into XHTML

About Krishana Kumar

Krishana Kumar is SharePoint Architect/Trainer having Architecture experience with high volumes at Enterprise level and global scale - creation of highly scalable solutions with global user base and geographically distributed architectural components. Good knowledge of SharePoint best practices and governance models. I hold Two Master degree in Computer Science with over 11 years of experience working on Microsoft Technologies specially SharePoint, Project, .NET and other Information Worker Technologies. Having good exposer in Client side scripting Angular.js, backbone and Node. I am currently responsible for SharePoint Infrastructure set up and leading teams in various medium and large scale projects, architecting, designing & installing SharePoint farms, developing custom components,, and providing advanced SharePoint administration and development training to teams and customers. I regularly speaks in various SharePoint User Groups and other Events. I have MCSA Windows Azure, MCSA Office 365, MCSE & MCSD SharePoint 2013, Microsoft Certified Developer (MCD) and holds MCPD, MCTIP and MCTS for SharePoint 2010, MCTS MOSS 2007 & WSS 3.0, MCPD, MCITP (EPM 2010 & 2007) and MCSD .NET.
This entry was posted in General Interest. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *