ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# XSL(内置) 安装:内置扩展,默认为开启,需要在php.ini手动开启 xml是标记语言,用描述和保存数据。 xsl是扩展样式表语言,xsl是将xml变形的,将数据转换成html页面。 **它起始于 XSL,结束于 XSLT、XPath 以及 XSL-FO。** > ## CSS 是 HTML 样式表,而XSL是XML 样式表 ### XSL 包括三部分: XSLT 一种用于转换 XML 文档的语言或其他 XML 文档的语言。 XPath 一种用于在 XML 文档中导航的语言。 XSL-FO 一种用于格式化 XML 文档的语言。 声明 XSL 样式表的正确方法是: ``` <pre class="calibre10">``` <span class="token1"><</span>xsl<span class="token3">:</span>stylesheet version<span class="token1">=</span><span class="token2">"1.0"</span> xmlns<span class="token3">:</span>xsl<span class="token1">=</span><span class="token2">"http://www.w3.org/1999/XSL/Transform"</span><span class="token1">></span> ``` ``` 或者: ``` <pre class="calibre10">``` <span class="token1"><</span>xsl<span class="token3">:</span>transform version<span class="token1">=</span><span class="token2">"1.0"</span> xmlns<span class="token3">:</span>xsl<span class="token1">=</span><span class="token2">"http://www.w3.org/1999/XSL/Transform"</span><span class="token1">></span> ``` ``` 例子: cdcatalog.xml ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>xml version<span class="token1">=</span><span class="token2">"1.0"</span> encoding<span class="token1">=</span><span class="token2">"utf-8"</span><span class="token1">?</span><span class="token1">></span> <span class="token1"><</span>catalog<span class="token1">></span> <span class="token1"><</span>cd<span class="token1">></span> <span class="token1"><</span>title<span class="token1">></span>Empire Burlesque<span class="token1"><</span><span class="token1">/</span>title<span class="token1">></span> <span class="token1"><</span>artist<span class="token1">></span>Bob Dylan<span class="token1"><</span><span class="token1">/</span>artist<span class="token1">></span> <span class="token1"><</span>country<span class="token1">></span>USA<span class="token1"><</span><span class="token1">/</span>country<span class="token1">></span> <span class="token1"><</span>company<span class="token1">></span>Columbia<span class="token1"><</span><span class="token1">/</span>company<span class="token1">></span> <span class="token1"><</span>price<span class="token1">></span><span class="token6">10.90</span><span class="token1"><</span><span class="token1">/</span>price<span class="token1">></span> <span class="token1"><</span>year<span class="token1">></span><span class="token6">1985</span><span class="token1"><</span><span class="token1">/</span>year<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>cd<span class="token1">></span> <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> <span class="token1"><</span><span class="token1">/</span>catalog<span class="token1">></span> <span class="token">//直接访问此文件,浏览器显示为xml</span> <span class="token">//需要向 XML 文档(cdcatalog.xml)添加 XSL 样式表引用:即把 XSL 样式表链接到 XML 文档,然后访问此xml时,自动转换为html了</span> <span class="token1"><</span><span class="token1">?</span>xml version<span class="token1">=</span><span class="token2">"1.0"</span> encoding<span class="token1">=</span><span class="token2">"utf-8"</span><span class="token1">?</span><span class="token1">></span> <span class="token1"><</span><span class="token1">?</span>xml<span class="token1">-</span>stylesheet type<span class="token1">=</span><span class="token2">"text/xsl"</span> href<span class="token1">=</span><span class="token2">"cdcatalog.xsl"</span><span class="token1">?</span><span class="token1">></span> <span class="token1"><</span>catalog<span class="token1">></span> <span class="token1"><</span>cd<span class="token1">></span> <span class="token1"><</span>title<span class="token1">></span>Empire Burlesque<span class="token1"><</span><span class="token1">/</span>title<span class="token1">></span> <span class="token1"><</span>artist<span class="token1">></span>Bob Dylan<span class="token1"><</span><span class="token1">/</span>artist<span class="token1">></span> <span class="token1"><</span>country<span class="token1">></span>USA<span class="token1"><</span><span class="token1">/</span>country<span class="token1">></span> <span class="token1"><</span>company<span class="token1">></span>Columbia<span class="token1"><</span><span class="token1">/</span>company<span class="token1">></span> <span class="token1"><</span>price<span class="token1">></span><span class="token6">10.90</span><span class="token1"><</span><span class="token1">/</span>price<span class="token1">></span> <span class="token1"><</span>year<span class="token1">></span><span class="token6">1985</span><span class="token1"><</span><span class="token1">/</span>year<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>cd<span class="token1">></span> <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> <span class="token1"><</span><span class="token1">/</span>catalog<span class="token1">></span> ``` ``` cdcatalog.xsl ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>xml version<span class="token1">=</span><span class="token2">"1.0"</span> encoding<span class="token1">=</span><span class="token2">"utf-8"</span><span class="token1">?</span><span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>stylesheet version<span class="token1">=</span><span class="token2">"1.0"</span> xmlns<span class="token3">:</span>xsl<span class="token1">=</span><span class="token2">"http://www.w3.org/1999/XSL/Transform"</span><span class="token1">></span> <span class="token">//元素定义了一个模板。而match="/"属性则把此模板与 XML 源文档的根相联系</span> <span class="token">//<xsl:template match="/">元素内部的内容定义了写到输出结果的 HTML 代码</span> <span class="token1"><</span>xsl<span class="token3">:</span>template match<span class="token1">=</span><span class="token2">"/"</span><span class="token1">></span> <span class="token1"><</span>html<span class="token1">></span> <span class="token1"><</span>body<span class="token1">></span> <span class="token1"><</span>h2<span class="token1">></span>My CD Collection<span class="token1"><</span><span class="token1">/</span>h2<span class="token1">></span> <span class="token1"><</span>table border<span class="token1">=</span><span class="token2">"1"</span><span class="token1">></span> <span class="token1"><</span>tr bgcolor<span class="token1">=</span><span class="token2">"#9acd32"</span><span class="token1">></span> <span class="token1"><</span>th align<span class="token1">=</span><span class="token2">"left"</span><span class="token1">></span>Title<span class="token1"><</span><span class="token1">/</span>th<span class="token1">></span> <span class="token1"><</span>th align<span class="token1">=</span><span class="token2">"left"</span><span class="token1">></span>Artist<span class="token1"><</span><span class="token1">/</span>th<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>tr<span class="token1">></span> <span class="token">//<xsl:for-each>元素允许您在 XSLT 中进行循环</span> <span class="token1"><</span>xsl<span class="token3">:</span><span class="token5">for</span><span class="token1">-</span>each select<span class="token1">=</span><span class="token2">"catalog/cd"</span><span class="token1">></span> <span class="token">//<xsl:for-each>内部添加<xsl:sort />元素用于对结果进行排序</span> <span class="token1"><</span>xsl<span class="token3">:</span>sort select<span class="token1">=</span><span class="token2">"artist"</span><span class="token1">/</span><span class="token1">></span> <span class="token1"><</span>tr<span class="token1">></span> <span class="token">// <xsl:value-of/>元素用于提取某个选定节点的值,并把值添加到转换的输出流中:</span> <span class="token">//select属性的值是一个 XPath 表达式。此表达式的工作方式类似于定位某个文件系统,在其中正斜杠可选择子目录</span> <span class="token1"><</span>td<span class="token1">></span><span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"title"</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>td<span class="token1">></span> <span class="token1"><</span>td<span class="token1">></span><span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"artist"</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>td<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>tr<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span><span class="token5">for</span><span class="token1">-</span>each<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>table<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>body<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>html<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>template<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>stylesheet<span class="token1">></span> ``` ``` <xsl:for-each>内部添加<xsl:if > 如:下面的代码仅仅会输出价格高于 10 的 CD 的 title 和 artist 元素 ``` <pre class="calibre10">``` <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> <span class="token1"><</span>xsl<span class="token3">:</span><span class="token5">for</span><span class="token1">-</span>each select<span class="token1">=</span><span class="token2">"catalog/cd"</span><span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span><span class="token5">if</span> test<span class="token1">=</span><span class="token2">"price &gt; 10"</span><span class="token1">></span> <span class="token1"><</span>tr<span class="token1">></span> <span class="token1"><</span>td<span class="token1">></span><span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"title"</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>td<span class="token1">></span> <span class="token1"><</span>td<span class="token1">></span><span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"artist"</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>td<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>tr<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span><span class="token5">if</span><span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span><span class="token5">for</span><span class="token1">-</span>each<span class="token1">></span> <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> ``` ``` XSLT <xsl:choose> 元素用于结合 <xsl:when> 和 <xsl:otherwise> 来表达多重条件测试。 ### 语法 ``` <pre class="calibre10">``` <span class="token1"><</span>xsl<span class="token3">:</span>choose<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>when test<span class="token1">=</span><span class="token2">"expression"</span><span class="token1">></span> <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> 输出 <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>when<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>otherwise<span class="token1">></span> <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> 输出 <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>otherwise<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>choose<span class="token1">></span> ``` ``` 下面的代码会在 CD 的价格高于 10 时向 "Artist" 列添加粉色的背景颜色 ``` <pre class="calibre10">``` <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> <span class="token1"><</span>xsl<span class="token3">:</span><span class="token5">for</span><span class="token1">-</span>each select<span class="token1">=</span><span class="token2">"catalog/cd"</span><span class="token1">></span> <span class="token1"><</span>tr<span class="token1">></span> <span class="token1"><</span>td<span class="token1">></span><span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"title"</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>td<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>choose<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>when test<span class="token1">=</span><span class="token2">"price &gt; 10"</span><span class="token1">></span> <span class="token1"><</span>td bgcolor<span class="token1">=</span><span class="token2">"#ff00ff"</span><span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"artist"</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>td<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>when<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>otherwise<span class="token1">></span> <span class="token1"><</span>td<span class="token1">></span><span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"artist"</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>td<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>otherwise<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>choose<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>tr<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span><span class="token5">for</span><span class="token1">-</span>each<span class="token1">></span> <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span> ``` ``` <xsl:apply-templates>元素可把一个模板应用于当前的元素或者当前元素的子节点。 ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>xml version<span class="token1">=</span><span class="token2">"1.0"</span> encoding<span class="token1">=</span><span class="token2">"ISO-8859-1"</span><span class="token1">?</span><span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>stylesheet version<span class="token1">=</span><span class="token2">"1.0"</span> xmlns<span class="token3">:</span>xsl<span class="token1">=</span><span class="token2">"http://www.w3.org/1999/XSL/Transform"</span><span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>template match<span class="token1">=</span><span class="token2">"/"</span><span class="token1">></span> <span class="token1"><</span>html<span class="token1">></span> <span class="token1"><</span>body<span class="token1">></span> <span class="token1"><</span>h2<span class="token1">></span>My CD Collection<span class="token1"><</span><span class="token1">/</span>h2<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>apply<span class="token1">-</span>templates<span class="token1">/</span><span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>body<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>html<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>template<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>template match<span class="token1">=</span><span class="token2">"cd"</span><span class="token1">></span> <span class="token1"><</span>p<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>apply<span class="token1">-</span>templates select<span class="token1">=</span><span class="token2">"title"</span><span class="token1">/</span><span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>apply<span class="token1">-</span>templates select<span class="token1">=</span><span class="token2">"artist"</span><span class="token1">/</span><span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>p<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>template<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>template match<span class="token1">=</span><span class="token2">"title"</span><span class="token1">></span> Title<span class="token3">:</span> <span class="token1"><</span>span style<span class="token1">=</span><span class="token2">"color:#ff0000"</span><span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"."</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>span<span class="token1">></span> <span class="token1"><</span>br <span class="token1">/</span><span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>template<span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>template match<span class="token1">=</span><span class="token2">"artist"</span><span class="token1">></span> Artist<span class="token3">:</span> <span class="token1"><</span>span style<span class="token1">=</span><span class="token2">"color:#00ff00"</span><span class="token1">></span> <span class="token1"><</span>xsl<span class="token3">:</span>value<span class="token1">-</span>of select<span class="token1">=</span><span class="token2">"."</span><span class="token1">/</span><span class="token1">></span><span class="token1"><</span><span class="token1">/</span>span<span class="token1">></span> <span class="token1"><</span>br <span class="token1">/</span><span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>template<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>xsl<span class="token3">:</span>stylesheet<span class="token1">></span> ``` ``` **由于并非所有的浏览器都支持 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字符串 ``` <pre class="calibre10">``` <span class="token4">functiontransform</span><span class="token3">(</span>$xmldata<span class="token3">,</span>$xsldata<span class="token3">)</span> <span class="token3">{</span> $xslt<span class="token1">=</span> <span class="token4">newXSLTProcessor</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $xslt<span class="token1">-</span><span class="token1">></span><span class="token4">importStylesheet</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">SimpleXMLElement</span><span class="token3">(</span>$xsldata<span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">return</span> $xslt<span class="token1">-</span><span class="token1">></span><span class="token4">transformToXml</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">SimpleXMLElement</span><span class="token3">(</span>$xmldata<span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> ``` ``` 接受xml字符串 xsl以文件导入 ``` <pre class="calibre17">``` $xmldata<span class="token1">=</span>"<span class="token1"><</span><span class="token1">?</span>xml version<span class="token1">=</span>\"<span class="token6">1.0</span>\" encoding<span class="token1">=</span>\"utf<span class="token1">-</span><span class="token6">8</span>\"<span class="token1">?</span><span class="token1">></span> <span class="token1"><</span>catalog<span class="token1">></span> <span class="token1"><</span>cd<span class="token1">></span> <span class="token1"><</span>title<span class="token1">></span>Empire Burlesque<span class="token1"><</span><span class="token1">/</span>title<span class="token1">></span> <span class="token1"><</span>artist<span class="token1">></span>Bob Dylan<span class="token1"><</span><span class="token1">/</span>artist<span class="token1">></span> <span class="token1"><</span>country<span class="token1">></span>USA<span class="token1"><</span><span class="token1">/</span>country<span class="token1">></span> <span class="token1"><</span>company<span class="token1">></span>Columbia<span class="token1"><</span><span class="token1">/</span>company<span class="token1">></span> <span class="token1"><</span>price<span class="token1">></span><span class="token6">10.90</span><span class="token1"><</span><span class="token1">/</span>price<span class="token1">></span> <span class="token1"><</span>year<span class="token1">></span><span class="token6">1985</span><span class="token1"><</span><span class="token1">/</span>year<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>cd<span class="token1">></span> <span class="token1"><</span><span class="token1">/</span>catalog<span class="token1">></span>"<span class="token3">;</span> $xslt <span class="token1">=</span> <span class="token5">new</span> <span class="token4">xsltProcessor</span><span class="token3">;</span> $xslt<span class="token1">-</span><span class="token1">></span><span class="token4">importStyleSheet</span><span class="token3">(</span>@DomDocument<span class="token3">:</span><span class="token3">:</span><span class="token4">load</span><span class="token3">(</span><span class="token2">'cdcatalog.xsl'</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> print $xslt<span class="token1">-</span><span class="token1">></span><span class="token4">transformToXML</span><span class="token3">(</span>@DomDocument<span class="token3">:</span><span class="token3">:</span><span class="token4">loadXML</span><span class="token3">(</span>$xmldata<span class="token3">)</span><span class="token3">)</span><span class="token3">;</span> ``` ```