Home Backend Development XML/RSS Tutorial Detailed introduction to code case analysis of XSLT template conversion to XML documents

Detailed introduction to code case analysis of XSLT template conversion to XML documents

Mar 21, 2017 pm 04:31 PM

    XMLThe document can only represent the content of the data, while the actual data is to be presented on the web page. You can use CSS to format the XML document so that it can be displayed. This content has been explained in detail in the previous article. . In addition to CSS, there is also a language that can also express XML data content in web pages, that is XSL. XSL Languages ​​include XSLT (XSL Transformation) and FO(Format Object). The XSLT document can convert the XML document into other document forms, such as HTML , Text, etc. FO is used for formatted output, because W3C also forms a unification for FO Standard, here we will only explain the usage of XSLT.

     The most important part when using XSLT is choosing XMLNode values ​​and creation templates. The tag used to create a template is match Attribute is used to determine the XML node it matches. The tag used to select the XML node value is , this tag requires select attribute to determine the XML node it matches. A simple example will be used below to illustrate, look at the following XML document:

1 <?xml version="1.0" encoding="utf-8"?>
2 <?xml-stylesheet type="text/xsl" href="stylesheet.xslt"?>
3 <xml>
4   <book>
5     <name>Xml应用系列</name>
6     <author>学路的小孩</author>
7     <date>2009-03-23</date>
8   </book>
9 </xml>
Copy after login


## Code description: The first line is the XML file Declaration part; the second line declares the introduction of the XSLT file, and the type attribute indicates that the type of the file is text/ xsl, href attributes point to a XSLT file named stylesheet. xslt. The third line and onwards are the XML document part. The following is the content of stylesheet.xslt:

1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 3   <xsl:template match="/">
 4     <html>
 5       <head>
 6         <title>第一个XSLT文件</title>
 7       </head>
 8       <body>
 9         <xsl:apply-templates select="xml"></xsl:apply-templates>
10       </body>
11     </html>
12   </xsl:template>
13   <xsl:template match="xml"> 
14     <table style="background-color:orange">
15       <tr>
16         <th>书名</th>
17         <th>作者</th>
18         <th>日期</th>
19       </tr>
20       <xsl:apply-templates select="book"></xsl:apply-templates>
21     </table>
22   </xsl:template>
23   <xsl:template match="book">
24     <tr>
25       <td>
26         <xsl:value-of select="name"/>
27       </td>
28       <td>
29         <xsl:value-of select="author"/>
30       </td>
31       <td>
32         <xsl:value-of select="date"/>
33       </td>
34     </tr>
35   </xsl:template>
36 </xsl:stylesheet>
Copy after login


## ​

Code description: Since the format of the XSLT document is still in the XML format, the first line is ## The header declaration of #XML; the second line is the version of XSLT and the namespace declaration, and the tag is XSLTThe following node of the document. The third line uses to create a template, select="/" Indicates that the match is the root node of the document. The fourth to eleventh lines are the HTML node information to be generated by this node, of which the ninth line The tag indicates the application template, where select="xml" indicates that the template to be called is the matching XML node template, which appears on line 13. All subsequent lines (except lines 26, etc.) are nothing more than repetitions of these contents without much introduction. The twenty-sixth line is the content of selecting the name tag. Use IE to open the XML file, and the displayed content is as follows:  ​

另外,XSLT还具有流程控制、条件选择、循环处理、元素排序等功能。下面通过一个实例来说明,其中XML文档内容如下:

1 <?xml version="1.0" encoding="utf-8" ?> 
 2 <?xml-stylesheet type="text/xsl" href="bookListStyle.xslt"?>
 3 <bookList>
 4   <category type="计算机">
 5     <book id="1">
 6       <title>网页与Web程序设计</title>
 7       <author>吴利兵</author>
 8       <pubInfo>
 9         <publisher>机械工业出版社</publisher>
10         <pubDate>2009-04-01</pubDate>
11         <price>16.50</price>
12       </pubInfo>
13     </book>
14     <book id="2">
15       <title>软件工程</title>
16       <author>邓良松</author>
17       <pubInfo>
18         <publisher>西安电子科技出版社</publisher>
19         <pubDate>2005-06-10</pubDate>
20         <price>33.20</price>
21       </pubInfo>
22     </book>
23   </category>
24   <category type="小说">
25     <book id="3">
26       <title>茶花女</title>
27       <author>小仲马</author>
28       <pubInfo>
29         <publisher>外语出版社</publisher>
30         <pubDate>2005-06-30</pubDate>
31         <price>22.00</price>
32       </pubInfo>
33     </book>
34     <book id="4">
35       <title>红楼梦</title>
36       <author>曹雪芹</author>
37       <pubInfo>
38         <publisher>中国教育出版社</publisher>
39         <pubDate>2005-09-06</pubDate>
40         <price>55.00</price>
41       </pubInfo>
42     </book>
43   </category>
44 </bookList>
Copy after login


bookListStyle.xslt文件的内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
        <title>图书列表</title>
        <style>
          <![CDATA[
            body,td,th{
              font-size:10pt;
              font-family:宋体;
            }
            body{
              background-color:#c0c0c0;
            }
            table{
              border:solid red 1px;
              margin-left:30px;
              margin-right:30px;
              background-color:#ffffc0;
              cellPadding:4;
            }
          ]]>
        </style>
      </head>
      <body>
        <table>
          <caption align="top" style="font-weight:bold; text-align:left">图书列表</caption>
          <tr style="color:#8b0000" align="left">
            <th width="5%">编号</th>
            <th width="10%">类别</th>
            <th width="25%">书名</th>
            <th width="20%">作者</th>
            <th width="25%">出版社</th>
            <th width="10%">出版日期</th>
            <th width="5%">定价</th>
          </tr>
          <xsl:for-each select="bookList/category/book">
            <xsl:sort select="pubInfo/price" order="descending"/>
            <tr>
              <xsl:attribute name="style">
                color:
                <xsl:if test="../@type[.=&#39;计算机&#39;]">blue</xsl:if>
              </xsl:attribute>
              <xsl:attribute name="title">
                <xsl:value-of select="title"/>
                <xsl:choose>
                  <xsl:when test="../@type[.=&#39;计算机&#39;]">
        类别:计算机类图书
                  </xsl:when>
                  <xsl:otherwise>
        类别:小说类图书
                  </xsl:otherwise>
                </xsl:choose>
        作者:<xsl:value-of select="author"></xsl:value-of>
                <br/>
        出版社:<xsl:value-of select="pubInfo/publisher"/>
                <br/>
        出版日期:<xsl:value-of select="pubInfo/pubDate"/>
                <br/>
        定价:<xsl:value-of select="pubInfo/price"/>元
              </xsl:attribute>
              <td>
                <xsl:value-of select="@id"/>
              </td>
              <td>
                <xsl:value-of select="../@type"/>
              </td>
              <td>
                <xsl:value-of select="title"/>
              </td>
              <td>
                <xsl:value-of select="author"/>
              </td>
              <td>
                <xsl:value-of select="pubInfo/publisher"/>
              </td>
              <td>
                <xsl:value-of select="pubInfo/pubDate"/>
              </td>
              <td>
                <xsl:value-of select="pubInfo/price"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Copy after login

 

     这里不再对代码进行分析,请读者自己理解这段代码,并动手写一下自己的XSLT模板。这段代码的运行效果如下图:

 

 

The above is the detailed content of Detailed introduction to code case analysis of XSLT template conversion to XML documents. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles