XML 到 HTML 使用 XSLT,输入和输出字符串而不是文件
XML to HTML using XSLT, input and output strings rather than files
我已经学习了一些 XSLT 教程,它们都在谈论使用输入和输出文件;在我的具体情况下,我将 XML 和 XSL 全部作为字符串 - 我在运行时从数据库中读取它们,并且它会根据我正在使用的记录而变化。
有没有办法进行某种转换以操纵 StreamSource,为其提供实际的字符串而不是文件名?
我说的是:
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsl));
StreamSource src = new StreamSource(new FileInputStream(xml));
其中 xml 和 xls 都是字符串,包含实际的 xml 或实际的 xsl。
使用 StringReader and use the StreamSource constructor that takes the reader as arguments.
这是一个 MCVE:
package transformStrings;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
public class transformFromStrings {
public static void main(String[] args) throws TransformerException
{
StringReader xmlReader = new StringReader("<x/>");
StreamSource xmlSource = new StreamSource(xmlReader);
StringReader xslReader = new StringReader("<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"/\">OMG!</xsl:template></xsl:stylesheet>");
StreamSource xslSource = new StreamSource(xslReader);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xslSource);
StringWriter resultWriter = new StringWriter();
transformer.transform(xmlSource, new javax.xml.transform.stream.StreamResult(resultWriter));
System.out.println(resultWriter.toString());
}
}
我已经学习了一些 XSLT 教程,它们都在谈论使用输入和输出文件;在我的具体情况下,我将 XML 和 XSL 全部作为字符串 - 我在运行时从数据库中读取它们,并且它会根据我正在使用的记录而变化。
有没有办法进行某种转换以操纵 StreamSource,为其提供实际的字符串而不是文件名?
我说的是:
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsl));
StreamSource src = new StreamSource(new FileInputStream(xml));
其中 xml 和 xls 都是字符串,包含实际的 xml 或实际的 xsl。
使用 StringReader and use the StreamSource constructor that takes the reader as arguments.
这是一个 MCVE:
package transformStrings;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
public class transformFromStrings {
public static void main(String[] args) throws TransformerException
{
StringReader xmlReader = new StringReader("<x/>");
StreamSource xmlSource = new StreamSource(xmlReader);
StringReader xslReader = new StringReader("<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"/\">OMG!</xsl:template></xsl:stylesheet>");
StreamSource xslSource = new StreamSource(xslReader);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xslSource);
StringWriter resultWriter = new StringWriter();
transformer.transform(xmlSource, new javax.xml.transform.stream.StreamResult(resultWriter));
System.out.println(resultWriter.toString());
}
}