search
HomeBackend DevelopmentXML/RSS TutorialCrazy XML study notes (11) -----------XSLT explanation


The basic knowledge of XML has been sorted out. If you are interested, you can visit the following URL

http://www.php.cn/

There is an error in the image and text display part of the article, and it cannot be Display, please understand!

If you want to check it out, you can download the w3cshool API document

Connect to http://www.php.cn/Download

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

XPath will be explained in detail later

Start with XSL

XSL refers to the Extended Stylesheet Language (EXtensible Stylesheet Language).

The World Wide Web Consortium (W3C) started developing XSL because there was a need for an XML-based stylesheet language.

CSS = HTML style sheet

HTML uses predefined tags, and the meaning of the tagsis easy to understand.

The

element in the HTML element defines a table - and the browser knows how to display it.

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

The meaning

is not always easy to understand

. The

element means an HTML table, a piece of furniture, or something else - the browser doesn't know how to display it.

XSL can describehow to display XML documents!

XSL - More than just a stylesheet languageXSL consists of three parts:

XSLT

  • A language for transforming XML documents.

  • XPath

  • A language for navigating in XML documents.

  • XSL-FO

  • A language for formatting XML documents.


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

XPath is a language for navigating in XML documents.

Basic knowledge you need to have before studying:

Before you continue studying, you need to have a basic understanding of the following knowledge Learn about:

HTML / XHTML

  • XML / XML namespace

  • XPath

  • If you would like to learn about these projects first, please contact us Home page to access these tutorials.

What is XSLT?

XSLT refers to XSL Transformations.

  • XSLT is the most important part of XSL.

  • XSLT can transform one XML document into another.

  • XSLT uses XPath to navigate within XML documents.

  • XPath is a W3C standard.

XSLT = XSL Transformation

XSLT is the most important part of XSL.

XSLT is used to convert one XML document into another XML document, or other types of documents that can be recognized by browsers, such as HTML and XHTML. Typically, XSLT does this by converting each XML element into an (X)HTML element.

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

A common way of describing the transformation process is that

XSLT transforms an XML source tree into an XML result tree

.

XSLT uses XPath

XSLT uses XPath to find information in XML documents. XPath is used to navigate within XML documents through elements and attributes.

If you would like to learn XPath first, please visit our XPath Tutorial.

How does it work?

During the transformation process, XSLT uses XPath to define portions of the source document that match one or more predefined templates. Once a match is found, XSLT transforms the matching portion of the source document into the result document.

Correct style sheet declaration

Declare the document as XSL The root element of the style sheet is or .

Note: and are completely synonymous and can be used!

According to the W3C's XSLT standard, the correct way to declare an XSL style sheet is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

or:

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 指向了官方的 W3C XSLT 命名空间。如果您使用此命名空间,就必须包含属性 version="1.0"。

从一个原始的 XML 文档开始

我们现在要把下面这个 XML 文档("cdcatalog.xml")转换为 XHTML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
.
.
.
</catalog>

在 Internet Explorer 和 Firefox 中查看 XML 文件:

打开 XML 文件(通常通过点击某个链接) - XML 文档会以颜色化的代码方式来显示根元素及子元素。点击元素左侧的加号或减号可展开或收缩元素的结构。如需查看原始的XML源文件(不带有加号和减号),请在浏览器菜单中选择“查看页面源代码”。

在 Netscape 6 中查看 XML 文件:

打开 XML 文件,然后在 XML 文件中右击,并选择“查看页面源代码”。XML文档会以颜色化的代码方式来显示根元素及子元素。

在 Opera 7 中查看 XML 文件:

打开 XML 文件,然后在XML文件中右击,选择“框架”/“查看源代码”。XML文档将显示为纯文本。

 

创建 XSL 样式表

然后创建一个带有转换模板的 XSL 样式表("cdcatalog.xsl"):

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


  
  
    

My CD Collection

Title Artist

 

把 XSL 样式表链接到 XML 文档

