XML study (Lesson 4: XSL Style Sheets (Part I))

    XML is a wonderful way for us to organize data exchange between organizations and they are among the applications. But sooner or later, we will explore the diverse database of everywhere. XML standards and even then, we still need an effective tool to present data in many different styles suitable for application processing in a different place.

XSL - eXtensible Style Sheet (pages describe posture) is a standard language to help us transform (transform) an XML document into another format, such as HTML, Wireless (radio) Markup Language (WML), and even a XML. As primitive, XSL is designed for multiple HTML born in different forms depending on the style sheet. Posture that is more XSL for XML, because the very nature of XML is a structured piece of data.
For example we have two versions for an XML style sheet, one used to create regular HTML Web page on the computer, while the other used to create Web pages for Mobile Phone or Pocket PC, the device has small screen . Both Web sites contain the same amount of data, can be on the small screen, the limit of critical data only, but the presentation can be very different.

However, shortly thereafter, it was found XML can be changed into any XSL Output Format, even the XML.There is a new version, or the XSL is just born. It is called XSL Transformations (XSLT) .

We will in turn learn the common syntax of XSL. But not much, but it gives you a basic idea of this technique so you can start using XSL style sheets and processed data in XML documents. Want a full XSL reference, you can visit http://www.w3.org/Style/XSL .

Remember that just like XPath, XSL and XSLT is the standard set for what we require an application program is done to support them need to be. However, who deploy that program, and programming language will do. Such as Microsoft MSXML version 3 for me to use XSL and XSLT.

Những trang XSL Style Sheet

These pages define the XSL style sheets (page posture) so that we can apply to the XML document. A style sheet contains instructions (instructions) to make an XML parser how to generate (generate) a browser documentation for these data results in an XML document.

XSL style sheet itself is also a well-formed XML, but it contains the commands (commands), XSL and HTML text sentences using the whole of the output.

