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

黄舟
Release: 2017-02-21 14:44:50
Original
1396 people have browsed it


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">
Copy after login

or:

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

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>
Copy after login

在 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
Copy after login

把 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>
Copy after login

元素

元素用于构建模板。

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>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>
Copy after login

代码解释:

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

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

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

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

元素内部的内容定义了写到输出结果的 HTML 代码。

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

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

元素

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

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



 
 
   

My CD Collection

Title Artist
Copy after login

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

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


元素

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

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



  
  
    

My CD Collection

Title Artist
Copy after login

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

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

结果过滤

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

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

合法的过滤运算符:

  • = (等于)

  • != (不等于)

  • < (小于)

  • > (大于)

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



 
  
  

My CD Collection

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

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


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

在何处放置排序信息

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

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



  
  
    

My CD Collection

Title Artist
Copy after login

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

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


元素

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

语法

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

在何处放置 元素

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

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


  
  
    

My CD Collection

Title Artist
Copy after login

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

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

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


元素

语法

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

在何处放置选择条件

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

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



  
  
    

My CD Collection

Title Artist
Copy after login

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

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

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

另一个例子

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

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



  
  
    

My CD Collection

Title Artist
Copy after login

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

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


元素

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

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

请看下面的 XSL 样式表:

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





My CD Collection

Title:
Artist:
Copy after login

 

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)!

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template