| Introduction to Schema |
|
| Evolution of XML |
|
| What is XML? |
|
| The eXtensible Markup Language (XML) is a general-purpose markup language. Its primary purpose is to facilitate the sharing of data across different information systems, particularly via the Internet. |
|
| It is a simplified subset of the Standard Generalized Markup Language (SGML), and is designed to be relatively human-legible. By adding semantic constraints, application languages can be implemented in XML. These include XHTML, RSS,MathML, GraphML, Scalable Vector Graphics(SVG), MusicXML, and thousands of others. Moreover, XML is sometimes used as the specification language for such application languages. |
|
| XML is nothing by itself. XML is more of a "common ground" standard. The main benefit of XML is that you can take data from a program like MSSQL(Microsoft SQL), convert it into XML, then share that XML with a slough of other programs, platforms, etc. Each of these receiving platforms can then convert the XML into a structure the platform uses normally and presto! you have just communicated between two potentially very different platforms! |
|
| History of Markup languages…... |
|
| A markup language combines text and extra information about the text. The extra information, for example about the text's structure or presentation, is expressed using markup, which is intermingled with the primary text. |
|
| The best-known markup language in modern use is HTML (HyperText Markup Language), one of the foundations of the World Wide Web. Originally markup was used in the publishing industry in the communication of printed work between authors, editors, and printers. |
|
| GenCode |
|
| The idea of "markup languages" was apparently first presented in 1967, by William W. Tunnicliffe at a conference. Tunnicliffe would later lead the development of a standard called GenCode for the publishing industry. |
|
| Book designer Stanley Fish also published speculation along similar lines in the late1960s. Brian Reid, in his 1980 dissertation at Carnegie Mellon University, developed the theory and a working implementation of descriptive markup in actual use. |
|
| However, IBM researcher Charles Goldfarb is more commonly seen today as the "father" of markup languages, because of his work on IBM GML, and then as chair of the International Organization for Standardization committee that developed SGML, the first widely used descriptive markup system. |
|
| SGML |
|
| Standard Generalized Markup Language |
|
| The first language to make a clear and clean distinction between structure and presentation was certainly Scribe, which was developed by Brian Reid and described in his doctoral thesis in 1980. |
|
| Scribe was revolutionary in a number of ways, not least that it introduced the idea of styles separated from the marked up document, and of a grammar controlling the usage of descriptive elements. Scribe influenced the development of Generalized Markup Language (GML)(later SGML) and is a direct ancestor to HTML and LaTeX. |
|
| SGML specified a syntax for including the markup in documents, as well as one for separately describing what tags were allowed, and where (the Document Type Definition (DTD) or schema). |
|
| This allowed authors to create and use any markup they wished, selecting tags that made the most sense to them and were named in their own natural languages. Thus, SGML is properly a meta-language, and many particular markup languages are derived from it. |
|
| From the late 80s on, most substantial new markup languages have been based on SGML system, including for example TEI and DocBook. SGML was promulgated as an International Standard by International Organization for Standardization, ISO 8879, in 1986. |
|
| The Rise of XML |
|
| The flexibility of SGML for lively information display was understood by early digital media publishers in the late 1980s prior to the rise of the Internet By the mid-1990s some practitioners of SGML had gained experience with the then-new WWW, and believed that SGML offered solutions to some of the problems the Web was likely to face as it grew. |
|
| Dan Connolly added SGML to the list of W3C's activities when he joined the staff in 1995; work began in mid-1996 when Jon Bosak developed a charter and recruited collaborators. Bosak was well connected in the small community of people who had experience both in SGML and the Web. He received support in his efforts from Microsoft. |
|
| XML was compiled by a working group of eleven members, supported by an (approximately) 150-member Interest Group. Technical debate took place on the Interest Group mailing list and issues were resolved by consensus or, when that failed, majority vote of the Working Group. |
|
| The decision record was compiled by Michael Sperberg-McQueen on December 4th 1997. James Clark served as Technical Lead of the Working Group, notably contributing the empty-element " " syntax and the name "XML". |
|
| Other names that had been put forward for consideration included "MAGMA" (Minimal Architecture for Generalized Markup Applications), "SLIM" (Structured Language for Internet Markup) and "MGML" (Minimal Generalized Markup Language). |
|
| The co-editors of the specification were originally Tim Bray and Michael Sperberg-McQueen. Halfway through the project Bray accepted a consulting engagement with Netscape, provoking vociferous protests from Microsoft. Bray was temporarily asked to resign the editorship. This led to intense dispute in the Working Group, eventually solved by the appointment of Microsoft's Jean Paoli as a third co-editor. |
|
| The XML Working Group never met face-to-face; the design was accomplished using a combination of email and weekly teleconferences. The major design decisions were reached in twenty weeks of intense work between July and November of 1996, when the first Working Draft of an XML specification was published. Further design work continued through 1997, and XML 1.0 became a W3C Recommendation on February 10, 1998. |
|
| XML 1.0 achieved the Working Group's goals of Internet usability, general-purpose usability, SGML compatibility, facilitation of easy development of processing software, minimization of optional features, legibility, formality, conciseness, and ease of authoring. Like its antecedent SGML, XML allows for some redundant syntactic constructs and includes repetition of element identifiers. In these respects, terseness was not considered essential in its structure. |
|
|
|
|
|
| Introduction to XSL |
|
| Overview of XSL Transformations |
|
| In an XSL transformation, an XSLT processor reads both an XML document and anXSLT style sheet. Based on the instructions the processor finds in the XSLT style sheet, it outputs a new XML document or fragment thereof. |
|
| There's also special support for outputting HTML. With some effort most XSLTprocessors can also be made to output essentially arbitrary text, though XSLT is designed primarily for XML-to-XML and XML-to-HTML transformations. |
|
| Trees |
|
| Every well-formed XML document is a tree. A tree is a data structure composed of connected nodes beginning with a top node called the root. The root is connected to its child nodes, each of which is connected to zero or more children of its own, and so forth. Nodes that have no children of their own are called leaves. |
|
| A diagram of a tree looks much like a genealogical descendant chart that lists the descendants of a single ancestor. The most useful property of a tree is that each node and its children also form a tree. Thus, a tree is a hierarchical structure of trees in which each tree is built out of smaller trees. |
|
| For the purposes of XSLT, elements, attributes, namespaces, processing instructions, and comments are counted as nodes. Furthermore, the root of the document must be distinguished from the root element. |
|
| Thus, XSLT processors model an XML document as a tree that contains seven kinds of nodes: |
|
• The root
• Elements
• Text
• Attributes
• Namespaces
• Processing instructions
• Comments |
|
| The Document Type Definition (DTD) and document type declaration are specifically not included in this tree. However, a DTD may add default attribute values to some elements, which then become additional attribute nodes in the tree. |
|
<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="sample.xsl"?>
<periodic_table>
<atom state="gas">
<name>hydrogen</name>
<symbol>h</symbol>
<atomic_number>1</atomic_number>
<atomic_weight>1.00794</atomic_weight>
<boiling_point units="kelvin">20.28</boiling_point>
<melting_point units="kelvin">13.81</melting_point>
<density units="grams/cubic centimeter">
<!-- at 300k, 1 atm -->
0.0000899
</density>
</atom>
<atom state="gas">
<name>helium</name>
<symbol>he</symbol>
<atomic_number>2</atomic_number>
<atomic_weight>4.0026</atomic_weight>
<boiling_point units="kelvin">4.216</boiling_point>
<melting_point units="kelvin">0.95</melting_point>
<density units="grams/cubic centimeter"><!-- at 300k -->
0.0001785
</density>
</atom>
</periodic_table>
|
|
| Click here to view the XML file |
|
| Figure below displays a tree diagram of this document. It begins at the top with the root node (not the same as the root element!) which contains two child nodes, the xml-stylesheet processing instruction and the root element PERIODIC_TABLE. |
|
| (The XML declaration is not visible to the XSLT processor and is not included in the tree the XSLT processor operates on.) |
|
| The PERIODIC_TABLE element contains two child nodes, both ATOM elements. EachATOM element has an attribute node for its STATE attribute, and a variety of child element nodes. Each child element contains a node for its contents, as well as nodes for any attributes, comments and processing instructions it possesses. |
|
| Notice in particular that many nodes are something other than elements. There are nodes for text, attributes, comments, namespaces and processing instructions. UnlikeCSS, XSL is not limited to working only with whole elements. It has a much more granular view of a document that enables you to base styles on comments, attributes, processing instructions, element content, and more. |
|
 |
