540 likes | 734 Views
XML Part 6 XSL: The Extensible Stylesheet Language . World Wide Web Technology. XML. Microsoft XML resources http://msdn.microsoft.com/xml/default.asp? Tutorial on XML http://msdn.microsoft.com/xml/tutorial/default.asp
E N D
XML Part 6 XSL: The Extensible Stylesheet Language World Wide Web Technology
XML • Microsoft XML resources http://msdn.microsoft.com/xml/default.asp? • Tutorial on XML http://msdn.microsoft.com/xml/tutorial/default.asp • Demo of XML & XSL http://msdn.microsoft.com/downloads/samples/internet/xml/multiple_views/default.asp
XSL transformations address some common needs in XML: • Enabling display: The XSL transformation language enables display of XML by transforming XML into grammar and structure suitable for display—for instance, into HTML or the XSL Formatting Objects language. • Direct browsing of XML files: Internet Explorer 5 can apply XSL style sheets that produce HTML, allowing direct browsing of the XML files. • Content delivery to downlevel browsers: XSL transformations can be executed on the server to provide HTML documents for downlevel browsers.
XSL • Schema Translation: The transformation process is independent of any particular output grammar and can be used for translating XML data from one schema to another. • Converting XML through querying, sorting, and filtering: The transformation can be used for general-purpose transformations within a single grammar, including filtering, sorting, and summarizing data.
XML and Style Sheets • Unlike HTML, XML does not predefine display properties for specific elements. Thus it requires a separate style sheet to contain the descriptions of how the XML should be displayed. • This separation of the XML content from its presentation allows the content to be easily repurposed. Internet Explorer 5 supports style sheets written in either CSS or XSL.
XML & XSL • You can enable an XML document for browsing by indicating the type and location of a style sheet with the style sheet processing instruction. The (W3C) Recommendation for style sheet PI syntax can be found at http://www.w3.org/TR/WD-xml-stylesheet . The basic form of the style sheet PI looks like this: • <?xml-stylesheet type="text/xsl" href="mystyle.xsl"?>
XML & XSL • When IE browses the XML document, it looks for this PI and downloads the style sheet as well and uses it to display the XML. • Note that the style sheet PI is used automatically only when displaying the XML directly in the browser. Other uses of XML such as data islands ignore the PI. • Each style sheet PI must have the type attribute. The values of this attribute describe the type of style sheet to apply: "text/css" indicates a CSS style sheet, and "text/xsl" indicates an XSL style sheet.
XML & XSL • The href attribute is a URL to the style sheet. Relative URLs are evaluated relative to the URL of the XML document. • An XML document can include multiple style sheet PIs. IE 5 looks first for a PI of type "text/xsl" and uses the first one it finds. Otherwise it searches for "text/css" PIs and cascades them together using the same process as the HTML LINK element.
XML & XSL • If you browse to an XML document that doesn't contain a valid style sheet PI, IE 5 will display the XML as an expanding and collapsing tree. • This provides a convenient way for XML authors to check that their XML is well-formed. • You can also test XSL style sheets and XML Schema documents for well-formedness by opening them in the browser.
XML & XSL • Well-formedness errors will be reported complete with line numbers to help you find and correct the error. • At times you might want to comment out any style sheet PIs in the source document to isolate source errors from style sheet errors.
Example • The Red Inn Example • View review.xml with an error • View without a style sheet • View the style sheet review.xsl • View with the style sheet
Target DSSL or HTML flow objects • The style sheet must target HTML as its output. • The style sheet applies to the root of the source document. Style sheets written to process against the document element instead will generally not display correctly. There are differences between the document root and the document element • The XSL style sheet must come from the same URL scheme (example: http) and host name as the XML source. The XSL style sheet download follows the same security policies as those for fetching an external entity. • Attempting to read a style sheet from a different domain results in an access violation error unless the security option "Access data across domains" is selected from the Internet Options dialog box.
XSL Transformation • An XSL transformation results in the creation of a new XML tree. • Clear separation between the source document and the result trees fulfils the goal of XML to separate content from presentation • by allowing both the grammar and the structure of the XML source to be independent of the presentation language and structure.
Template Driven Model • The style sheet contains a template of the desired result structure, • identifies data in the source document to insert into this template. • referred to as the template-driven model and works well on regular and repetitive data • familiar to users of Active Server Pages, which embed bits of script within an HTML template to control processing and to insert data
Data Driven Model • XSL also provides capabilities for handling highly irregular and recursive data such as is typical in documents • Template fragments are defined, and the XSL processor combines the results of these fragments into a final result tree based on the shape of the source data • Each template fragment declares the type and context of source nodes it is appropriate for, allowing the XSL processor to match source nodes with template fragments
Combining the models • The above model is known as the data-driven model since the shape of the data drives the shape of the final output • Template-driven and data-driven transformation mechanisms can be combined in a single style sheet, making XSL appropriate to a wide variety of XML applications.
Creating and Populating an HTML Template • <?xml version="1.0"?><portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes"> <stock exchange="nyse"> <name>zacx corp</name> <symbol>ZCXM</symbol> <price dt:dt="number">28.875</price> </stock> <stock exchange="nasdaq"> <name>zaffymat inc</name> <symbol>ZFFX</symbol> <price dt:dt="number">92.250</price> </stock> <stock exchange="nasdaq"> <name>zysmergy inc</name> <symbol>ZYSZ</symbol> <price dt:dt="number">20.313</price> </stock></portfolio>
The data • The structure of this sample data is repetitive and regular— • the stock element structure is repeated several times with the same subelements. • For this example the stock information will be displayed in a table with a stock in each row, and cells containing the name, symbol, and price. • First create a template of HTML elements to display such a table.
<HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>Symbol</TD> <TD>Name</TD> <TD>Price</TD> </TR> <!-- repeat the following row for each stock --> <TR> <TD><!-- symbol goes here --></TD> <TD><!-- name goes here --></TD> <TD><!-- price goes here --></TD> </TR> </TABLE> </BODY> </HTML>
Populating the Template • To populate this template with data from the XSL file, you could manually replace the comments with data from the XML file. • This is essentially the process the XSL performs. • Elements from the XSL namespace are used to locate data in the XML file, and insert it into the HTML template.
Locates stock elements • <HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>Symbol</TD> <TD>Name</TD> <TD>Price</TD> </TR><xsl:for-each select="portfolio/stock"> <TR> <TD><xsl:value-of select="symbol"/></TD> <TD><xsl:value-of select="name"/></TD> <TD><xsl:value-of select="price"/></TD> </TR></xsl:for-each> </TABLE> </BODY></HTML> One row for each
What is happening • The <xsl:for-each> element locates a set of elements in the XML data ("stock" elements inside the "portfolio" element) and repeats a portion of the template for each one. • Since this sample contains three stock elements, three rows will be created.
Select • The select attribute describes how to find a set of elements in the source document. • The syntax for this attribute is called an XSL Pattern, (see later) and works much like navigating a file system hierarchy • In an XSL style sheet, the navigation starts at the current node and drills down into the XML data hierarchy, selecting all the nodes that match the pattern. • In this case the pattern "portfolio/stock" starts at the document root and drills down through the "portfolio" element to select the three "stock" children.
<xsl:value-of> • Within the <xsl:for-each> element, you can further drill down to select children of each "stock" element. • The <xsl:value-of> element selects a specific child and then inserts the text content of that child into the template. • The patterns inside the <xsl:value-of> element's select attribute do not require starting at the document root again but are relative to the element selected in the <xsl:for-each> element.
<xsl:stylesheet> • The preceding template can be made into a complete XSL style sheet by placing it in an XML file and enclosing it within the <xsl:stylesheet> element.
<?xml version='1.0'?><xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>Symbol</TD> <TD>Name</TD> <TD>Price</TD> </TR> <xsl:for-each select="portfolio/stock"> <TR> <TD><xsl:value-of select="symbol"/></TD> <TD><xsl:value-of select="name"/></TD> <TD><xsl:value-of select="price"/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML></xsl:template></xsl:stylesheet>
XSL Stylesheet • An XSL style sheet is an XML file itself, the file begins with the recommended xml declaration. • The <xsl:stylesheet> element indicates that this document is a style sheet file, and provides a location for declaring the XSL namespace. • The XSL namespace URL supported by Microsoft® Internet Explorer 5 is http://www.w3.org/TR/WD-xsl.
xsl:template • You also must wrap the template with <xsl:template match="/"> to indicate that this template corresponds to the root (/) of the XML source document. For details, see Advanced XSL Features. • The entire file must be well-formed to comply with XML rules, including the HTML that comprises the template.
<HTML> • <BODY> • <TABLE BORDER="2"> • <TR> • <TD>Symbol</TD> • <TD>Name</TD> • <TD>Price</TD> • </TR> • <TR> • <TD>ZCXM</TD> • <TD>zacx corp</TD> • <TD>28.875</TD> • </TR> • <TR> • <TD>ZFFX</TD> • <TD>zaffymat inc</TD> • <TD>92.250</TD> • </TR> • <TR> • <TD>ZYSZ</TD> • <TD>zysmergy inc</TD> • <TD>20.313</TD> • </TR> • </TABLE> • </BODY> • </HTML>
Accessing and Outputting Attributes • XSL provides mechanisms for accessing attributes in the source document, • and for generating attributes in the result tree. • XSL attempts to be agnostic about whether data should be encoded as attribute values or subelements, and strives to process either with equal ease.
@attribute • Attributes in the source document can be accessed in XSL Patterns by preceding the attribute name with the @ symbol. • Thus the example below extracts the value of the "exchange" attribute on stock elements and inserts it into the output.
XSL Patterns • XSL Patterns provide a simple query language for identifying nodes in an XML document, based on their • type, • name, • and values, • as well as the relationship of the node to other nodes in the document.
XSL Patterns • For instance, the query "find 'author' elements that have a 'period' attribute with the value 'classical', and that are contained in the 'authors' element at the document root" can be expressed as an XSL Pattern of the form/authors/author[@period='classical'] • queries are a core part of XSL transformations, which associate the query results with templates to create a new XML document. • Queries using the XSL Pattern syntax can also be performed directly against the IE 5 XML Document Object Model (DOM).
XSL patterns • Internet Explorer 5 implements XSL Patterns as described in Section 2.6 of the Extensible Stylesheet Language (December 18th Working Draft), found at http://www.w3.org/TR/1998/WD-xsl-19981216.html
Creating attributes • XSL can create attributes in two ways—by placing them on an output element, such as the BORDER='2' attribute above, or by • adding them to an element with the <xsl:attribute> element. • <xsl:attribute> allows the output attribute's value to be generated from the source data.
Attributes • The name attribute specifies the name for the output attribute, and the contents of the tag are evaluated to determine its value. • In the pervious example, a TITLE attribute is added to the TR element to display a ToolTip generated by mixing text with element and attribute values from the source document. • Attributes can be added to an element that already has attributes directly specified on it, so you can mix direct specifications and <xsl:attribute> freely. Limitations, though: • You cannot add an attribute to an element that already has an attribute of that name. • Attributes added with <xsl:attribute> must appear before children are added to the element.
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <BODY> <TABLE BORDER="2"> <TR> <TD>Symbol</TD> <TD>Name</TD> <TD>Price</TD> </TR> <xsl:for-each select="portfolio/stock"> <TR> <xsl:attribute name="TITLE"><xsl:value-of select="symbol"/> is listed on the <xsl:value-of select="@exchange"/> stock exchange.</xsl:attribute> <TD><xsl:value-of select="symbol"/></TD> <TD><xsl:value-of select="name"/></TD> <TD><xsl:value-of select="price"/></TD> </TR> </xsl:for-each> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
Conditional Templates • Conditional templates are output only if certain conditions exist within the source document. • XSL defines conditional templates with the <xsl:if> and <xsl:choose> elements. • Notice in the following portfolio data that the "stock" element has an attribute named "exchange". • You might want to generate some output only when this attribute has a certain value.
<?xml version="1.0"?><portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes"> <stock exchange="nyse"> <name>zacx corp</name> <symbol>ZCXM</symbol> <price dt:dt="number">28.875</price> </stock> <stock exchange="nasdaq"> <name>zaffymat inc</name> <symbol>ZFFX</symbol> <price dt:dt="number">92.250</price> </stock> <stock exchange="nasdaq"> <name>zysmergy inc</name> <symbol>ZYSZ</symbol> <price dt:dt="number">20.313</price> </stock> </portfolio>
xsl:if • You could easily create another column in the table in which to place the attribute value. • But suppose you want to indicate stocks from a particular exchange by noting them with an "*". The <xsl:if> element provides a mechanism for conditionally inserting structure into the output tree.
Example • The <xsl:if> element inserts an "*" after the symbol for those stocks listed on the NASDAQ stock exchange. • The <xsl:if> contents can be simple text, as in this example, or elements, attributes—any structure allowed by XSL.
<?xml version='1.0'?> • <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • <xsl:template match="/"> • <HTML> • <BODY> • <TABLE BORDER="2"> • <TR> • <TD>Symbol</TD> • <TD>Name</TD> • <TD>Price</TD> • </TR> • <xsl:for-each select="portfolio/stock"> • <TR> • <TD> • <xsl:value-of select="symbol"/> • <xsl:if test="@exchange[.='nasdaq']">*</xsl:if> • </TD> • <TD><xsl:value-of select="name"/></TD> • <TD><xsl:value-of select="price"/></TD> • </TR> • </xsl:for-each> • </TABLE> • <P>* Listed on Nasdaq stock exchange</P> • </BODY> • </HTML> • </xsl:template> • </xsl:stylesheet>
test • The test attribute takes an XSL pattern. • If the query described by the pattern selects one or more nodes, the <xsl:if> template is inserted. • If the selection is empty, the conditional is skipped. • In this case, the query checks to see if the stock element has an "exchange" attribute, and further checks that the value of the exchange attribute is equal to "nasdaq".
Choosing Alternatives • The <xsl:choose> element provides a mechanism for "either/or" processing. • <xsl:choose> contains a series of <xsl:when> elements that are tested in order from top to bottom until a match is found. • An <xsl:otherwise> element can be used to insert a template if no match is found.
Using choose • The following could be added to the above example to colour-code the rows by price— • 0-25 are displayed in green, • 25-50 are displayed in blue, • and 50+ are displayed in red. • The colour is changed by conditionally generating a portion of the value of the STYLE attribute on the table row.
xsl:choose • <TR> • <xsl:attribute name="STYLE">color: • <xsl:choose> • <xsl:when test="price[. $le$ 25]">green</xsl:when> • <xsl:when test="price[. $le$ 50]">blue</xsl:when> • <xsl:otherwise>red</xsl:otherwise> • </xsl:choose> • </xsl:attribute> • <TD> • ...
$le$ • The $le$ operator (less-than-or-equals) is an XSL Pattern extension described in the XQL Proposal , and is not part of the December 1998 XSL Working Draft.
Sorting XML • When using the <xsl:for-each> element to display a repetitive data structure, the template within the <xsl:for-each> is normally processed for each selected item in the order that they appear in the source document. • The order-by attribute allows you to sort the selected items before iterating over the templates. • The syntax of the order-by attribute is an XSL Pattern that points to a node in the source document that should be used as the sort key.
<?xml version="1.0"?> • <portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes"> • <stock exchange="nyse"> • <name>zacx corp</name> • <symbol>ZCXM</symbol> • <price dt:dt="number">28.875</price> • </stock> • <stock exchange="nasdaq"> • <name>zaffymat inc</name> • <symbol>ZFFX</symbol> • <price dt:dt="number">92.250</price> • </stock> • <stock exchange="nasdaq"> • <name>zysmergy inc</name> • <symbol>ZYSZ</symbol> • <price dt:dt="number">20.313</price> • </stock> • </portfolio>
Ordering • An order-by="price" can be added to the <xsl:for-each> element to sort the stocks by price.