XSL即可扩展的样式表文件。 可以格式化xml的显示,也可以将xml转换成需要的另一种格式。
学习XSL必须熟悉XPath。XSL和XPath一样简单强大,容易学习。
1. XSL既然可以格式化xml的显示样式,我们先来看如何在xml中引用xsl文件
如下代码示例:
只需在xml文件的文档声明后面添加即可
2. XSL的格式
XSL也是一个标准的xml文件,它以xml文档声明开始,根元素必须是xsl:styleshee,同时根元素必须有version属性指定xsl的版本,和xmlns:xsl=” http://www.php.cn/”指定xsl命名空间,如下示例
3. Xsl要点 如下示例xml
<?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="text/xsl" href="pets-templates.xsl"?> <pets> <pig color="blue" weight="100"> <price>100</price> <desc>this is a blue pig</desc> </pig> <cat color="red" weight="9"> <price>80</price> <desc>this is a red cat</desc> </cat> <dog color="green" weight="15"> <price>80</price> <desc>this is a green dog</desc> </dog> <cat color="green" weight="15"> <price>80</price> <desc>this is a green cat</desc> </cat> <dog color="blue" weight="10"> <price>100</price> <desc>this is a blue dog</desc> </dog> <dog color="red" weight="9"> <price>80</price> <desc>this is a red dog</desc> </dog> </pets>
上面的xml在通过xsl格式化之后的显示效果如下:
1) xsl:template定义匹配节点的转换模板,属性match=”xpath expression”用来定义模板匹配的元素
如下定义匹配根节点的模板
<xsl:template match=”/”> </xsl:template>
2) xsl:for-each循环显示select=”xpath expression”选择节点的转换 (类似编程语言中的foreach语句),
如下示例,选择了pets下面的子元素,并循环显示子元素的几点名字:
<xsl:for-each select=”/pets/*”> <xsl:value-of select=”name()”/> </xsl:for-each>
3) xsl:if 元素条件显示节点(类似编程语言中的if语句)注意小于号大于号要分别用67bffed8d39c986beaddeed3e8cd5568替代
<xsl:if test=”@weight < 10”> <i>its weight is less than 10 km</i> </xsl:if>
4) xsl:choose 多分支条件显示 (类似编程语言中的switch语句)
<xsl:choose > <xsl:when test=”name() = ‘pig’”> <i>this is a pig</i> </xsl:when> <xsl:otherwise> <i>this is not a pig</i> </xsl:otherwise> </xsl:choose>
5) xsl:value-of 显示选择节点或者属性的值
选择子节点price
<xsl:value-of select=”pets/*/price”/>
选择属性weight
<xsl:value-of select=”pets/*/@weight”/>
6) xsl:attribute 构造xml节点的属性
用来向节点添加属性,例如:
<font> <xsl:attribute name=”color”><xsl:value-of select=”pets/*/@color”/></xsl:attribute> </font>
将输出
7) xsl:apply-templates 应用模板
如果xml文件结构比较复杂,可以定义多个template,然后使用
请看下面示例xslt文件pets-templates.xsl
完整的示例xsl文件:pets.xsl
<?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> <META http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>lovely pets</title> <style type="text/css"> ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;} li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;} </style> </head> <body> <center> <h1 id="lovely-nbsp-pets">lovely pets</h1> <ul> <xsl:for-each select="pets/*"> <li> <img align="right" alt="XSLT語法—在.net中使用XSLT轉換xml文件的範例程式碼詳解" > <xsl:choose> <xsl:when test="name() = 'dog'"> <xsl:attribute name="src">http://www.php.cn/;/xsl:attribute> </xsl:when> <xsl:when test="name() = 'pig'"> <xsl:attribute name="src">http://www.php.cn/;/xsl:attribute> </xsl:when> <xsl:otherwise> <xsl:attribute name="src">http://www.php.cn/@N00.jpg?1143660418</xsl:attribute> </xsl:otherwise> </xsl:choose> </img> <font> <xsl:attribute name="face">Courier</xsl:attribute> <xsl:attribute name="color"> <xsl:value-of select="@color"/> </xsl:attribute> <xsl:value-of select="name()"/> </font> said: "<xsl:value-of select="desc"/>" weight:<xsl:value-of select="@weight"/> <xsl:if test="@weight < 10"> <p> <i>its weight is less than 10 km</i> </p> </xsl:if> </li> </xsl:for-each> </ul> </center> </body> </html> </xsl:template> </xsl:stylesheet>
完整示例文件 pets-templates.xsl:
<?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> <META http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>lovely pets</title> <style type="text/css"> ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;} li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;} </style> </head> <body> <center> <h1 id="lovely-nbsp-pets">lovely pets</h1> <ul> <xsl:apply-templates select="pets" /> </ul> </center> </body> </html> </xsl:template> <xsl:template match="pets"> <xsl:apply-templates select="dog"></xsl:apply-templates> <xsl:apply-templates select="pig"></xsl:apply-templates> <xsl:apply-templates select="cat"></xsl:apply-templates> </xsl:template> <xsl:template match="dog"> <xsl:for-each select="."> <li> <img align="right" alt="XSLT語法—在.net中使用XSLT轉換xml文件的範例程式碼詳解" > <xsl:attribute name="src">http://www.php.cn/;/xsl:attribute> </img> <font> <xsl:attribute name="face">Courier</xsl:attribute> <xsl:attribute name="color"> <xsl:value-of select="@color"/> </xsl:attribute> dog </font> said: "<xsl:value-of select="desc"/>" weight:<xsl:value-of select="@weight"/> <xsl:if test="@weight < 10"> <p> <i>its weight is less than 10 km</i> </p> </xsl:if> </li> </xsl:for-each> </xsl:template> <xsl:template match="pig"> <xsl:for-each select="."> <li> <img align="right" alt="XSLT語法—在.net中使用XSLT轉換xml文件的範例程式碼詳解" > <xsl:attribute name="src">http://www.php.cn/;/xsl:attribute> </img> <font> <xsl:attribute name="face">Courier</xsl:attribute> <xsl:attribute name="color"> <xsl:value-of select="@color"/> </xsl:attribute> pig </font> said: "<xsl:value-of select="desc"/>" weight:<xsl:value-of select="@weight"/> <xsl:if test="@weight < 10"> <p> <i>its weight is less than 10 km</i> </p> </xsl:if> </li> </xsl:for-each> </xsl:template> <xsl:template match="cat"> <xsl:for-each select="."> <li> <img align="right" alt="XSLT語法—在.net中使用XSLT轉換xml文件的範例程式碼詳解" > <xsl:attribute name="src">http://www.php.cn/@N00.jpg?1143660418</xsl:attribute> </img> <font> <xsl:attribute name="face">Courier</xsl:attribute> <xsl:attribute name="color"> <xsl:value-of select="@color"/> </xsl:attribute> cat </font> said: "<xsl:value-of select="desc"/>" weight:<xsl:value-of select="@weight"/> <xsl:if test="@weight < 10"> <p> <i>its weight is less than 10 km</i> </p> </xsl:if> </li> </xsl:for-each> </xsl:template> </xsl:stylesheet>
在c#.net中使用XslCompiledTransform转换xml文档,XslTransform也可以使用,但是这个类已经被微软标记为过时,最好不要再用了,如下代码示例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; namespace UseXslt { class Program { static void Main(string[] args) { //声明XslTransform类实例 System.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform(); string xsltFile = @"X:\about.net\System.Xml\example\pets.xsl"; using (StreamReader rdr = new StreamReader(xsltFile)) { using (XmlReader xmlRdr = XmlReader.Create(rdr)) { //载入xsl文件 trans.Load(xmlRdr); } } string inputFile = @"X:\about.net\System.Xml\example\pets.xml"; string outputFile = @"X:\about.net\System.Xml\example\pets-out.htm"; //转化源文件输出到输出文件outputFile trans.Transform(inputFile, outputFile); } } }
有一点需要注意,使用XslCompiledTransform转换出来的文件,是一个html格式的,这个类会自动在html的head标签中添加一个未关闭的meta标签 ;微软帮我们想的太多了。
Xslt还可以指定参数,定义变量,有关这些方面请查看相关文档。
以上是XSLT語法—在.net中使用XSLT轉換xml文件的範例程式碼詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Xmlisstillusedduetoitsstructusednature,人類可讀性,以及範圍的addionindererpriseEnvormentments.1)itfacilitatesdataexchangeInsectInsectorlikeFinance(swift)andHealthCare(hl7)和hl7)

