什么是 XSLT?
- XSLT 指 XSL 转换(XSL Transformations)。
- XSLT 是 XSL 中最重要的部分。
- XSLT 可将一种 XML 文档转换为另外一种 XML 文档。
- XSLT 使用 XPath 在 XML 文档中进行导航。
- XPath 是一个 W3C 标准。
XSLT = XSL 转换
XSLT 是 XSL 中最重要的部分。
XSLT 用于将一种 XML 文档转换为另外一种 XML 文档,或者可被浏览器识别的其他类型的文档,比如 HTML 和 XHTML。通常,XSLT 是通过把每个 XML 元素转换为 (X)HTML 元素来完成这项工作的。
通过 XSLT,您可以向或者从输出文件添加或移除元素和属性。您也可重新排列元素,执行测试并决定隐藏或显示哪个元素,等等。
描述转化过程的一种通常的说法是,XSLT 把 XML 源树转换为 XML 结果树。
它如何工作?
在转换过程中,XSLT 使用 XPath 来定义源文档中可匹配一个或多个预定义模板的部分。一旦匹配被找到,XSLT 就会把源文档的匹配部分转换为结果文档。
案例:
将一个xml格式的文件,通过xsl定义的标准,转化这个xml文件。
1.xml文件:
<Items id="Info" text="信息管理" iconCls="menu-info" isPublic="false">
<Items id="Section" iconCls="menu-section-manage" text="公司主页管理">
<Item id="SectionList" iconCls="menu-section-list" text="列表管理"/>
</Items>
<Item id="NewsView" iconCls="menu-news" text="新闻管理">
<Function id="_NewsQuery" text="查看新闻" iconCls="menu-list">
<url>/info/categoryNews.do</url>
<url>/info/treeNewsType.do</url>
</Function>
<Function id="_NewsAdd" text="添加新闻" iconCls="menu-add">
<url>/info/categoryNews.do</url>
<url>/info/treeNewsType.do</url>
<url>/info/saveNews.do</url>
</Function>
<Function id="_NewsEdit" text="编辑新闻" iconCls="btn-edit">
<url>/info/categoryNews.do</url>
<url>/info/treeNewsType.do</url>
<url>/info/saveNews.do</url>
</Function>
<Function id="_NewsDel" text="删除新闻" iconCls="menu-del">
<url>/info/categoryNews.do</url>
<url>/info/treeNewsType.do</url>
<url>/info/multiDelNews.do</url>
</Function>
</Item>
<Item id="NewsCommentView" iconCls="menu-info" text="评论管理">
<Function id="_NewsCommentQuery" text="查看评论" iconCls="menu-list"/>
<Function id="_NewsCommentDel" text="删除评论" iconCls="menu-del">
<url>/info/multiDelNewsComment.do</url>
</Function>
</Item>
<Item id="NoticeView" iconCls="menu-notice" text="公告管理">
<Function id="_NoticeQuery" text="查看公告" iconCls="menu-list"/>
<Function id="_NoticeAdd" text="添加公告" iconCls="menu-add">
<url>/info/saveNotice.do</url>
</Function>
<Function id="_NoticeEdit" text="编辑公告" iconCls="btn-edit">
<url>/info/saveNotice.do</url>
</Function>
<Function id="_NoticeDel" text="删除公告" iconCls="menu-del">
<url>/info/multiDelNotice.do</url>
</Function>
</Item>
</Items>
2.xsl文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="Item"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Item|item" />
<xsl:template match="Function"/>
<xsl:template match="url"/>
</xsl:stylesheet>
转化描述:将Item|item, Function,url节点,过滤掉。
3.java代码:
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
/**
* 通过xsl将xml文件中的节点过滤掉
* @param document 要转化的Document文件
* @param stylesheet xls文件路径
* @return 转化后的Document对象
* @throws Exception
*/
public static Document styleDocument(Document document, String stylesheet)
throws Exception {
// load the transformer using JAXP
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(
stylesheet));
// now lets style the given document
DocumentSource source = new DocumentSource(document);
DocumentResult result = new DocumentResult();
transformer.transform(source, result);
// return the transformed document
Document transformedDoc = result.getDocument();
return transformedDoc;
}