|
| Listing as a tree diagram |
|
| XSLT operates by transforming one XML tree into another XML tree. More precisely, anXSLT processor accepts as input a tree represented as an XML document and produces as output a new tree, also represented as an XML document. Consequently, the transformation part of XSL is also called the tree construction part. |
|
| The XSL transformation language contains operators for selecting nodes from the tree, reordering the nodes, and outputting nodes. If one of these nodes is an element node, then it may be an entire tree itself. Remember that all these operators, both for input and output, are designed for operation on a tree. |
|
| The input must be an XML document. You cannot use XSLT to transform from non-XML formats such as PDF, TeX, Microsoft Word, PostScript, MIDI, or others. HTML andSGML are borderline cases because they're so close to XML. XSLT can work with HTMLand SGML documents that satisfy XML's well-formedness rules. |
|
| However, XSLT cannot handle the wide variety of non-well-formed HTML and SGMLthat you encounter on most Web sites and document production systems. XSLT is not a general-purpose regular expression language for transforming arbitrary data. |
|
| Most of the time the output of an XSLT transformation is also an XML document. However, it can also be a result tree fragment that could be used as an external parsed entity in another XML document. |
|
| (That is, it would be a well-formed XML document if it were enclosed in a single root element.) In other words, the output may not necessarily be a well-formed XMLdocument, but it will at least be a plausible part of a well-formed XML document. AnXSLT transformation cannot output text that is malformed XML such as. |
|
| <B><I>Tag Mismatch! </B></I> |
|
| Most XSLT processors also support output as HTML and/or raw text, although the standard does not require them to do so. To some extent this allows you to transform to non-XML formats like TeX, RTF, or PostScript. However XSLT is not designed to make these transformations easy. It is designed for XML-to-XML transformations. |
|
| XSLT style sheet documents |
|
| An XSLT document contains template rules. A template rule has a pattern specifying the nodes it matches and a template to be instantiated and output when the pattern is matched. When an XSLT processor transforms an XML document using an XSL style sheet, it walks the XML document tree, looking at each node in turn. |
|
| As each node in the XML document is read, the processor compares it with the pattern of each template rule in the style sheet. When the processor finds a node that matches a template rule's pattern, it outputs the rule's template. This template generally includes some markup, some new data, and some data copied out of the source XML document. |
|
| XSLT uses XML to describe these rules, templates, and patterns. The root element of the XSLT document is either a stylesheet or a transform element in the http://www.w3.org/1999/XSL/Transform namespace. By convention this namespace is mapped to the xsl prefix, but you're free to pick another prefix if you prefer. |
|
| Each template rule is an xsl:template element. The pattern of the rule is placed in the match attribute of the xsl:template element. The output template is the content of the xsl:template element. |
|
| All instructions in the template for doing things such as selecting parts of the input tree to include in the output tree are performed by one or another XSLT elements. These are identified by the xsl: prefix on the element names. Elements that do not have an xsl: prefix are part of the result tree. |
|
| Given below is a very simple XSLT style sheet with two template rules. The first template rule matches the root element PERIODIC_TABLE. It replaces this element with an html element. The contents of the html element are the results of applying the other templates in the document to the contents of the PERIODIC_TABLE element. |
|
| The second template matches ATOM elements. It replaces each ATOM element in the input document with a P element in the output document. The xsl:apply-templates rule inserts the text of the matched source element into the output document. Thus, the contents of a P element will be the text (but not the markup) contained in the corresponding ATOM element. |
|
| The xsl:stylesheet root element has two required attributes, version and xmlns:xsl, each of which must have exactly the values shown here (1.0 for version and http://www.w3.org/1999/XSL/Transform for xmlns:xsl). |
|
| An XSLT style sheet for the periodic table with two template rules |
|
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="PERIODIC_TABLE">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="ATOM">
<P>
<xsl:apply-templates/>
</P>
</xsl:template>
</xsl:stylesheet>
|
|
| The xsl:transform element can be used in place of xsl:stylesheet if you prefer. This is an exact synonym with the same syntax, semantics, and attributes. For example, |
|
<?xml version="1.0"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- templates go here -->
</xsl:transform>
|
|
|
|
|
| Introduction to XSL |
|
| XSL output methods |
|
| Options for displaying XML |
|
 |
|
<xsl:output
cdata-section-elements="namelist"
doctype-public="string"
doctype-system="string"
encoding="string"
indent="yes" | "no"
media-type="mimetype"
method="html" | "name" | "text" | "xml"
omit-xml-declaration="yes" | "no"
standalone="yes" | "no"
version="version_number"
/>
|
|
| The xsl:output element is used to define the format of the output created by the stylesheet. This is accomplished by setting one or more of ten optional attributes. The most important of these ten attributes is the method attribute which dictates if the type of output is HTML, text, or XML. The type of output, in turn, dictates which of the other nine attributes can be applied to the output. |
|
| The following table defines which attributes can optionally be set for each of the three types of output. A dash signifies that the attribute cannot effect the output. |
|
| Attribute | HTML | Text | XML |
| cdata-section-elements | - | - | YES |
| doctype-public | YES | - | YES |
| doctype-system | YES | - | YES |
| encoding | YES | YES | YES |
| indent | YES | - | YES |
| media-type | YES | YES | YES |
| omit-xml-declaration | - | - | YES |
| standalone | - | - | YES |
| version | YES | - | YES |
|
|
| You can have zero or more xsl:output elements: |
|
• If there is more than one xsl:output element, the XSLT processor essentially combines the information.
• If more than one xsl:output element sets the same attribute, the element with the highest import precedence will have its attribute selected.
• If more than one xsl:output element repeats an attribute and has the same highest import precedence, either the last will be chosen or an error will be declared.
• If there is more than one cdata-section-elements attribute, all of the values in the name lists will effectively be merged into one list. |
|
The xsl:output element can only be a child of the xsl:stylesheet or the xsl:transform elements.
This is a self-closing element and it cannot contain any child elements or any content. |
|
| cdata-section-elements="namelist" |
|
| The optional cdata-section-elements attribute is set to a white-space delimited list of qnames (element names) whose content is to be output in CDATA sections. CDATAsections permit the use of sequences of characters that contain markup elements without violating the XML requirement to be well-formed (i.e., all tags and elements are closed). |
|
| doctype-public="string" |
|
| The optional doctype-public attribute specifies the public identifiers that go in the document type declaration (DTD). If the doctype-system attribute is not set, then the doctype-public attribute is ignored. |
|
| doctype-system="string" |
|
| The optional doctype-system attribute specifies the system identifiers that go in the document type declaration (DTD). The DTD should go immediately after the XMLdeclaration. |
|
| encoding="string" |
|
| The optional encoding attribute specifies the preferred character encoding which is used to encode sequences of characters as sequences of bytes. |
|
| indent="yes" | "no" |
|
| The optional indent attribute specifies whether or not to indent. If set to yes, the XMLand HTML outputs are step-indented to make them more readable. |
|
| media-type="mimetype" |
|
| The optional media-type attribute sets the MIME type. The default is: media-type="text/xml" |
|
| method="html" | "qname" | "text" | "xml" |
|
| The optional method attribute dictates the type of output. The three permitted values are HTML, text and XML. (Some XSLT processors recognize a qname as being an acceptable value for this attribute, but it is not part of the W3C standard.) The default isXML. However, if the first child element of the root node is the HTML <html> tag and there are no preceding text nodes, then the default output type is set to HTML. |
|
| omit-xml-declaration="yes" | "no" |
|
| The optional omit-xml-declaration attribute dictates whether the XSLT processor should output an XML declaration. The default is no and an XML declaration is output. If yes, there is no output. |
|
| standalone="yes" | "no" |
|
| The optional standalone attribute dictates whether the XSLT processor should output a standalone declaration. Yes signifies that it will. No, the default, signifies that it will not. |
|
| version="version_number" |
|
| The optional version attribute provides the W3C version number for the output format. If the output is XML, the default version is 1.0 (currently, the only X3C version). Or if the output type is HTML, the default version is 4.0. |
|
| We use the eBIZ.com Employee List XML file for our example with the following header: |
|
| <?xml-stylesheet type="text/xsl" href="xsl_displaying_output.xsl"?> |
|
and we name it:
eBIZ_xsl_output_html.xml and given below is its source code: |
|
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl"
| href="xsl_displaying_output.xsl"?>
<ebiz>
<employee_details>
<emp_id>eBIZTECH001</emp_id>
<fname>Mr Aman Kumar</fname>
<lname>Singh</lname>
<department>TECHNICAL[Java]</department>
<designation>Sr. Developer</designation>
<phone> 32942</phone>
<address>Sector 44, Noida</address>
<age>27</age>
</employee_details>
<employee_details>
<emp_id>eBIZCSS001</emp_id>
<fname>Mrs Sunita</fname>
<lname>Singhania</lname>
<department>Custome
r Support[Web Assist]</department>
<designation>CCE</designation>
<phone> 000000</phone>
<address>Sector 66, Gurgaon</address>
<age>20</age>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>Miss Amisha</fname>
<lname>Mishra</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone> 32942</phone>
<address>Sector 66, Rohni</address>
<age>26</age>
</employee_details>
<employee_details>
<emp_id>eBIZAC002</emp_id>
<fname>ANITA</fname>
<lname>Mishra</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone> 09999 </phone>
<address>Sector 66, Noida</address>
<age>23</age>
</employee_details>
<employee_details>
<emp_id>eBIZAC003</emp_id>
<fname>ANKITA</fname>
<lname>DUBEY</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone>032942 </phone>
<address>Sector 99, Noida</address>
<age>21</age>
</employee_details>
<employee_details>
<emp_id>eBIZAC004</emp_id>
<fname>AKSHITA</fname>
<lname>Jaiswal </lname>
<department>TECHNICAL[Java]</department>
<designation>Jr. DEVELOPER</designation>
<phone> (+91)99990</phone>
<address>Sector 66, Noida</address>
<age>22</age>
</employee_details>
<employee_details>
<emp_id>eBIZTECH002</emp_id>
<fname>Shweta</fname>
<lname>Agrwal</lname>
<department>TECHNICAL[Java]</department>
<designation>Jr. Programmer</designation>
<phone> 32942</phone>
<address>Sector 66, Noida</address>
<age>24</age>
</employee_details>
<employee_details>
<emp_id>eBIZTECH003</emp_id>
<fname>Shalu</fname>
<lname>Jain</lname>
<department>TECHNICAL[Java]</department>
<designation>Jr. Programmer</designation>
<phone> 32942</phone>
<address>Sector 66, Noida</address>
<age>23</age>
</employee_details>
</ebiz>
|
|
| Click here to view the Output |
|
| Click here to view the Output |
|
| Click here to view the Output |
|
| In this example, we declare our output to be in HTML. (By default, the occurrence of the <html> tag in the code signifies to the Microsoft XSLT processor we are using that the output is to be in HTML.) |
|
| Source code for xsl_displaying_output.xsl: |
|
<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Sample XSL Example [Output]</title>
<style type="text/css">
<xsl:comment>
.style3 {font-family: Geneva, Arial, Helvetica, sans-serif; color: #0033FF; }
</xsl:comment>
</style>
</head>
<body>
<table width="59%" id="data">
<tr bgcolor="#6699FF">
<td width="22%" bgcolor="#6699FF"><strong>EMP ID </strong></td>
<td width="30%" bgcolor="#6699FF"><strong>Name</strong></td>
<td width="20%" bgcolor="#6699FF"><strong>Age</strong></td>
<td width="20%" bgcolor="#6699FF"><strong>Department</strong></td>
<td width="19%" bgcolor="#6699FF"><strong>Designation</strong></td>
<td width="19%" nowrap="nowrap
" bgcolor="#6699FF"><strong>Phone No </strong></td>
<td width="19%" nowrap="nowrap
" bgcolor="#6699FF"><strong>Address </strong></td>
</tr>
<xsl:for-each select="ebiz/employee_details">
<tr>
<td nowrap="nowrap"><span class="style3">
<xsl:value-of select="emp_id" /></span></td>
<td nowrap="nowrap"><span class="style3"
><xsl:value-of select="fname"> </xsl:value-of>
<xsl:value-of select="lname"></xsl:value-of></span></td>
<td nowrap="nowrap" class="style3"><xsl:value-of select="age" /></td>
<td nowrap="nowrap"><xsl:value-of select="department" /></td>
<td nowrap="nowrap"><xsl:value-of select="designation" /></td>
<td nowrap="nowrap">
<xsl:value-of select="phone" /></td >
<td nowrap="nowrap"><xsl:value-of select="address"/></td>
</tr></xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
|
|
|
|
| Introduction to XSL |
|
| What's with Stylesheets in the first place? |
|
| We can even use CSS to format XML, but as we know CSS is designed to format HTMLnot XML (meta languages) and XML is not like HTML. XML is related with data whereasHTML is used for presentation of the information on the internet. |
|
| How XML is different from HTML: |
|
XML is not a fixed tag set (like HTML)
XML by itself has no (application) semantics
A generic XML processor has no idea what is "meant" by the XML
XML markup does not (usually) include formatting information
The information in an XML document may not be in the form in which it is desired to present it
Therefore there must be something in addition to the XML document that provides information on how to present or otherwise process the XML |
|
| Advantages to separating content from style |
|
| Contrary to when style information is hard-coded into the content, separation of style from content allows for the same data to be presented in different ways. |
|
| This enables: |
|
| • Reuse of fragments of data: the same content should look different in different contexts |
|
| • Multiple output formats: different media (paper, online), different sizes (manuals, reports), different classes of output devices (workstations, hand-held devices) |
|
| • Styles tailored to the reader's preference (e.g., accessibility): print size, color, simplified layout for audio readers |
|
| • Standardized styles: corporate stylesheets can be applied to the content at any time |
|
| • Freedom from style issues for content authors: technical writers needn't be concerned with layout issues because the correct style can be applied later |
|
|
|
|
| Introduction to XSL |
|
| What Does a Stylesheet Do? |
|
| A stylesheet specifies the presentation of XML information using two basic categories of techniques: |
|
| • An optional transformation of the input document into another structure |
|
| • A description of how to present the transformed information (i.e., a specification of what properties to associate to each of the various parts of the transformed information) |
|
| As we know, XML does not use predefined tags (we can use any tag-names we like), and the meaning of these tags are not well understood. |
|
| A <table> element could mean an HTML table, a piece of furniture, or something else - and a browser does not know how to display it. |
|
| XSL describes how the XML document should be displayed! |
|
| XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. |
|
| An XSLT stylesheet is an XML document, complete with elements and attributes. It has two kinds of elements, top-level and instruction. Top-level elements fall directly under the stylesheet root element. |
|
| Instruction elements represent a set of formatting instructions that dictate how the contents of an XML document will be transformed. During the transformation process,XSLT analyzes the XML document, or the source tree, and converts it into a node tree, a hierarchical representation of the entire XML document, also known as the result tree. |
|
| Each node represents a piece of the XML document, such as an element, attribute or some text content. The XSL stylesheet contains predefined “templates” that contain instructions on what to do with the nodes. XSLT will use the match attribute to relateXML element nodes to the templates, and transform them into the result document. |
|
|
|
|
| Introduction to XSL |
|
| Transformation capabilities |
|
| Transformation capabilities include: |
|
• generation of constant text • suppression of content
• moving text (e.g., exchanging the order of the first and last name)
• duplicating text (e.g., copying titles to make a table of contents)
• sorting
• more complex transformations that "compute" new information in terms of the existing information |
|
| Description of information |
|
| Description of how to present the (possibly transformed) data includes three levels of formatting information: |
|
• Specification of the general screen or page (or even audio) layout
• Assignment of the transformed content into basic "content container types" (e.g., lists, paragraphs, inline text)
• Specification of formatting properties (spacing, margins, alignment, fonts, etc.) for each resulting "container" |
|
| XSLT can be used to transform an XML source into many different types of documents.XHTML is also XML, if it is well formed, so it could also be used as the source or the result. |
|
| However, transforming plain HTML into XML won't work unless it is first turned intoXHTML so that it conforms to the XML 1.0 recommendation. Here is a list of all the possible type-to-type transformations performed by XSLT: |
|
| Type-To-Type Transformations |
|
|
XML
|
XHTML
|
HTML
|
text
|
XML
| X | X | X | X |
XHTML
| X | | X | X |
|
|
|
|
|
| Introduction to XSL |
|
| The components of the XSL |
|
| The full XSL language logically consists of three component languages which are described in three W3C (World Wide Web Consortium) Recommendations: |
|
XPath:
XML Path Language--a language for referencing specific parts of an XML document |
|
XSLT:
XSL Transformations--a language for describing how to transform one XML document (represented as a tree) into another |
|
XSL:
Extensible Stylesheet Language--XSLT plus a description of a set of Formatting Objects and Formatting Properties |
|
| What is XSLT? |
|
• XSLT stands for XSL Transformations
• XSLT is the most important part of XSL
• XSLT transforms an XML document into another XML document
• XSLT uses XPath to navigate in XML documents
• XSLT is a W3C Recommendation |
|
| XSLT = XSL Transformations |
|
| XSLT is the most important part of XSL. XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XMLelement into an (X)HTML element. |
|
| With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more. |
|
|
|
|
| Getting Started With XSL |
|
| XSL vs. HTML Formatting Objects |
|
| XSL is a W3C Recommendation XSL or CSS is essential for XML presentation and XSLfar more powerful and complicated than CSS. |
|
| XSL permits : |
|
• element reordering
• selection of source elements
• text generation
• processing source elements multiple times
• Transformation is independent of the target result type |
|
• Most people are more familiar with HTML so many of the examples in this tutorial useHTML
• The XSL implementation in IE5 is incomplete. The examples in this tutorial will not work in IE5
• The techniques apply equally well to XSL Formatting Objects or other tag sets
• XSLT is a tree-to-tree transformation process
• Serialization may vary depending on the selected output method |
|
• There is a distinction between HTML element names and HTML
• XSL uses XML syntax, i.e., XSL is an XML vocabulary
• it uses the namespace http://www.w3.org/1999/XSL/Transform, usually with prefix xsl: |
|
• rather than rules of CSS like
• CD { ... } • composer { ... }
XSL uses template rules like <xsl:template match="CD">
... </xsl:template> <xsl:template match="composer"> ... </xsl:template> |
|
• unlike CSS, XSL rules do not just apply styles to elements
• XSL is a language for transforming an input document to an output document |
|
• given an XSL rule like
<xsl:template match="composer"> ... </xsl:template> |
|
• the value of the match attribute (e.g., composer) is a pattern for matching part of the input document
• the contents of the template element (e.g., ...) is a sequence of instructions for constructing part of the output document |
|
|
|
|
| Getting Started With XSL |
|
| The Structure of a Stylesheet |
|
| • XSLT Stylesheets are XML documents; namespaces (http://www.w3.org/TR/REC-xml-names) are used to identify semantically significant elements. |
|
| • Most stylesheets are stand-alone documents rooted at <xsl:stylesheet> or <xsl:transform>. It is possible to have "single template" stylesheet/documents. |
|
|
| • <xsl:stylesheet> and <xsl:transform> are completely synonymous. |
|
| Note that it is the mapping from namespace abbreviation to URI that is important, not the literal namespace abbreviation "xsl:" that is used most commonly. |
|
| A Stylesheet |
|
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
...
</xsl:stylesheet> |
|
| A Transformation Sheet |
|
<eg:transform xmlns:eg="http://www.w3.org/1999/XSL/Transform" version="1.0">
...
</eg:transform> |
|
| Document as Stylesheet |
|
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<head>
<title>Silly Example</title>
</head>
<body>
<h1>Silly Example</h1>
<p>You'd probably use extension elements, or somthing more interesting in real life: 3+4 is <xsl:value-of select="3+4"/>. </p>
</body>
</html> |
|
| XML document for city entity |
|
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document: city.xml
-->
<cities xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='host.xsd'>
<city>
<cityID>c1</cityID>
<cityName>Atlanta</cityName>
<cityCountry>USA</cityCountry>
<cityPop>4000000</cityPop>
<cityHostYr>1996</cityHostYr>
</city>
..........
</cities>
|
|
| XML stylesheet for city entity |
|
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document: city2.xsl
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:attribute-set name="date">
<xsl:attribute name="year">2004</xsl:attribute>
<xsl:attribute name="month">03</xsl:attribute>
<xsl:attribute name="day">19</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="tourGuide">
<xsl:processing-instruction name="xsl-stylesheet">
href="style.css" type="text/css"<br />
</xsl:processing-instruction>
<xsl:comment>This is a list of the cities
we are visiting this week</xsl:comment>
<xsl:for-each select="city">
<!-- element name creates a new element where the
value of the attribute <tt>name</tt> sets name of
the new element. Multiple attribute sets can
be used in the same element -->
<!-- use-attribute-sets attribute adds all the
attributes declared in attribute-set from above -->
<xsl:element name="cityList" use-attribute-sets="date">
<xsl:element name="city">
<xsl:attribute name="country">
<xsl:apply-templates select="country"/> </xsl:attribute>
<xsl:apply-templates select="cityName"/>
</xsl:element>
<xsl:element name="details">Will write up a
one page report of the trip</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
|
|
| • Although the output method is set to "xml", since there is no <html> element as the root of the result tree, it would default to XML output. |
|
| • attribute-set is a top-level element that creates a group of attributes by the name of "date." This attribute set can be reused throughout the stylesheet. The element attribute-set also has the attribute use-attribute-sets allowing you to chain together several sets of attributes. |
|
| • The processing-instructionproduces the XML stylesheet processing instructions. |
|
| • The element comment creates a comment in the result tree |
|
| • The attribute element allows you to add an attribute to an element that is created in the result tree. |
|
|
|
|
| Getting Started with XSL |
|
| A Complete Example |
|
| This is a simple stylesheet that transforms source eBIZ_com_staff XML file to generate eye-catching output: |
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE xsl:stylesheet[
<!ENTITY nbsp " ">
]>
<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" />
<!-- XSL Variable Declaration-->
<xsl:variable name="tbl">
<tr>
<td><b>Name</b></td>
<td><b>Address</b></td>
<td><b>Department</b></td>
<td><b>Designation</b></td>
<td><b>Age</b></td>
</tr>
</xsl:variable>
<xsl:attribute-set name="tblAttrib">
<xsl:attribute name="border">
2
</xsl:attribute>
<xsl:attribute name="cellspacing">
3
</xsl:attribute>
<xsl:attribute name="bordercolor">
"#3344dd"
</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="/">
<ebizml>
<head>
<title>XSLT Example</title>
</head>
<body>
<span style="width 600px;border:
2px outset blue solidl;display:block">
</span><br />
<table xsl:use-attribute-sets="tblAttrib">
<xsl:copy-of select="$tbl" />
<xsl:for-each select="ebiz/employee_details">
<xsl:sort data-type="number" order="descending" select="age" />
<tr>
<td><xsl:value-of select="fname" /
> <xsl:value-of select="lname" /></td>
<td><xsl:value-of select="address" /></td>
<xsl:choose>
<xsl:when test="department-sub ='Java'">
<td style="background-color:pink"> <xsl:value-of se
lect="department" /><xsl:text>[</xsl:text>
<xsl:value-of select="department-sub" />
<xsl:text>]</xsl:text></td>
</xsl:when>
<xsl:when test="department ='TECHNICAL'">
<td style="background-color:#FFCC99
;color:white;font-weight:bold">
<xsl:value-of select="department" />
<xsl:text>[</xsl:text
><xsl:value-of select="department-sub" />
<xsl:text>]</xsl:text></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="department" /> </td>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="department='TECHNICAL'">
<td style="background-color:#FFCC99
;color:white;font-weight:bold">
<xsl:value-of select="designation" />
</td>
</xsl:when>
<xsl:otherwise>
<td style="font-weight:bold">
<xsl:value-of select="designation" />
</td>
</xsl:otherwise>
</xsl:choose>
<td>
<xsl:value-of select="age" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</ebizml>
</xsl:template>
</xsl:stylesheet>
|
|
| With this stylesheet, the following XML document: |
|
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl"
href="xsl_complete_example.xsl"?>
<ebiz>
<employee_details>
<emp_id>eBIZTECH001</emp_id>
<fname>Mr Aman Kumar</fname>
<lname>Singh</lname>
<department>TECHNICAL</department>
<department-sub>Java</department-sub>
<designation>Sr. Developer</designation>
<phone> 32942</phone>
<address>Sector 44, Noida</address>
<age>27</age>
</employee_details>
<employee_details>
<emp_id>eBIZTECH003</emp_id>
<fname>Shalu</fname>
<lname>Jain</lname>
<department>TECHNICAL</department>
<department-sub>Java</department-sub>
<designation>Jr. Programmer</designation>
<phone> 32942</phone>
<address>Sector 66, Noida</address>
<age>23</age>
</employee_details>
<employee_details>
<emp_id>eBIZTECH004</emp_id>
<fname>Mr Ravish</fname>
<lname>Tiwari</lname>
<department>TECHNICAL</department>
<department-sub>Java</department-sub>
<designation>Sr. Developer</designation>
<phone>(+91)99990</phone>
<address>Sector 56, Rohni</address>
<age>23</age>
</employee_details>
<employee_details>
<emp_id>eBIZCSS001</emp_id>
<fname>Mrs Sunita</fname>
<lname>Singhania</lname>
<department>Custome
r Support[Web Assist]</department>
<department-sub>NA</department-sub>
<designation>CCE</designation>
<phone> 000000</phone>
<address>Sector 66, Gurgaon</address>
<age>20</age>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>Miss Amisha</fname>
<lname>Mishra</lname>
<department>Accounts</department>
<department-sub>NA</department-sub>
<designation>Jr. Accountant</designation>
<phone> 32942</phone>
<address>Sector 66, Rohni</address>
<age>26</age>
</employee_details>
<employee_details>
<emp_id>eBIZAC002</emp_id>
<fname>ANITA</fname>
<lname>Mishra</lname>
<department>Accounts</department>
<department-sub>NA</department-sub>
<designation>Jr. Accountant</designation>
<phone> 09999 </phone>
<address>Sector 66, Noida</address>
<age>23</age>
</employee_details>
<employee_details>
<emp_id>eBIZAC003</emp_id>
<fname>ANKITA</fname>
<lname>DUBEY</lname>
<department>Accounts</department>
<department-sub>NA</department-sub>
<designation>Jr. Accountant</designation>
<phone>032942 </phone>
<address>Sector 99, Noida</address>
<age>21</age>
</employee_details>
<employee_details>
<emp_id>eBIZAC004</emp_id>
<fname>AKSHITA</fname>
<lname>Jaiswal </lname>
<department>TECHNICAL</department>
<department-sub>Java</department-sub>
<designation>Jr. DEVELOPER</designation>
<phone> (+91)99990</phone>
<address>Sector 66, Noida</address>
<age>22</age>
</employee_details>
<employee_details>
<emp_id>eBIZTECH002</emp_id>
<fname>Shweta</fname>
<lname>Agrwal</lname>
<department>TECHNICAL</department>
<department-sub>Java</department-sub>
<designation>Jr. Programmer</designation>
<phone> 32942</phone>
<address>Sector 66, Noida</address>
<age>24</age>
</employee_details>
<employee_details>
<emp_id>eBIZTECH003</emp_id>
<fname>Shalu</fname>
<lname>Jain</lname>
<department>TECHNICAL</department>
<department-sub>Java</department-sub>
<designation>Jr. Programmer</designation>
<phone> 32942</phone>
<address>Sector 66, Noida</address>
<age>23</age>
</employee_details><employee_details>
<emp_id>eBIZAC004</emp_id>
<fname>ANAMIKA</fname>
<lname>SINGH</lname>
<department>Accounts</department>
<department-sub></department-sub>
<designation>Jr. Accountant</designation>
<phone>032942 </phone>
<address>Sector 99, Noida</address>
<age>21</age>
</employee_details>
<employee_details>
<emp_id>eBIZTECH004</emp_id>
<fname>Nilofar</fname>
<lname>Khan</lname>
<department>TECHNICAL</department>
<department-sub></department-sub>
<designation>Jr. Programmer</designation>
<phone> 32942</phone>
<address>Sector 66, Noida</address>
<age>23</age>
</employee_details>
</ebiz>
|
|
Click the respective links to view the Formatted XML file or Source XML file or XSL file |
|
| Understanding A Template |
|
| Most templates have the following form: |
|
| <xsl:template match="/"> </xsl:template> |
|
• The whole <xsl:template> element is a template
• The match pattern determines where this template applies
• Literal result element(s) come from non-XSL namespace(s)
• XSLT elements come from the XSL namespace |
|
| One critical capability of a stylesheet language is to locate source elements to be styled. CSS, for example, does this with "selectors." FOSIs do it with "e-i-c's", elements in context. XSLT does it with "match patterns" defined by the XML Path Language (XPath). |
|
XPath has an extensible string-based syntax
• It describes "location paths" between parts of a document or documents
• One inspiration for XPath was the common "path/file" file system syntax Two things to remember about XPath expressions:
• Pattern matching occurs in a context; XPath expressions and XSLT elements can change the current context and consequently the nodes which match
• XPath is inclusive or greedy, it addresses all matching elements. You must use predicates to refine the set of nodes selected. |
|
|
|
|
| XSL Match Pattern |
|
| Match Patterns (Locating Elements) |
|
| One critical capability of a stylesheet language is to locate source elements to be styled.CSS, for example, does this with "selectors." FOSIs do it with "e-i-c's", elements in context. XSLT does it with "match patterns" defined by the XML Path Language (XPath). |
|
| XPath has an extensible string-based syntax. It describes "location paths" between parts of a document or documents One inspiration for XPath was the common "path/file" file system syntax |
|
| Two things to remember about XPath expressions: |
|
| Pattern matching occurs in a context; XPath expressions and XSLT elements can change the current context and consequently the nodes which match XPath is inclusive or greedy, it addresses all matching elements. You must use predicates to refine the set of nodes selected. |
|
| Everything you've read so far has been fairly straightforward, except for one thing: match patterns, which have been a little mysterious. We've used various match patterns, such as "/PLANETS" in <xsl:template> elements, without offering a lot of systematic explanation on how these patterns really work, as in this case: |
|
<xsl:template match="/PLANETS">
<HTML>
<HEAD>
<TITLE> The Planets Table </TITLE>
</HEAD>
<BODY>
.
.
.
</BODY>
</HTML>
</xsl:template> |
|
| You can also use match patterns in the select attribute of elements such as <xsl:apply-templates>, <xsl:value-of>, <xsl:for-each>, and <xsl:copy-of>. Here's one important thing you need to know, however: |
|
| The select attribute of these elements is more powerful than the match, count, and from attributes of <xsl:template>, <xsl:key>, and <xsl:number>, because you can use full XPath expressions, not just match patterns in select. |
|
| The xsl:template element is used to define a template that can be applied to a node to produce a desired output display. |
|
| There must be either a match or name attribute, or both, and this determines how the template rule can be invoked. If there is only a match attribute, then you can use the xsl:apply-template element to invoke the template rule defined by the xsl:template element. |
|
| If there is only a name attribute, then you can use the xsl:call-template element to invoke the named template defined by the xsl:template element. If both attributes are present, then you may invoke the template by either procedure. |
|
| It is quite possible that more than one template can be applied to a node. The highest priority value template is always chosen. If more than one suitable template has the same highest priority value, then the XSLT processor usually chooses the one that appears last. Different templates can not have both the same name and priority values. This is an error. |
|
| The xsl:template element is always a child of either the xsl:stylesheet or xsl:transform elements. |
|
| This is not a self-closing element. The separate closing element is mandatory. |
|
| match="pattern" |
|
| The match attribute is mandatory unless the element has a name attribute, then this attribute is optional. In other words, there must be either a match or name attribute, or both. This attribute is a pattern that is used to define which nodes will have which template rules applied to them. |
|
| mode="qname" |
|
| The optional mode attribute allows the same nodes to be processed more than once. Each time the nodes are processed, they can be displayed in a different manner. A qname is a qualified name that is composed of an optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML name (for example, xsl:zipcode or zipcode). |
|
| If an xsl:template element has a mode attribute, then it must also have a match attribute. If the xsl:apply-templates element has a mode attribute, then it can only apply templates from xsl:templates elements that also have a mode attribute. Likewise, if the xsl:apply-templates element does not have a mode attribute, then it can only apply templates from xsl:templates elements that also do not have a mode attribute. |
|
| name="qname" |
|
| The name attribute is mandatory unless the element has a match attribute, then this attribute is optional. In other words, there must be either a match or name attribute, or both. A qname is a qualified name that is composed of an optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML name (for example, xsl:zipcode or zipcode). |
|
| If the name attribute is present, you can use the xsl:call-template element to invoke the template. To do this, the name attribute must be the same for both the xsl:template and the xsl:call-template elements. |
|
| Match patterns are a subset of XPath expressions that is, all match patterns are validXPath expressions, but not all XPath expressions are match patterns. The only XPathexpressions that can be patterns are those that return a node set (even a node set with one node) and that use paths that specify only child or attribute nodes. |
|
| Match patterns are defined in the XSLT recommendation itself, whereas XPathexpressions are defined in the XPath recommendation (www.w3.org/TR/xpath); however, the two are compatible because all match patterns are also XPathexpressions. |
|
| Creating Full XPath Expressions |
|
| You can use full XPath expressions in XSLT in the following places: in the select attribute of the <xsl:apply-templates>, <xsl:value-of>, <xsl:for-each>, <xsl:param>, <xsl:variable>, <xsl:wit-param>, <xsl:copy-of>, and <xsl:sort> elements; in attribute value templates; in the test attribute of <xsl:if> and <xsl:when> elements; in the value attribute of <xsl:number>; and in the predicates of match patterns. |
|
| To make things just a little more confusing, however, it turns out that you actually can use XPath expressions in a special, optional part (and only this part) of match patterns: the predicate. As you're going to see here, predicates are XPath expressions that evaluate to either true/false values or numbers, which you enclose in brackets, [ and ]. |
|
| Microsoft and Nonstandard Match Patterns |
|
| Microsoft supports match patterns with its MSXML3® XML processor, but there's one thing you should know: Microsoft also supports a great deal of non-standard, non-W3C syntax in its match patterns. I'm going to stick to the official, W3C, version in this chapter, and if you happen to read Microsoft documentation on match patterns, keep in mind that much of what you read is Microsoft-only. |
|
| Matching the Root Node |
|
| As you've already seen, you can match the root node with the match pattern "/" like this: |
|
<xsl:template match="/">
<HTML>
<xsl:apply-templates/>
</HTML>
</xsl:template> |
|
| Matching Elements |
|
| You can match elements simply by giving their names, as you've also seen. The following template matches <PLANETS> elements: |
|
<xsl:template match="PLANETS">
<HTML>
<xsl:apply-templates/>
</HTML>
</xsl:template> |
|
| Matching Children |
|
| You can use the / step operator to separate element names when you want to refer to a child of a particular node. For example, say that you want to create a rule that applies to only those <NAME> elements that are children of <PLANET> elements. In that case, you can match the expression "PLANET/NAME". Here's a rule surrounds the text of such elements in a <H3> |
|
| HTML element: |
|
<xsl:template match="PLANET/NAME">
<H3>
<xsl:value-of select="."/>
</H3>
</xsl:template> |
|
| You can also use the * character as a wildcard, standing for any element. (* can match only elements, although note that the pattern @* can match any attribute.) For example, the following rule applies to all <NAME> elements that are grandchildren of <PLANET> elements: |
|
<xsl:template match="PLANET/*/NAME">
<H3>
<xsl:value-of select="."/>
</H3>
</xsl:template> |
|
| Matching Element Descendants |
|
| In the preceding section, I used the expression "PLANET/NAME" to match all <NAME> elements that are direct children of <PLANET> elements, and the expression "PLANET/*/NAME" to match all <NAME> elements that are grandchildren of <PLANET> elements. |
|
| However, there's an easier way to perform both matches: just use the expression"PLANET//NAME", which matches all <NAME> elements that are inside <PLANET> elements, no matter how many levels deep (the matched elements are called descendants of the <PLANET> element). In other words, "PLANET//NAME" matches "PLANET/NAME", "PLANET/*/NAME", "PLANET/*/*/NAME", and so on: |
|
<xsl:template match="PLANETS//NAME">
<H3>
<xsl:value-of select="."/>
</H3>
</xsl:template> |
|
| Matching Attributes |
|
| You can match attributes if you preface their names with an @. You've already worked with the UNITS attribute that most of the children of <PLANET> elements support: |
|
<PLANET>
<NAME>Earth</NAME>
<MASS UNITS="(Earth = 1)">
1
</MASS>
<DAY UNITS="days">
1
</DAY>
<RADIUS UNITS="miles">
2107
</RADIUS>
<DENSITY UNITS="(Earth = 1)">
1
</DENSITY>
<DISTANCE UNITS="million miles">
128.4
</DISTANCE>
<!--At perihelion-->
</PLANET> |
|
| To recover the units and display them as well as the values for the mass and so on, you can match the UNITS attribute with @UNITS, as follows: |
|
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/PLANETS">
<HTML>
<HEAD>
.
.
.
</HEAD>
<BODY>
.
.
.
</BODY>
</HTML>
</xsl:template>
<xsl:template match="PLANET">
<TR>
<TD><xsl:value-of select="NAME"/></TD>
<TD><xsl:apply-templates select="MASS"/></TD>
<TD><xsl:apply-templates select="RADIUS"/></TD>
</TR>
</xsl:template>
<xsl:template match="MASS">
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="@UNITS"/>
</xsl:template>
<xsl:template match="RADIUS">
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="@UNITS"/>
</xsl:template>
<xsl:template match="DAY">
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="@UNITS"/>
</xsl:template>
</xsl:stylesheet>
|
|
| Now the resulting HTML table includes not only values, but also their units of measurement: |
|
<HTML>
<HEAD>
<TITLE>
The Planets Table
</TITLE>
</HEAD>
<BODY>
<H1>
The Planets Table
</H1>
<TABLE>
<TR>
.
.
.
<TR>
<TD>Mercury</TD>
<TD>.0553 (Earth = 1)</TD>
<TD>1516 miles</TD>
</TR>
<TR>
<TD>Venus</TD>
<TD>.815 (Earth = 1)</TD>
<TD>3716 miles</TD>
</TR>
.
.
.
</TABLE>
</BODY>
</HTML>
|
|
| You can also use the @* wildcard to select all attributes of an element. For example,"PLANET/@*" selects all attributes of <PLANET> elements. |
|
|
|
|
| XSL Match Pattern |
|
| More Complex Patterns |
|
| section/*/note |
|
| Matches <note> elements that have <section> grandparents. |
|
| stockquote[@symbol] |
|
| Matches <stockquote> elements that have a "symbol" attribute |
|
| stockquote[@symbol="XXXX"] |
|
| Matches <stockquote> elements that have a "symbol" attribute with the value "XXXX" |
|
| emphasis|strong |
|
| Matches <emphasis> or <strong> elements |
|
| Pattern Syntax |
|
| The XPath pattern syntax is described formally in the XPath specification. Here we're going to look at the syntax in abstract, but less formal terms: |
|
• An XPath pattern is a 'location path', a location path is absolute if it begins with a slash ("/") and relative otherwise
• A relative location path consists of a series of steps, separated by slashes
• A step is an axis specifier, a node test, and a predicate |
|
| The formal syntax is made more complicated by the fact that it must describe both the abbreviated and unabbreviated syntaxes |
|
|
|
|
| XSL Match Pattern |
|
| Node Tests & Axis Specifiers |
|
| Node tests are most frequently element names, but other node tests are possible: name |
|
| Matches <name> element nodes |
|
*
Matches any element node |
|
| namespace:name |
|
| Matches <name> element nodes in the specified namespace |
|
| namespace:* |
|
| Matches any element node in the specified namespace |
|
| comment() |
|
| Matches comment nodes |
|
| text() |
|
| Matches text nodes |
|
| processing-instruction() |
|
| Matches processing instructions |
|
| processing-instruction('target') |
|
| Matches processing instructions with the specified target (<?target ...?> |
|
| node() |
|
| Matches any node |
|
| This table lists seven proprietary XSLT elements created by Microsoft for some of its implementations of the XSLT technology. These are not part of the W3C XSLTstandard. |
|
| Element Name | Description |
| xsl:cdata | Creates and adds a new CDATA section which allows you to use markup elements without violating the XML well-formed requirements. |
| xsl:define-template-set | Defines a new set of templates. |
| xsl:entity-ref | Creates and adds an entity reference node. |
| xsl:eval | Evaluates a script expression and generates a string that can appear in the output. |
| xsl:node-name | Adds the current node name to output. |
| xsl:pi | Generates a processing instruction in the output. |
xsl:script
msxsl:script | Creates a scripting area in a template in which you can declare global variables and define functions. msxsl:scriptis used by the latest Microsoft XML implementation (version 3.0). It is a child of the xsl:stylesheet element and has two attributes: language which defines the scripting language (the default is JScript) and implementation-prefix which declares the associated namespace. |
|
|
| Predicates |
|
| Predicates occur after the node test in square brackets. A wide range of expressions are possible. |
|
| nodetest[1] |
|
| Matches the first node |
|
| Most node tests return nodes in document order, only the tests which select ancestor or preceding elements return nodes in reverse document order. The practical result of this is that the "first" node is almost always the one closest to the context node, although parenthesis can change the effective order. |
|
| nodetest[position()=last()] |
|
| Matches the last node |
|
| nodetest[position() mod 2 = 0] |
|
| Matches even nodes |
|
| element[@id='foo'] |
|
| Matches the element(s) with id attributes whos value is "foo" |
|
| element[not(@id)] |
|
| Matches elements that don't have an id attribute |
|
| author[firstname="Norman"] |
|
| Match <author> elements that have <firstname> children with the content "Norman". |
|
| author[normalize-space(firstname)="Norman"] |
|
| Match "Norman" without regard to leading and trailing space. Predicate expressions can be more-or-less arbitrarily complex. |
|
| Axis Specifiers |
|
| The axis of a node test determines what general category of nodes may be considered for the following node test. There are thirteen axes: |
|
ancestor
Ancestors of the current node
ancestor-or-self
Ancestors, including the current node
attribute
Attributes of the current node (abbreviated "@")
child
Children of the current node (the default axis)
descendant
Descendants of the current node
descendant-or-self
Descendants, including the current node (abbreviated "//")
following / following-sibling
Elements which occur after the current node, in document order
preceding / preceding-sibling
Elements which occur before the current node, in document order (returned in reverse-document order)
namespace
The namespace nodes of the current node
parent
The parent of the current node (abbreviated "..")
self
The current node (abbreviated ".") |
|
| The following graphic, courtesy of Crane Softwrights, demonstrates the axes graphically: |
|
 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
0 comments: