Crazy XML learning notes (11) ----------- XSLT explanation

Source: Internet
Author: User
Tags xsl file
The reason why the World Wide Web Consortium (W3C) began to develop XSL is that there is a need for XML-based style sheet languages. The basic knowledge of XML has been sorted out before. if you are interested, you can visit the following URL:

#

An error occurred while displaying the text and text in the article. please forgive me!

You can download the w3cshool API documentation.

Connection # Download

XSLT starts with XSL and ends with XSLT, XPath, and XSL-FO.

XPath will be explained in detail later

Started with XSL

XSL refers to the extended style table language (EXtensibleSTylesheetLAnguage ).

The reason why the World Wide Web Consortium (W3C) began to develop XSL is that there is a need for XML-based style sheet languages.

CSS = HTML style sheet

HTML uses pre-defined tags, meaning of tagsEasy to understand.

In the HTML element

How to display it

It is easy to add styles to HTML elements. With CSS, it is easy to tell the browser to display an element with a specific font or color.

XML does not use pre-defined labels (we can use any tag names we like), and the meaning of these labelsNot all are so easy to understand.

Element definition table-Clear browser.XSL = XML style sheet
I don't know how to display it

XSLDescriptionHow to display XML documents!

  • XSLT

  • A language used to convert XML documents.

  • XPath

  • A language used for navigation in XML documents.

  • XSL-FO

  • A language used to format XML documents.


XSLT is a language used to convert XML documents into XHTML documents or other XML documents.

XPath is a language used for navigation in XML documents.

Before you continue learning, you need to have a basic understanding of the following:

  • HTML/XHTML

  • XML/XML namespace

  • XPath

If you want to learn about these projects, visit these tutorials on our homepage.

  • XSLT refers to XSL Transformation (XSL Transformations ).

  • XSLT is the most important part of XSL.

  • XSLT can convert an XML document into another XML document.

  • XSLT uses XPath for navigation in XML documents.

  • XPath is a W3C standard.

XSLT is the most important part of XSL.

XSLT is used to convert an XML document to another XML document, or other types of documents that can be recognized by the browser, such as HTML and XHTML. Generally, XSLT converts each XML element into an (X) HTML element to complete this task.

With XSLT, you can add or remove elements and attributes to or from an output file. You can also rearrange elements, perform tests, and decide which elements to hide or display.

A common saying about the conversion process is,XSLT converts an XML source tree to an XML result tree..

XSLT uses XPath to search for information in XML documents. XPath is used to navigate the XML document through elements and attributes.

If you want to learn XPath first, visit our XPath tutorial.

During the conversion process, XSLT uses XPath to define the parts of the source document that can match one or more predefined templates. Once the matching is found, XSLT converts the matching part of the source document to the result document.

Declare the document as the root element of the XSL style sheet Or .

Note: And Is completely synonymous and can be used!

According to W3C XSLT standards, the correct way to declare an XSL style sheet is:

 

Or:

 

To access XSLT elements, attributes, and features, we must declare the XSLT namespace at the top of the document.

Xmlns: xsl = "http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must include the attribute version = "1.0 ".

Now we want to convert the following XML document ("cdcatalog. xml") to XHTML:

 
   
      
   Empire Burlesque    Bob Dylan    
   
    USA
       
   
    Columbia
       
   
    10.90
       
   
    1985
     
  ...
 

Open the XML file (usually by clicking a link)-the XML file will display the root element and child element in color code mode. Click the plus or minus sign on the left side of the element to expand or contract the structure of the element. To view the original XML source file (without the plus sign or minus sign), select "View page source code" in the browser menu ".

Open the XML file, right-click the XML file, and select "View Page Source Code ". The XML document displays the root element and child element in a color-based code.

Open the XML file, right-click the XML file, and select "framework"/"View Source Code ". The XML document is displayed as plain text.

Create an XSL style table ("cdcatalog. xsl") with a conversion template "):

 
 
          My CD Collection    
  
 
Elements mean an HTML table, a piece of furniture, or something else-a browser.XSL-not only the style sheet language XSL includes three parts:The basic knowledge you need before learning:What is XSLT?XSLT = XSL conversionXSLT uses XPathHow does it work?Correct style table declarationStarting with an original XML documentView XML files in Internet Explorer and Firefox:View the XML file in Netscape 6:View the XML file in Opera 7:Create an XSL style sheet
Title Artist

Link an XSL style sheet to an XML document