XML parser to recognize commands in a XSL, you must declare (declare) a namespace in the root element, usually with a prefix xsl . A style sheet usually contains one of two namespaces: one namespaceXSL primitive ( http://www.w3.org/TR/WD-xsl ) or the new namespace XSLT ( http://www.w3.org/1999 / XSL / Transform ). Microsoft XML parser ( MSXML ) from version 3.0 or higher will support both namespaces.

Please note that Internet Explorer version 5.x to use MSXML 2.5, should not support the XSLT namespace. Want to overcome it, or you install Internet Explorer version 6, or you install MSXML3 inReplace mode , using tools called Xmlinst.exe to support additional functionality in the XSLT namespace IE v5.x.

The Root Element XSL document in a document often is a stylesheet Element . It contains one or moreElement template to be matched (because the same pair) with the data in XML documents, such as document order (order) below:

<? xml version = "1.0" ?> 
< Order OrderNo = "1047" > 
   < order date > 03/26/2002 </ date order > 
   < Customer > John Costello </ Customer > 
   < Item > 
      < Product ProductID = "1" UnitPrice = "70" > Chair </ Product > 
      < Quantity > 6 </ Quantity > 
   </ Item > 
   < Item > 
      < Product ProductID = "2" UnitPrice = "250" > Desktop </ Product > 
      < Quantity > 1 </ Quantity > 
   </ Item > 
</ Order >
Since the XSL style sheet is an XML document, it must comply with all laws of a well-formed XML. Here is a simple XSL style sheet can be applied to the document order:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <HTML>
         <HEAD>
            <TITLE>Northwind Home Page</TITLE>
         </HEAD>
         <BODY>
            <P>Customer Order</P>
         </BODY>
      </HTML>
   </xsl:template>
</xsl:stylesheet>
This style sheet is based on XSLT namespace and contains a mere template (included in the table) is applied to the Root (symbols separated by slash / is the value of the attribute match ) of the XML document inside the Element and all of it.

A template is then composed of a series of HTML Tags will appear in the record results, but in this case the template does not do anything useful, it only output (for a) an HTML document exactly as in XSL and does not contain any data from input XML documents. To merge (harmony) in the XML data into XSL template, you need to use a few commands (commands) XSL.

Lệnh value-of

XSL defines a number of orders processed (processing commands) to extract data from an XML document and air it on a record result. The basic commands and most useful of these is the command value-of .Order value-of select value (value) of an Element or Attribute that the XML file and output it to air.

Command -value of Element in the form of an XML XSL. It uses an attribute named select a value is an XPath Location Path to extract a node. The result is the value of (value-of) Node him. Therefore, rather than before, now we can present the XML data with value-of command as follows:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <HTML>
         <HEAD>
            <TITLE>Northwind Home Page</TITLE>
         </HEAD>
         <BODY>
            <P>Customer Order</P>
            <P>Order No: 
               <xsl:value-of select="Order/@OrderNo"/>
            </P>
            <P>Date: 
               <xsl:value-of select="Order/OrderDate"/>
            </P>
            <P>Customer: 
               <xsl:value-of select="Order/Customer"/>
            </P>
         </BODY>
      </HTML>
   </xsl:template>
</xsl:stylesheet>
The Style Attribute extract this term sheet and OrderNo OrderDate value of the Elementsand Element Order Customer from using an XPath location path. Note that the XPathexpressions here, relative to the context node specified in the match of the Elementtemplate parameter (in this case is the Root Element, symbols separated by slash /).

Applying Style sheet to record this order (order) XML will be the following HTML:
<HTML>
   <HEAD>
      <TITLE>Northwind Home Page</TITLE>
   </HEAD>
   <BODY>
      <P>Customer Order</P>
      <P>Order No: 1047</P>
      <P>Date: 2002-03-26</P>
      <P>Customer: John Costello</P>
   </BODY>
</HTML>

Lệnh for horses

In an XML document, there may be more Elements bearing the same name to refer to a list of things similar. For example in the document Element ordering two items to describe the two items are located.

Most programming languages give us the means to apply the same processing for all the items in the group. As in Visual Basic have DO FOR loop to loop or iterate through each item in the set. In XSL, too, you can use for-each in turn to go through each Element in the group, using the select attribute to specify the nodes that you want to work.

For example we can do better for the style sheet by listing the items in the Order into a table:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <HTML>
         <HEAD>
            <TITLE>Northwind Home Page</TITLE>
         </HEAD>
         <BODY>
            <P>Customer Order</P>
            <P>Order No: 
               <xsl:value-of select="Order/@OrderNo"/>
            </P>
            <P>Date: 
               <xsl:value-of select="Order/OrderDate"/>
            </P>
            <P>Customer: 
               <xsl:value-of select="Order/Customer"/>
            </P>
            <TABLE Border="0">
               <TR>
                  <TD>ProductID</TD>
                  <TD>Product Name</TD>
                  <TD>Price</TD>
                  <TD>Quantity Ordered</TD>
               </TR>
               <xsl:for-each select="Order/Item">
                  <TR>
                     <TD>
                        <xsl:value-of select="Product/@ProductID"/>
                     </TD>
                     <TD>
                        <xsl:value-of select="Product"/>
                     </TD>
                     <TD>
                        <xsl:value-of select="Product/@UnitPrice"/>
                     </TD>
                     <TD>
                        <xsl:value-of select="Quantity"/>
                     </TD>
                  </TR>
               </xsl:for-each>
            </TABLE>
         </BODY>
      </HTML>
   </xsl:template>
</xsl:stylesheet>
This time the style sheet, we sure go through each parser to retrieve the Item ElementAttributes Element ProductID and UnitPrice of Product, and Product and Quantity valuesof the Elements, and then to the table

Note here the relative XPath expressions using the Node specified in the command for-each work context node. In this case it is the Node Item. End of the for-each loop is the closing tag of the Element for-each (</ xsl: for-each ). In this style sheet to apply in order to document for the following HTML:

<HTML>
   <HEAD>
      <TITLE>Northwind Home Page</TITLE>
   </HEAD>
   <BODY>
      <P>Customer Order</P>
      <P>Order No: 1047</P>
      <P>Date: 2002-03-26</P>
      <P>Customer: John Costello</P>
      <TABLE Border="0">
         <TR>
            <TD>ProductID</TD>
            <TD>Product Name</TD>
            <TD>Price</TD>
            <TD>Quantity Ordered</TD>
         </TR>
         <TR>
            <TD>1</TD>
            <TD>Chair</TD>
            <TD>70</TD>
            <TD>6</TD>
         </TR>
         <TR>
            <TD>2</TD>
            <TD>Desk</TD>
            <TD>250</TD>
            <TD>1</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>


Part BODY of the HTML on the display as follows:

Customer Order
Order No: 1047
Date: 2002-03-26
Customer: John Costello
ProductIDProduct NamePriceQuantity Ordered
1Chair706
2Desk2501

Lệnh Attribute

Sometimes we want to create a record Attribute with a value output from the input XML document. Eg corresponding to each name of a Product, you want to create a hyperlink to switch (pass) the ProductIDthrough another site, where it will display information about this item.

To create a hyperlink in an HTML file, you need to create an Element A (Anchor) with an href attribute .You can use the command attribute of the XSL to perform sex as illustrated in the following style sheet:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <HTML>
         <HEAD>
            <TITLE>Northwind Home Page</TITLE>
         </HEAD>
         <BODY>
            <P>Customer Order</P>
            <P>Order No: 
               <xsl:value-of select="Order/@OrderNo"/>
            </P>
            <P>Date: 
               <xsl:value-of select="Order/OrderDate"/>
            </P>
            <P>Customer: 
               <xsl:value-of select="Order/Customer"/>
            </P>
            <TABLE Border="0">
               <TR>
                  <TD>ProductID</TD>
                  <TD>Product Name</TD>
                  <TD>Price</TD>
                  <TD>Quantity Ordered</TD>
               </TR>
               <xsl:for-each select="Order/Item">
                  <TR>
                     <TD>
                        <xsl:value-of select="Product/@ProductID"/>
                     </TD>
                     <TD>
                        <A>
                           <xsl:attribute name="HREF">Products.asp?ProductID=
                              <xsl:value-of select="Product/@ProductID"/>
                           </xsl:attribute>
                           <xsl:value-of select="Product"/>
                        </A>
                     </TD>
                     <TD>
                        <xsl:value-of select="Product/@UnitPrice"/>
                     </TD>
                     <TD>
                        <xsl:value-of select="Quantity"/>
                     </TD>
                  </TR>
               </xsl:for-each>
            </TABLE>
         </BODY>
      </HTML>
   </xsl:template>
</xsl:stylesheet>

This style sheet applies to the order XML document, you will have the following HTML file:

<HTML>
   <HEAD>
      <TITLE>Northwind Home Page</TITLE>
   </HEAD>
   <BODY>
      <P>Customer Order</P>
      <P>Order No: 1047</P>
      <P>Date: 2002-03-26</P>
      <P>Customer: John Costello</P>
      <TABLE Border="0">
         <TR>
            <TD>ProductID</TD>
            <TD>Product Name</TD>
            <TD>Price</TD>
            <TD>Quantity Ordered</TD>
         </TR>
         <TR>
            <TD>1</TD>
            <TD>
               <A HREF="Products.asp?ProductID=1">Chair</A>
            </TD>
            <TD>70</TD>
            <TD>6</TD>
         </TR>
         <TR>
            <TD>2</TD>
            <TD>
               <A HREF="Products.asp?ProductID=2">Desk</A>
            </TD>
            <TD>250</TD>
            <TD>1</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>


Part BODY of the HTML on the display as follows:

Customer Order
Order No: 1047
Date: 2002-03-26
Customer: John Costello
ProductIDProduct NamePriceQuantity Ordered
1Chair706
2Desk2501


You can leave the mouse cursor over the word Chair or text Desk hyperlink to see their names displayed in the status bar of your browser.

(Continued)

0 comments:

Post a Comment