XSLT 实例
XSLT 实例
XSLT是什么
XSLT是一种XML样式表语言,用于将XML文档转换为另一种XML文档,HTML文档或其他文本/文本格式,如PDF,CSV等。XSLT允许您选择和转换源XML文档中的元素,属性和文本,并将它们映射到目标XML文档的新元素,属性和文本。
XSLT语法
XSLT使用XML DTD(文档类型定义)定义格式。XSLT文件包含一个根元素,该元素必须是<xsl:stylesheet>
或<xsl:transform>
标记之一。此外,XSLT通过两种不同格式的模板来完成工作——匹配模板和定位模板。匹配模板处理解析器所访问的每个匹配节点,并将其转换为输出格式。而定位模板处理整个输出文档。
XSLT 实例
考虑概述:
- 遍历XML
- 控制XML输出
- 使用模板
- IF/ELSE语句
遍历XML
首先,我们将使用一个简单的XML文件作为示例。它包含一些star元素,每个star元素都有一个title和一个category元素。
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<star>
<title>A Guide to the Stars</title>
<category>Astronomy</category>
</star>
<star>
<title>The History of Space Exploration</title>
<category>Astronomy</category>
</star>
<star>
<title>The Universe: A Journey through Time and Space</title>
<category>Astronomy</category>
</star>
<star>
<title>Introduction to Astrophysics</title>
<category>Astronomy</category>
</star>
<star>
<title>The Illustrated Guide to the Solar System</title>
<category>Astronomy</category>
</star>
</catalog>
现在,我们将遍历XML文档中的所有节点,并将它们输出为文本。此处我们使用XSLT及HTML来展现,但在XML文档中run XSLT时,仅会输出我们在XSLT中定义的部分。
下面是遍历XML的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet [
<!ELEMENT stylesheet (text)>
<!ELEMENT text (#PCDATA)>
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>XSLT Output</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Category</th>
</tr>
<xsl:for-each select="catalog/star">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="category"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
输出文本:
XSLT Output
Title Category
A Guide to the Stars Astronomy
The History of Space Exploration Astronomy
The Universe: A Journey through Time and Space Astronomy
Introduction to Astrophysics Astronomy
The Illustrated Guide to the Solar System Astronomy
控制XML输出
在这个实例中,我们将更改XSLT以输出XML文档。由于XML允许嵌套元素,这是有可能的。
下面是控制XML输出的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet [
<!ELEMENT stylesheet (text)>
<!ELEMENT catalog (star+)>
<!ELEMENT star (title,category)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT category (#PCDATA)>
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<catalog>
<xsl:for-each select="catalog/star">
<book>
<title>
<xsl:value-of select="title"/>
</title>
<category>
<xsl:value-of select="category"/>
</category>
</book>
</xsl:for-each>
</catalog>
</xsl:template>
</xsl:stylesheet>
输出:
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<book>
<title>A Guide to the Stars</title>
<category>Astronomy</category>
</book>
<book>
<title>The History of Space Exploration</title>
<category>Astronomy</category>
</book>
<book>
<title>The Universe: A Journey through Time and Space</title>
<category>Astronomy</category>
</book>
<book>
<title>Introduction to Astrophysics</title>
<category>Astronomy</category>
</book>
<book>
<title>The Illustrated Guide to the Solar System</title>
<category>Astronomy</category>
</book>
</catalog>
使用模板
为方便起见,在此示例中,我们添加了一些额外的Book元素,并使用它作为带有单个模板的XSLT的节点。此XSLT将向XML文档中的每个匹配元素添加Price元素。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet [
<!ELEMENT stylesheet (text)>
<!ELEMENT catalog (book*)>
<!ELEMENT book (title,category)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT category (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<catalog>
<xsl:apply-templates/>
</catalog>
</xsl:template>
<xsl:template match="book">
<xsl:element name="book">
<xsl:apply-templates select="title"/>
<xsl:element name="Price">14.99</xsl:element>
<xsl:apply-templates select="category"/>
</xsl:element>
</xsl:template>
<xsl:template match="title">
<xsl:element name="name">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="category">
<xsl:element name="type">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
上面的XSLT将遍历XML文档中的每个<book>
元素并将它们转换为输出文档中的相应节点。
IF/ELSE语句
在此示例中,我们再次将使用XML和XSLT。但是,这个XML文档包含一个属性price,该属性的值高于10的元素将被忽略。因此,我们将使用一个IF/ELSE语句。
略有不同的DGPML文档:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE books [
<!ELEMENT books (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ATTLIST book price CDATA #REQUIRED>
]>
<books>
<book price="9.99">
<title>Learning XML</title>
<author>Erik T. Ray</author>
</book>
<book price="14.99">
<title>Professional XML</title>
<author>Bill Evjen, Kent Sharkey, Thiru Thangarathinam, Michael Kay, Alessandro Vernet, Sam Wouters, Kerry A. Nietz, J. Balasubramanian, Joe Fawcett</author>
</book>
<book price="11.99">
<title>Beginning XML with C# 2008: From Novice to Professional</title>
<author>Bipin Joshi</author>
</book>
</books>
下面是带有IF/ELSE语句的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE stylesheet [
<!ELEMENT stylesheet (text)>
<!ELEMENT books (book*)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ATTLIST book price CDATA #REQUIRED>
<!ELEMENT text (#PCDATA)>
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<books>
<xsl:for-each select="books/book">
<xsl:if test="@price <= 10.00">
<book>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="author"/>
</book>
</xsl:if>
<xsl:if test="@price > 10.00">
<xsl:comment>
<xsl:text>This book was excluded because the price was over $10</xsl:text>
</xsl:comment>
</xsl:if>
</xsl:for-each>
</books>
</xsl:template>
<xsl:template match="title">
<name>
<xsl:value-of select="."/>
</name>
</xsl:template>
<xsl:template match="author">
<writer>
<xsl:value-of select="."/>
</writer>
</xsl:template>
</xsl:stylesheet>
输出:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<name>Learning XML</name>
<writer>Erik T. Ray</writer>
</book>
<!--This book was excluded because the price was over $10-->
<book>
<name>Beginning XML with C# 2008: From Novice to Professional</name>
<writer>Bipin Joshi</writer>
</book>
</books>
结论
XSLT允许开发人员定制XML文档的输出格式。这些样式表可以在整个文档中定义经过匹配的模板,控制XML输出并使用IF/ELSE语句。开发人员可以根据需要使用XML为XSLT创建模板和DTD。