向 XML 文档("cdcatalog.xml")添加 XSL 样式表引用:

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?><catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
.
.
.
</catalog>

 

 

match 属性用于关联 XML 元素和模板。match 属性也可用来为整个文档定义模板。match 属性的值是 XPath 表达式(举例,match="/" 定义整个文档)。

好了,让我们看一下上一节中的 XSL 文件的简化版本:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
 <html>
 <body>
   <h2 id="My-nbsp-CD-nbsp-Collection">My CD Collection</h2>
   <table border="1">
     <tr bgcolor="#9acd32">
       <th>Title</th>
       <th>Artist</th>
     </tr>
     <tr>
       <td>.</td>
       <td>.</td>
     </tr>
   </table>
 </body>
 </html>
</xsl:template>

</xsl:stylesheet>

代码解释:

由于 XSL 样式表本身也是一个 XML 文档,因此它总是由 XML 声明起始:

<?xml version="1.0" encoding="ISO-8859-1"?>

下一个元素,,定义此文档是一个 XSLT 样式表文档(连同版本号和 XSLT 命名空间属性)。

元素定义了一个模板。而 match="/" 属性则把此模板与 XML 源文档的根相联系。

最后两行定义了模板的结尾,及样式表的结尾。

以上转换的结果类似这样:

 

 

元素

元素用于提取某个选定节点的值,并把值添加到转换的输出流中:

<?xml version="1.0" encoding="ISO-8859-1"?>



 
 
   

My CD Collection

Title Artist

注释:select 属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录。

上面的转换结果类似这样:

 

 

 


元素

元素可用于选取指定的节点集中的每个 XML 元素。

<?xml version="1.0" encoding="ISO-8859-1"?>



  
  
    

My CD Collection

Title Artist

注释:select 属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录。

上面的转换结果类似这样:

 

结果过滤

通过在 元素中添加一个选择属性的判别式,我们也可以过滤从 XML 文件输出的结果。

<xsl:for-each select="catalog/cd[artist=&#39;Bob Dylan&#39;]">

合法的过滤运算符:

  • =  (等于)

  • != (不等于)

  • 363535d26c327c0d81ed29d457003451 (大于)

<?xml version="1.0" encoding="ISO-8859-1"?>



 
  
  

My CD Collection

<xsl:for-each select="catalog/cd[artist=&#39;Bob Dylan&#39;]">
Title Artist

上面的转换结果类似这样:

 


元素用于对结果进行排序。

在何处放置排序信息

如需对结果进行排序,只要简单地在 XSL 文件中的 元素内部添加一个 元素:

<?xml version="1.0" encoding="ISO-8859-1"?>



  
  
    

My CD Collection

Title Artist

注释:select 属性指示需要排序的 XML 元素。

上面的转换结果类似这样:

 

 

 

 


元素

如需放置针对 XML 文件内容的条件测试,请向 XSL 文档添加 元素。

语法

<xsl:if test="expression">
  ...
  ...如果条件成立则输出...
  ...
</xsl:if>

在何处放置 元素

如需添加有条件的测试,请在 XSL 文件中的 元素内部添加 元素:

<?xml version="1.0" encoding="ISO-8859-1"?>


  
  
    

My CD Collection

Title Artist

注释:必选的 test 属性的值包含了需要求值的表达式。

上面的代码仅仅会输出价格高于 10 的 CD 的 title 和 artist 元素。

上面的转换结果类似这样:

 

 

 

 

 


元素

语法

<xsl:choose>
  <xsl:when test="expression">
    ... 输出 ...
  </xsl:when>
  <xsl:otherwise>
    ... 输出 ....
  </xsl:otherwise>
</xsl:choose>

在何处放置选择条件

要插入针对 XML 文件的多重条件测试,请向 XSL 文件添加 以及

<?xml version="1.0" encoding="ISO-8859-1"?>



  
  
    

My CD Collection

Title Artist

