下面,将同样使用唱片店的例子来帮助大家更好的理解XSLTRANFORM函数的实现。每个XML文档里包含了多个CD的内容,每张CD包含了CD的名字,唱片作者等等内容,一个简单XML的例子如下:
<?xml version="1.0"?> <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文档做XSLT 变换的用途在于抽取 XML 记录中的信息并创建可在浏览器中查看的 HTML Web 页面。为此,应使用以下 XSLT 样式表,该样式表存储在数据库中
<?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> <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 select="catalog/cd"> <tr> <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>
为了存放XML数据和其对应的XSLT变换表,首先建立一个表XML_TEST,表的定义如下:
CREATE TABLE XML_TEST (DOCID INTEGER, XML_DOC XML, XSL_DOC CLOB(1M));
在这个表里,XML_DOC放XML文件,XSL_DOC放其对应的XSLT样式表。
将XML和对应的XSLT插入表中,
INSERT INTO XML_TEST VALUES
(1, ‘<?xml version="1.0"?> <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 version="1.0" encoding="UTF-8"?> <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 align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <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>');