Layout of an XSL Document and Templates -: An extension Stylesheet Language (XSL ) consists of one or more then one set of rules that are called templates.A template contains rules to apply when a specified node is matched. Creating XSL files is similar to creating XML files. Let’s get started by taking a look at the text that is required in our XSL document . We take a example of Layout of an XSL Document and Templates
<?xml version="1.0" ?> <xsl : stylesheet version="1.0" xmlns :xsl="http://www.w3.org/1999/XSL/Transform"> <xsl : output method="html"/> </xsl :stylesheet>
In this <?xml version=”1.0″ ?> refers to the version of xml being used, and the standards that are being applied to the document. <xsl:st ylesheet version=”1.0″> indicates the version of the style sheet definition we are using .
In xmlns:xsl=”ht t p://www.w3.org/1999/XSL/Transform” declares the name space our XSL document is using.At last xsl:output method=”html”/ tells our translator that we are outputting HTML text as opposed to plain text./xsl:stylesheet is the style sheet’s closing tag. Make sure we include this closing tag when we are editing our files in CodeRunner. It’s easy to forget.
XSL : Output -: This tag isn’t actually required by all processors, but it’s a good idea to include it anyway. Even if our processor ignores the tag, a human who tries to read our XSL document will not .So what does this tag do? It tells the processor (and human readers) the “target” of our translation. There are three types of xsl:output targets.These are.
- <xsl:output method=”html”>
- <xsl:output method=”text”>
- <xsl:output method=”xml”>
Selecting between the XML and HTML tags may seem excessive at times since both XML and HTML are actually text files, but some extra magic may occur depending on which of these output types we select.For example if our XSL document is going to convert our XML phone book to an HTML file and we set our output type to “html,”,.*In this condition our processor may add some extra information to the HTML document to make it easier for web browsers to process. Our processor may also try to indent the resulting document if the output type is HTML.
If we are translating our document to XML. We will want to indicate our DOCTYPE or schema as well. If our DTD is external,we can add a docttype system attribute to our xsl:output element. To specify our external DTD for our XML output method. for example
< xsl:output method="xml" doctype-system="http://www.netnic.org/dtds/mydtd .dtd" / >
To declare a schema in our XML output method, we would do this
<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <xsl:template match="/"> <Friends2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Friends2.xsd"> </Friends2> </xsl:template> </xsl:stylesheet>
XSL: Template -: We know that the xtension stylesheet language consists of one or more then one set of rules that are called templates. For understanding the XSL template we take a example we’ll apply XSL to our existing XML data. Let’s work with our address book and get it to print out HELLO every time it hits an entry. Create an XSL document by typing the code below in CodeRunner as shown:
<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> Hello </xsl:template> </xsl:stylesheet>
Save this file in our /xml1 folder as AddressBook01.xsl; then, change Friends.xml as shown:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="AddressBook01.xsl" type="text/xsl"?>
<!DOCTYPE Amigos>
<Amigos
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Friends.xsd">
<Person>
<FirstName>Vikram</FirstName>
<LastName>Singh</LastName>
<Phone Type="Cell">000-1212</Phone>
<IM>Vikram90</IM>
</Person>
</Amigos>
Translate the document. we should see a “HELLO” on the screen. Once we get that example working try adding another Person to our XML document. Translate the document again and we see that the tag defines a set of instructions for the root element in our XML source file.
When the root element is matched against this template, HELLO is output. Only one HELLO is ever output, because there is only one root element.
Note -:The root element is the first element that occurs in an XML document. In this case, the Friends element is the root element
XSL: apply Template -: xsl:apply-templates is one of the most frequently used tags in extension stylesheet language. When translation software encounters a tag in our XML file that matches the tag defined by our template, it will “execute” the XSL instructions located within the xsl:template tags of our XSL document.
If we place our own XML tags or text inside the template, they will be copied to the resulting document. We define a set of instructions within xsl:template tags and they are executed for the tags at we have designated in our source XML document. This is known in the XML world as push-processing., Because our XML document is “pushed” through the instructions.
The XSLT processor “pushes” the source tree nodes through the stylesheet, which has template rules to handle various kinds of nodes as they come through.heet, which has template rules to handle various kinds of nodes as they come through. For example of XSL:apply Template
- <xsl:apply-templates select=”//*”/> -:Applies templates to all elements in the document.
- <xsl:apply-templates > -: Applies templates to all children of the current element.
- <xsl:apply-templates select=”Phone”/>-: Applies templates to Phone elements that are children of the current element.