我目前正在开发一个使用 XSL 转换从 XML 生成 HTML 的项目。在输入字段上,我必须设置一些属性。
样本:
<input name="/my/xpath/to/node"
class="{/my/xpath/to/node/@isValid}"
value="{/my/xpath/to/node}" />
这很愚蠢,因为我必须编写相同的 XPath 3 次......我的想法是为 xsl 文件配备某种后处理器,这样我就可以编写:
<input xpath="/my/xpath/to/node" />
我正在使用类似的东西来转换我的xml
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.dom4j.Document;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
public class Foo {
public 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;
}
}
我希望我可以从 Document 对象中创建一个 Transformer 对象。但它似乎必须是文件路径 - 至少我找不到直接使用 Document 的方法。
任何人都知道实现我想要的方法吗?
谢谢