如何将 XSLT 转换为带有编码数据的纯文本?
How to convert XSLT to plain text with encoded data?
我正在尝试将 XSLT 文件转换为纯文本。问题是 XSLT 文件具有编码值,例如 & 是 &
并且 Transformer 工厂输出编码值。除了使用 XML 解码器然后隐藏为纯文本之外,还有更好的方法吗?
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(XSLT_Source));
transformer.setOutputProperty("media-type", "text/plain");
transformer.transform(new StreamSource(XSLT_Source), new StreamResult(new FileOutputStream(outputTrager)));
如果您可以编辑样式表,请确保它使用 <xsl:output method="text"/>
。如果您想使用 JAXP API 以编程方式设置它,请参阅 https://docs.oracle.com/javase/8/docs/api/javax/xml/transform/OutputKeys.html#METHOD,您需要设置
transformer.setOutputProperty(javax.xml.transform.OutputKeys.METHOD, "text");
我正在尝试将 XSLT 文件转换为纯文本。问题是 XSLT 文件具有编码值,例如 & 是 &
并且 Transformer 工厂输出编码值。除了使用 XML 解码器然后隐藏为纯文本之外,还有更好的方法吗?
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(XSLT_Source));
transformer.setOutputProperty("media-type", "text/plain");
transformer.transform(new StreamSource(XSLT_Source), new StreamResult(new FileOutputStream(outputTrager)));
如果您可以编辑样式表,请确保它使用 <xsl:output method="text"/>
。如果您想使用 JAXP API 以编程方式设置它,请参阅 https://docs.oracle.com/javase/8/docs/api/javax/xml/transform/OutputKeys.html#METHOD,您需要设置
transformer.setOutputProperty(javax.xml.transform.OutputKeys.METHOD, "text");