XSL学习笔记一:概念、作用与使用方法
一、什么是XSL
所谓XSL,就是可扩展样式语言(extensible stylesheet language,简称XSL)(参考自:http://en.wikipedia.org/wiki/XSL)。其中包含XSLT,即可扩展样式表转换语言(Extensible Stylesheet Language Transformations,简称XSLT)是我最感兴趣的一部分,所以以下凡是称XSL的地方都是指XSLT。
二、XSL的作用
XSL是一种处理XML文件的方式。顾名思义,就是一种类似css(层叠样式表)的语言,只不过处理的输入是XML而非HTML(输入格式比HTML要严格),而且一般可以依靠一些XSLT处理器(例如xalan和saxon)脱离浏览器直接运行。例如可以把以下XML内容:<?xml version="1.0"?><doc>Hello</doc>转换为<?xml version="1.0" encoding="UTF-8"?><out>Hello</out>。
XSL的常见用途如下:
(1) 网页渲染(类似css)
* 用Xalan在IE中浏览XML文件
http://www.infosecurity.org.cn/content/appsec/xmlbasic/use_xalan_ie_bran_xml_file.htm
(2) XML与其它数据格式的互换
* A CSV to XML converter in XSLT 2.0
http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html
* xml2json-xslt
http://code.google.com/p/xml2json-xslt/
(3) 代码生成(模板)
* 利用工具生成数据解析层代码
http://www.xncat.com/?p=1554
* FCG
http://www.dehats.com/drupal/?q=node/7
http://www.flashas.net/html/Flex/20081229/3830.html
* AMF3用のクラスを自動生成する
http://www.d-project.com/flex/013_XML2Src/
(4) 文档生成和翻译
* jquery-api-zh-cn
http://code.google.com/p/jquery-api-zh-cn/
https://github.com/shawphy/jquery-api
其中我最感兴趣的是代码生成方面。如果不限于XML的输入,还有其它可用于代码生成的方案,例如:
* GNU m4
http://www.linuxforum.net/forum/files/591080-GNU_m4_info_page_cn.txt
* StringTemplate
http://www.stringtemplate.org/
还有很多类似的方法(有些微语言和语言支持占位符、注入和宏展开功能)
三、使用XSL/XSLT处理器进行转换、处理或代码生成
1. 使用xalan
官方网站:
* http://xml.apache.org/xalan-j/
2. 使用saxon
参考资料:
* http://saxon.sourceforge.net/
3. 使用Kernow
参考资料:
* http://kernowforsaxon.sourceforge.net/
4. 使用Apache Ant
参考资料:
* http://ant.apache.org/manual/Tasks/style.html
5. 使用Maven XML Plugin
参考资料:
* http://mojo.codehaus.org/xml-maven-plugin/
* http://mojo.codehaus.org/xslt-maven-plugin/plugin-info.html
(TODO)