上面的代码会在 CD 的价格高于 10 时向 "Artist" 列添加粉色的背景颜色。

上面的转换结果类似这样:

查看此 XML 文件,查看此 XSL 文件,查看结果。

另一个例子

这是另外一个包含两个 元素的例子:

<?xml version="1.0" encoding="ISO-8859-1"?>



  
  
    

My CD Collection

Title Artist

上面的代码会在 CD 的价格高于 10 时向 "Artist" 列添加粉色的背景颜色,并在 CD 的价格高于 9 且低于等于 10 时向 "Artist" 列添加灰色的背景颜色。

上面的转换结果类似这样:

 

 

 


元素

元素可把一个模板应用于当前的元素或者当前元素的子节点。

假如我们向 元素添加一个 select 属性,此元素就会仅仅处理与属性值匹配的子元素。我们可以使用 select 属性来规定子节点被处理的顺序。

请看下面的 XSL 样式表:

<?xml version="1.0" encoding="ISO-8859-1"?>





My CD Collection

Title:
Artist:

 

XSLT 元素

如果您需要有关下列元素的更详细的信息,请点击元素列中的链接。

  • N: 表示最早支持此标签的 Netscape 版本

  • IE: 表示最早支持此标签的 Internet Explorer 版本

注释:在 IE 5 中所支持的元素可能出现非标准的行为,这是由于 IE 5 发布于 XSLT 被确立为正式的 W3C 标准之前。

##apply-importsApply template rules from the imported style sheet. 6.0apply-templatesApply a template to the current element or a child element of the current element. 5.06.0attributeAdd an attribute to an element. 5.06.0attribute-setCreate a named attribute set. 6.06.0call-templateCall a specified template. 6.06.0choose Used in conjunction with and to express multiple conditions test. 5.06.0commentCreate a comment node in the result tree. 5.06.0copyCreate a backup of the current node (without child nodes and attributes). 5.06.0copy-ofCreate a backup of the current node (with child nodes and attributes). 6.06.0decimal-formatDefinition When a number is converted to a string through the format-number() function characters and symbols to be used. 6.0elementCreate an element node in the output document. 5.06.0fallback Specifies a fallback code to run if the processor does not support an XSLT element. 6.0for-eachTraverse each node in the specified node set. 5.06.0if Contains a template that is applied only when a specified condition is true. 5.06.0import is used to pour the contents of one style sheet into another style sheet. 6.06.0includeInclude the contents of one style sheet into another style sheet. 6.06.0keyDeclare a named key. 6.06.0messageWrite a message to the output (for error reporting). 6.06.0namespace-aliasReplace the namespace in the stylesheet with a different namespace in the output . 6.0number Determine the integer position of the current node and format the number. 6.06.0otherwiseSpecifies the default action of the element. 5.06.0outputDefine the format of the output document. 6.06.0paramDeclare a local or global parameter. 6.06.0preserve-space is used to define elements that retain whitespace. 6.06.0processing-instructionGenerate processing instruction node. 5.06.0sort Sort the results. 6.06.0strip-space Defines elements from which whitespace characters should be removed. 6.06.0stylesheet Define the root element of the style sheet. 5.06.0templateThe rule to apply when the specified node is matched. 5.06.0textGenerate text nodes through style sheets. 5.06.0transform Define the root element of the style sheet. 6.06.0value-ofExtract the value of the selected node. 5.06.0variableDeclare local or global variables. 6.06.0whenSpecifies the action of the element. 5.06.0with-paramSpecifies the value of the parameter that needs to be passed into a template. 6.06.0
Element Description IE N

##The above are the crazy XML study notes (11) ------ -----XSLT explanation, for more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement
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
Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

From XML to Readable Content: Demystifying RSS FeedsFrom XML to Readable Content: Demystifying RSS FeedsApr 11, 2025 am 12:03 AM

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

Is There an RSS Alternative Based on JSON?Is There an RSS Alternative Based on JSON?Apr 10, 2025 am 09:31 AM

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools