🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
安装:内置扩展,默认为开启,需要在php.ini手动开启 xml是标记语言,用描述和保存数据。 xsl是扩展样式表语言,xsl是将xml变形的,将数据转换成html页面。 **它起始于 XSL,结束于 XSLT、XPath 以及 XSL-FO。** >[info] ## CSS 是 HTML 样式表,而XSL是XML 样式表 ### XSL 包括三部分: XSLT 一种用于转换 XML 文档的语言或其他 XML 文档的语言。 XPath 一种用于在 XML 文档中导航的语言。 XSL-FO 一种用于格式化 XML 文档的语言。 声明 XSL 样式表的正确方法是: ~~~ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> ~~~ 或者: ~~~ <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> ~~~ 例子: cdcatalog.xml ``` <?xml version="1.0" encoding="utf-8"?> <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> //直接访问此文件,浏览器显示为xml //需要向 XML 文档(cdcatalog.xml)添加 XSL 样式表引用:即把 XSL 样式表链接到 XML 文档,然后访问此xml时,自动转换为html了 <?xml version="1.0" encoding="utf-8"?> <?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> ``` cdcatalog.xsl ``` <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> //元素定义了一个模板。而match="/"属性则把此模板与 XML 源文档的根相联系 //<xsl:template match="/">元素内部的内容定义了写到输出结果的 HTML 代码 <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> //<xsl:for-each>元素允许您在 XSLT 中进行循环 <xsl:for-each select="catalog/cd"> //<xsl:for-each>内部添加<xsl:sort />元素用于对结果进行排序 <xsl:sort select="artist"/> <tr> // <xsl:value-of/>元素用于提取某个选定节点的值,并把值添加到转换的输出流中: //select属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录 <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> ``` \<xsl:for-each>内部添加\<xsl:if > 如:下面的代码仅仅会输出价格高于 10 的 CD 的 title 和 artist 元素 ~~~ ... <xsl:for-each select="catalog/cd"> <xsl:if test="price &gt; 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> ... ~~~ XSLT \<xsl:choose> 元素用于结合 \<xsl:when> 和 \<xsl:otherwise> 来表达多重条件测试。 ### 语法 ~~~ <xsl:choose> <xsl:when test="expression"> ... 输出 ... </xsl:when> <xsl:otherwise> ... 输出 .... </xsl:otherwise> </xsl:choose> ~~~ 下面的代码会在 CD 的价格高于 10 时向 "Artist" 列添加粉色的背景颜色 ``` ... <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price &gt; 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> ... ``` \<xsl:apply-templates>元素可把一个模板应用于当前的元素或者当前元素的子节点。 ~~~ <?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> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet> ~~~ **由于并非所有的浏览器都支持 XSLT,另一种解决方案是在服务器上完成 XML 至 XHTML 的转化,这就是xsl扩展的由来** XSLTProcessor类 * [XSLTProcessor :: \_\_ construct](https://www.php.net/manual/en/xsltprocessor.construct.php)—创建一个新的XSLTProcessor对象 * [XSLTProcessor :: getParameter](https://www.php.net/manual/en/xsltprocessor.getparameter.php)—获取参数的值 * [XSLTProcessor :: getSecurityPrefs](https://www.php.net/manual/en/xsltprocessor.getsecurityprefs.php)—获取安全性首选项 * [XSLTProcessor :: hasExsltSupport](https://www.php.net/manual/en/xsltprocessor.hasexsltsupport.php)—确定PHP是否具有EXSLT支持 * [XSLTProcessor :: importStylesheet](https://www.php.net/manual/en/xsltprocessor.importstylesheet.php)—导入样式表 * [XSLTProcessor :: registerPHPFunctions](https://www.php.net/manual/en/xsltprocessor.registerphpfunctions.php)—启用将PHP函数用作XSLT函数的功能 * [XSLTProcessor :: removeParameter](https://www.php.net/manual/en/xsltprocessor.removeparameter.php)—删除参数 * [XSLTProcessor :: setParameter](https://www.php.net/manual/en/xsltprocessor.setparameter.php)—设置参数的值 * [XSLTProcessor :: setProfiling](https://www.php.net/manual/en/xsltprocessor.setprofiling.php)—设置分析输出文件 * [XSLTProcessor :: setSecurityPrefs](https://www.php.net/manual/en/xsltprocessor.setsecurityprefs.php)—设置安全性首选项 * [XSLTProcessor :: transformToDoc](https://www.php.net/manual/en/xsltprocessor.transformtodoc.php)—转换为DOMDocument * [XSLTProcessor :: transformToUri](https://www.php.net/manual/en/xsltprocessor.transformtouri.php)—转换为URI * [XSLTProcessor :: transformToXml](https://www.php.net/manual/en/xsltprocessor.transformtoxml.php)—转换为XML ## 例子 接受xml与xsl字符串 ``` functiontransform($xmldata,$xsldata) { $xslt= newXSLTProcessor(); $xslt->importStylesheet(new SimpleXMLElement($xsldata)); return $xslt->transformToXml(new SimpleXMLElement($xmldata)); } ``` 接受xml字符串 xsl以文件导入 ``` $xmldata="<?xml version=\"1.0\" encoding=\"utf-8\"?> <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>"; $xslt = new xsltProcessor; $xslt->importStyleSheet(@DomDocument::load('cdcatalog.xsl')); print $xslt->transformToXML(@DomDocument::loadXML($xmldata)); ```