java生成XSLT转换文件以识别ascii字符
我有一个XSLT,它将html表转换为CSV,定义如下
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="//tr">
<xsl:for-each select="td">
<xsl:if test="position() > 1">,</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
但我现在遇到的问题是,这些表的标记是用ascii码编写的
样本输入:
<table><tr>
<th>Order ID</th>
<th>Item ID</th>
<th>Participant ID</th>
<th>Status</th>
<th>Shipping Provider</th>
<th>Tracking Number</th>
<th>Shipped Date</th>
<th>Shipping Method</th></tr>
<tr>
<td align="center"> Choice_DJ4</td>
<td align="center"> 4</td>
<td align="center"> DXM09902</td>
<td align="center"> Shipped</td>
<td align="center"> USPS</td>
<td align="center"> </td>
<td align="center"> 04/13/2017</td>
<td align="center"> Standard Ground</td>
</tr>
</table>
我的问题是,有没有一种方法可以让xsl文件将ascii码识别为它们的预期字符。 更新: 这是我的java代码
String data = readFile("config/email.xml");
System.out.println("Data: \n" + data);
InputSource is = new InputSource(new StringReader(data));
String configFile = "config/email-xslt.xsl";
File stylesheet = new File(configFile);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(is);
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(stylesource);
Source source = new DOMSource(document);
StringWriter sw = new StringWriter();
Result outputTarget = new StreamResult(sw);
transformer.transform(source, outputTarget);
data = sw.toString();
System.out.println("Output: " + data);
# 1 楼答案
终于解决了这个问题。。。使用
org.apache.commons.lang3.StringEscapeUtils.unescapeJava(str);
我的xsl文件和数据输入(config/email.xml)仍然与OP中的一样,但在传递给xsl转换器之前,我必须修改java代码以取消对这些字符的扫描
# 2 楼答案
使用XSLT3.0,可以使用^{} 加载文本,^{} 取消实体的scape,使用^{} 解析XML字符串