Download presentation
Presentation is loading. Please wait.
1
In this session, you will learn to:
Objectives In this session, you will learn to: Use the <xsl:include> element Use various XSLT functions Categorize different types of CSS style sheets Use the class attribute
2
The <xsl:include> Element
Enables you to treat style sheets as modular units Used to include another style sheet in the current style sheet Is the child element of the <xsl:stylesheet> element or the <xsl:transform> element The following code snippet illustrates the use of the <xsl:include> element: <xsl:stylesheet version “1.0” xmlns:xsl=” <xsl:include href=”other_stylesheet.xsl”/> </xsl:stylesheet>
3
XSLT provides more than 100 inbuilt functions
XSLT Functions XSLT provides more than 100 inbuilt functions The following table lists the various functions provided by XSLT: Method Name Description Example current() Used to return the reference of the current node. <xsl:value-of select="current()"/> format-number() Used to convert a number to a string. <xsl:value-of select='format-number(500100, "###,###.00")' /> System-property() Used to return the value of the system property specified by the parameter. <xsl:value-of select="system-property('xsl:version')" /> <br /> <xsl:value-of select="system-property('xsl:vendor')" />
4
XSLT Functions (Contd.)
The following table lists the various functions provided by XSLT (Contd.): Method Name Description Example Key() Used to return a node-set using the index specified by an <xsl:key> element. <xsl:key name="studentList" match="Student" use="Score" /> . <xsl:template match="/"> <xsl:for-each select="key('studentList', '100')"> <p>Title: <xsl:value-of select="Rollno"/> <br/> Artist: <xsl:value-of select="Name"/> <br/> Price: <xsl:value-of select="Score"/></p> </xsl:for-each></xsl:template>
5
XSLT Functions (Contd.)
The following table lists the various functions provided by XSLT (Contd.): Method Name Description Example Key() The preceding code displays the Roll Number, Name, and Score of all students with score 100, where the XML file has the following structure: <StudentDetails> <Student> <Rollno>1</Rollno> <Name> Tom </Name> <Score>100</Score> </Student> <Rollno>2</Rollno> <Name> Jack </Name> <Score>90</Score> . </StudentDetails>
6
Types of CSS Style Sheets
Can be used for formatting both HTML and XML documents Can be one of the following types: Inline: Can be used only for the HTML element in which it appears Cannot be reused for other elements, even within the same document Can only be used in HTML and not in XML The following example illustrates its use: <p style="color: red; margin-left: 10px"> This is a sample paragraph </p> Embedded: Is embedded within an XML document Cannot be used for other XML documents
7
Types of CSS Style Sheets (Contd.)
Embedded (Contd.): The following code uses an embedded CSS stylesheet: <?xml-stylesheet href="#style" type="text/css"?> <article> <extras id="style"> headline { display: block } headline { color: red } content { display: block } content { color: blue } extras { display: none } </extras> <headline>This is the headline of the article</headline> <content>This is the content of the article</content> </article>
8
Types of CSS Style Sheets (Contd.)
External: Is external to the XML document Formatting instructions are saved in an external file that can be imported in any number of XML documents The following code uses an external CSS stylesheet: <?xml-stylesheet href="my-style.css" type="text/css"?> <article> <headline>this is the headline of the article</headline> <content>this is the content of the article</content> </article> The my-style.css file contains the following text: headline { display: block } headline { color: red } content { display: block } content { color: blue }
9
Used to apply different CSS styles to the same type of elements
Class Attribute Used to apply different CSS styles to the same type of elements The following code snippet depicts the usage of the class attribute: <heading class="red">This heading is red.</heading> <heading class="green">This heading is green.</heading>
10
Best Practices External style sheets should be preferred over embedded style sheets because they provide better management of code and more control over rendering. External style sheets enable you to keep the formatting instructions separate from the content, thereby serving one of the key purposes of using style sheets. The following benefits are available when you use external style sheets: External style sheets are reusable. This eliminates the need for creating separate style sheets for documents that require the same formatting. External style sheets allow consistent rendering of XML documents. An external style sheet can be created and associated with XML documents to render any number of XML documents in a particular format. This is not easily achievable through inline or embedded style sheets.
11
Best Practices (Contd.)
Unlike inline and embedded style sheets, the formatting of XML documents can be easily updated by updating the corresponding external style sheet. External style sheets enable you to keep the XML documents light. As a result, the XML documents download faster than when used with inline or embedded style sheets.
12
Tips and Tricks You should ensure that the value of the class attribute in the XML document exactly matches the string that appears after the period in the corresponding class selector. Otherwise, the style rule will not apply to the element. You can either create a complex data type, or a group to define a group of elements that can be referred to at multiple places in an XML schema. Groups are preferred over complex data types because they are easier to implement than complex data types. Complex data types should be used when you need to specify attributes. This is because, groups allow you to specify only elements and not attributes.
13
Tips and Tricks (Contd.)
You should use an XML editor to create XML documents. An XML editor helps in creating an error free and well-formed XML document. It can also be used to validate the XML document against a schema. Some examples of XML editors are XMLSpy, Stylus Studio, and, oXygen.
14
How is an XML source document connected to an XSL style sheet?
FAQs How is an XML source document connected to an XSL style sheet? You can connect an XSLT style sheet with an XML source document by declaring a processing instruction in the XML document. The processing instruction needs to be placed before the root element and after the XML version declaration to connect an XML document to its style sheet. The following code snippet connects the sample.xsl file with an XML document: <?xml version="1.0"?> <?xml:stylesheet type="text/xsl" href="sample.xsl"?> <root> ... </root>
15
Can both CSS and XSLT be used together in an XML document?
FAQs (Contd.) Can both CSS and XSLT be used together in an XML document? Yes. You can use XSLT to transform XML data into structures, such as lists and tables, and then apply CSS to the result to control how these structures appear in the resulting Web page. Do all XML parsers support XSLT? No, all XML parsers do not support XSLT. For example, Apache Xerces, a Java-based parser does not support XSLT. If a user wants to perform XSL transformations by using Apache Xerces, the user needs to use the XALAN parser. XALAN is an XSLT parser for transforming XML documents into HTML, text, or other XML document types. Microsoft’s MSXML parser provides an edge in this regard, as it supports XSLT and does not require additional XSLT parsers for XSL transformations.
16
Why is the xsl:apply-templates element used?
FAQs (Contd.) Why is the xsl:apply-templates element used? The xsl:apply-templates element is used in templates-based formatting. This element directs the XSLT processor to find and apply the appropriate xsl:template.
17
Challenge Which of the following code snippets can be used in an XML document named Test.xml to associate it with a style sheet named Test.css? <?xml-stylesheet href="Test.css" type="text/css"?> <?xml-stylesheet href="Test.xsl" type="text/css"?> <?xmlstylesheet href="Test.css" type="text/xsl"?> <xml-stylesheet href="Test.css" type="text/css"/> Answer: <?xml-stylesheet href="Test.css" type="text/css"?>
18
Challenge (Contd.) A user is creating a schema for an XML document. The user wants to refer an external schema, which is stored in the product.xsd file. The target namespace of both the external schema and the current schema is the same. Which of following code snippets should the user use to refer to this schema? <include Location= "product.xsd"/> <include schemaLocation= "product.xsd"/> <import schemaLocation= "product.xsd"/> <import Location=" product.xsd"/> Answer: <include schemaLocation= "product.xsd"/>
19
Challenge (Contd.) A user is creating a schema for an XML document. In the schema, the user wants to include the declaration for the employee element and its three child elements: name, designation, and salary. The user wants that the child elements of employee may appear in any sequence in the XML document. Which of the following code snippets should the user use to declare the employee element? <xsd:complexType name="employee"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="designation" type="xsd:string"/> <xsd:element name="salary" type="xsd:string"/> </xsd:sequence> </xsd:complexType>
20
Challenge (Contd.) <xsd:complexType name="employee">
<xsd:group> <xsd:element name="name" type="xsd:string"/> <xsd:element name="designation" type="xsd:string"/> <xsd:element name="salary" type="xsd:string"/> </xsd:group> </xsd:complexType>
21
Challenge (Contd.) <xsd:complexType name="employee">
<xsd:choice> <xsd:element name="name" type="xsd:string"/> <xsd:element name="designation" type="xsd:string"/> <xsd:element name="salary" type="xsd:string"/> </xsd:choice> </xsd:complexType>
22
Challenge (Contd.) <xsd:complexType name="employee">
<xsd:all> <xsd:element name="name" type="xsd:string"/> <xsd:element name="designation" type="xsd:string"/> <xsd:element name="salary" type="xsd:string"/> </xsd:all> </xsd:complexType>
23
Challenge (Contd.) Answer: <xsd:complexType name="employee">
<xsd:group> <xsd:element name="name" type="xsd:string"/> <xsd:element name="designation" type="xsd:string"/> <xsd:element name="salary" type="xsd:string"/> </xsd:group> </xsd:complexType>
24
Challenge (Contd.) A user is creating a schema for an XML document. In the schema, the user wants to create a group of attributes that can be reused with different elements. Which of the following XSD elements should the user use for this purpose? group attribute attributeGroup choice Answer: attributeGroup
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.