Add an XSL style sheet reference to the XML document ("cdcatalog. xml:

 
 
   
      
   Empire Burlesque    Bob Dylan    
   
    USA
       
   
    Columbia
       
   
    10.90
       
   
    1985
     
  ...
 

Element

Element is used to build a template.

MatchAttributes are used to associate XML elements and templates. The match attribute can also be used to define a template for the entire document. The value of the match attribute is an XPath expression (for example, match = "/" defines the entire document ).

Let's take a look at the simplified version of the XSL file in the previous section:
  
  
        My CD Collection   
    
 
Title Artist
. .
Code explanation:

Because the XSL style sheet itself is also an XML document, it is always started from the XML declaration:

  

Next element, Defines this document as an XSLT style sheet document (along with the version number and XSLT namespace attributes ).

Element defines a template. WhileMatch = "/"The template is associated with the root of the XML source document.

The content inside the element defines the HTML code written to the output result.

The last two rows define the end of the template and the end of the style sheet.

The conversion result is similar to the following:

Element

Element is used to extract the value of a selected node and add the value to the output stream of the conversion:

   
   
         My CD Collection   
     
 
Title Artist

Note:SelectThe attribute value is an XPath expression. This expression works in a way similar to locating a file system. you can select sub-directories as the forward slash.

The preceding conversion result is similar to the following:


Element

Element can be used to select each XML element in the specified node set.

    
    
             My CD Collection    
            
      
      
 
Title Artist

Note:SelectThe attribute value is an XPath expression. This expression works in a way similar to locating a file system. you can select sub-directories as the forward slash.

The preceding conversion result is similar to the following:

Result filtering

In Add a selection attribute criterion to the element. we can also filter the output results from the XML file.

    
Valid filter operators:
  • = (Equal)

  • ! = (Not equal)

  • <(Less)

  • > (Greater)

    
    
          My CD Collection  
         
      
      
 
Title Artist
The preceding conversion result is similar to the following:


Elements are used to sort the results.

Where to place sorting information

To sort the results, simply add Add Element:

    
    
             My CD Collection    
            
             
      
      
 
Title Artist

Note:SelectAttribute indicates the XML elements to be sorted.

The preceding conversion result is similar to the following:


Element

To test the XML file content, add Element.

Syntax
     
      
...... If the condition is true, the output ......
     

Where to place Element

To add conditional tests, see Add element internally Element:

      
      
               My CD Collection    
              
                 
         
        
        
 
Title Artist

Note: RequiredTestThe attribute value contains the expression that requires the value.

The above code only outputs the title and artist elements of a CD with a price higher than 10.

The preceding conversion result is similar to the following:


Element syntax
         
        
         
... Output...
          
        
         
... Output ....
        
       

Where to place selection conditions

To insert a multi-condition test for an XML file, add , And :

       
       
                My CD Collection    
               
         
                   
                      
          
         
                     
         
         
 
Title Artist

The above code adds a pink background color to the "Artist" column when the CD price is higher than 10.

The preceding conversion result is similar to the following:

View the XML file, the XSL file, and the result.

Another example

This is another one that contains two Element example:

       
       
                My CD Collection    
               
         
                   
                      
          
         
                     
         
                     
         
         
 
Title Artist

The above code adds a pink background color to the "Artist" column when the CD price is higher than 10, add a gray background color to the "Artist" column when the CD price is higher than 9 and less than or equal to 10.

The preceding conversion result is similar to the following:


Element

An element can apply a template to the current element or the child node of the current element.

Assume that When a select attribute is added to an element, this element only processes child elements that match the attribute value. We can use the select attribute to specify the sequence in which the subnode is processed.

See the following XSL style sheet:

        
        
         
          My CD Collection 
           
         
         
          

Title:
Artist:

XSLT element

For more details about the following elements, click the link in the element column.

  • N: indicates the earliest Netscape version that supports this label.

  • IE: indicates the earliest version of Internet Explorer that supports this label.

Note: elements supported in IE 5 may have non-standard behaviors because IE 5 was published before XSLT was formally established as the W3C standard.

Element Description IE N
Apply-imports Apply the template rules from the imported style sheet. 6.0
Apply-templates Apply the template to the current element or the child element of the current element. 5.0 6.0
Attribute Add attributes to an element. 5.0 6.0
Attribute-set Create a named property set. 6.0 6.0
Call-template Call a specified template. 6.0 6.0
Choose And And Collaborative use to express multi-condition tests. 5.0 6.0
Comment Create a comment node in the result tree. 5.0 6.0
Copy Create a backup for the current node (no sub-nodes and attributes ). 5.0 6.0
Copy- Create a backup (with sub-nodes and attributes) for the current node ). 6.0 6.0
Decimal-format Defines the characters and symbols used to convert a number to a string using the format-number () function. 6.0
Element Create an element node in the output document. 5.0 6.0
Fallback If the processor does not support an XSLT element, it requires a piece of spare code to run. 6.0
For-each Traverses each node in the specified node set. 5.0 6.0
If Contains a template, which is applied only when a specified condition is set. 5.0 6.0
Import It is used to add the content of a style sheet to another style sheet. 6.0 6.0
Include Include the content in one style sheet to another. 6.0 6.0
Key Declare a named key. 6.0 6.0
Message Write a message to the output (used for error reporting ). 6.0 6.0
Namespace-alias Replace the namespace in the style sheet with different namespaces in the output. 6.0
Number Measure the integer position of the current node and format the number. 6.0 6.0
Otherwise Rules The default action of the element. 5.0 6.0
Output Define the output document format. 6.0 6.0
Param Declare a local or global parameter. 6.0 6.0
Preserve-space Defines elements that retain white space. 6.0 6.0
Processing-instruction Generate a processing command node. 5.0 6.0
Sort Sort the results. 6.0 6.0
Strip-space Defines the elements that should be deleted from white spaces. 6.0 6.0
Stylesheet Defines the root element of the style sheet. 5.0 6.0
Template The rule applied when the specified node is matched. 5.0 6.0
Text Use a style sheet to generate a text node. 5.0 6.0
Transform Defines the root element of the style sheet. 6.0 6.0
Value- Extract the value of the selected node. 5.0 6.0
Variable Declare local or global variables. 6.0 6.0
When Rules The action of the element. 5.0 6.0
With-param Specifies the parameter value of a template. 6.0 6.0

The above are crazy XML learning notes (11) ----------- content explained by XSLT. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.