How to use XSLT to convert XML Sample code

Source: Internet
Author: User
Tags xsl file xslt
XSLT is a language used to convert XML document structures. it is short for EXtensibleStyleLanguageExtensionsTransformations. 1. Introduction

XSLT is a Language used to convert XML document structures. it is short for EXtensible Style Language Extensions Transformations.

XSLT is similar to CSS in HTML, but it is more powerful than CSS.

According to the W3C specification, the earliest design of XSLT is to help convert XML documents into other documents.

However, as the application goes deeper, XSLT is not only used to convert XML into HTML or other document formats, but also to convert XML document structures.

The elements and attributes of XSLT provide a declaration for processing XML data. you can use XSLT words to extract content from other documents and create new elements and attributes, in more cases, the two methods are organically combined.

XSLT 1.0 conversion requires two operation files (XML source file and XSLT style sheet file) to generate a result document.

In the new XSLT 2.0 standard, the two can be combined.

Using XSLT, you can match each element and its attributes with HTML or XHTML to display and output the document content correctly.

An XSLT style sheet is a correct and valid XML document in XML format, and its extension is. xsl.

The syntax for using XSLT style sheets in XML documents is as follows:

 

2. use XSLT to convert XML applications

In this example, the XSLT extensible style is used to convert an XML file to a file in another format before output.

In this example, XML is converted using XSLT, and the data in the XML file after XSLT style conversion is output.

First, declare the XML file and specify the document as an XML file.

Note that the XSLT style sheet itself is an XML document, so it also complies with the XML document rules.

Then declare the XSLT style sheet and the prefix of the XSLT namespace (xsl: stylesheet ).

The XSLT namespace prefix can be written in two ways: xsl: stylesheet and xsl: transform.

Its meaning and function are exactly the same, but the former is more commonly used. Specify the XSLT namespace through the xmlns: xsl attribute.

Then define the template rules. here we use the xsl: template element for encapsulation. The Math attribute specifies a mode to describe the input that the rule matches.

Finally, convert the XML document.

Code 3

1. the code for creating the cdcatalog. xsl file is as follows:

 
  
   My CD Collection
   
   
 
Title Artist

2. the code for creating cdcatalog. xml is as follows:

 
 
 
  
   Empire BurlesqueBob Dylan
   
    USA
   
   
    Columbia
   
   
    10.90
   
   
    1985
   
  
  
   Hide your heartBonnie Tyler
   
    UK
   
   
    CBS Records
   
   
    9.90
   
   
    1988
   
  
 

IV. running results

The preceding section describes how to use XSLT to convert the sample code of XML. For more information, see other related articles in the first PHP community!

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.