java以编程方式创建xslt文件
我知道我可以通过使用java中的DOM
api以编程方式创建xml
文件,如下所示:
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("map");
document.appendChild(rootElement);
Element em = document.createElement("string");
em.setAttribute("name", "FirstName");
....
但是,是否有任何API来构建xslt
树?(比如像Dom这样的api)
我需要这样的东西:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
<xsl:template match="root">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page">
<fo:region-body margin="1in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:external-graphic width="100pt" height="100pt" content-width="50pt" content-height="50pt" src="images/shopping-cart_100.jpg"/>
</fo:block>
<fo:block>Good Morning, <xsl:value-of select="name" />!</fo:block>
<fo:block>
<fo:table>
<fo:table-body>
<fo:table-row>
<fo:table-cell border="solid 1px black" text-align="center" font-weight="bold">
<fo:block>
以及:
<xsl:for-each select="./friend">
<fo:table-row>
<fo:table-cell border="solid 1px black" text-align="center">
<fo:block>
<xsl:value-of select="position()" />
</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black" text-align="center">
<fo:block>
<xsl:value-of select="name" />
</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black" text-align="center">
提前谢谢
# 1 楼答案
创建XSLT的方式与创建XML文件的方式相同,因为XSLT是XML文件
然而,如果您必须大量处理XML/XSLT转换,ApacheCook 3有一个非常轻量级的XML/XSLT管道系统,可以用作库,而不是手动处理所有XML dom内容和XSLT转换
# 2 楼答案
DOM是创建XML的一种相当麻烦的方法
有一种更好的方法——使用XSLT
XML越复杂,使用XSLT而不是DOM来创建它的收益就越大
没有理由不能使用XSLT来创建XSLT(甚至还有一个特殊的声明xsl:namespace alias,使其更简单一些——搜索xsl:namespace alias将显示其使用示例)
# 3 楼答案
因为XSLT也是XML,所以您可以简单地使用相同的策略:
等等
但它不是很优雅。你应该使用一个库或框架,你应该很容易在谷歌上找到一个