RSS文檔的結構包括三個主要元素:1.:根元素,定義RSS版本;2.:包含頻道信息,如標題、鏈接、描述;3.:代表具體的內容條目,包含標題、鏈接、描述等。

RSS文檔是一種簡便的訂閱機制,通過XML文件發佈內容更新。 1.RSS文檔結構由和元素組成,包含多個。 2.使用RSS閱讀器訂閱頻道,並通過解析XML提取信息。 3.高級用法包括使用feedparser庫進行過濾和排序。 4.常見錯誤包括XML解析和編碼問題,調試時需驗證XML格式和編碼。 5.性能優化建議包括緩存RSS文檔和異步解析。

RSS和XML在現代Web中依然重要。 1.RSS用於發布和分發內容,用戶可通過RSS閱讀器訂閱並獲取更新。 2.XML作為標記語言,支持數據存儲和交換,RSS文件基於XML。

RSS可以實現多媒體內容嵌入、條件訂閱、以及性能和安全性優化。 1)通過標籤嵌入多媒體內容,如音頻和視頻。 2)使用XML命名空間實現條件訂閱,允許訂閱者根據特定條件篩選內容。 3)通過CDATA節和XMLSchema優化RSSFeed的性能和安全性,確保穩定性和符合標準。

RSS是一種基於XML的格式,用於發布常更新的數據。作為Web開發者,理解RSS能提升內容聚合和自動化更新能力。通過學習RSS結構、解析和生成方法,你將能自信地處理RSSfeeds,優化Web開發技能。

RSS選擇XML而不是JSON是因為:1)XML的結構化和驗證能力優於JSON,適合RSS複雜數據結構的需求;2)XML當時有廣泛的工具支持;3)RSS早期版本基於XML,已成標準。

RSS是一種基於XML的格式,用於訂閱和閱讀頻繁更新的內容。它的工作原理包括生成和消費兩部分,使用RSS閱讀器可以高效獲取信息。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載
最受歡迎的的開源編輯器