我面臨的問題是關於甚至在XSLT轉換後保留特殊字符。我的源XHTML文件包含幾個特殊字符,如
,—
,’
;它在XSLT轉換時被忽略。使用XSLT保留特殊字符作爲輸出
如果手動將特殊字符的值更改爲其相應的Unicode表示形式,則字符將保留在輸出中。
例如,將
更改爲 
,它會在輸出中產生空間。請參考下面的一些示例文件:
來源XHTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:text="http://giraffe.wkle.com/text" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<div class="section" id="section_1">
<p id="para_1" class="para">Content of paragraph—1.</p>
<p id="para_2" class="para">Content of paragraph—2.</p>
</div>
</body>
</html>
XSL模板:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[local-name()='p']/text()">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
預期輸出:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:text="http://giraffe.wkle.com/text">
<body>
<div class="section" id="section_1">
<p class="para" id="para_1">Content of paragraph—1.</p>
<p class="para" id="para_2">Content of paragraph—2.</p>
</div>
</body>
</html>
實際輸出:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:text="http://giraffe.wkle.com/text">
<body>
<div class="section" id="section_1">
<p class="para" id="para_1">Contentofparagraph1.</p>
<p class="para" id="para_2">Contentofparagraph2.</p>
</div>
</body>
</html>
限制:
- 我沒有獲得修改源XHTML內容或其DTD。
- XSLT的版本是1.0。
請讓我知道是否有任何方法可以使用它們的Unicode值轉換特殊字符並將它們保留在我的輸出XML文檔中。
更新:
我使用這段Java代碼轉換調用:
public class XSLTUtil {
public static String processXHTML(String sourceFileName, String outputXhtml, String xslFilePath) throws ParserConfigurationException, SAXException, IOException, TransformerException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = factory.newDocumentBuilder();
Document doc = docbuilder.parse(new FileInputStream(sourceFileName));
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream(outputXhtml);
fis = new FileInputStream(xslFilePath);
TransformerFactory transformfactory = TransformerFactory.newInstance();
Templates xsl = transformfactory.newTemplates(new StreamSource(fis));
Transformer transformer = xsl.newTransformer();
transformer.transform(new DOMSource(doc.getDocumentElement()),new StreamResult(fos));
return outputXhtml;
} finally {
if(fos != null) {
fos.close();
}
if(fis != null) {
fis.close();
}
}
}
public static void main(String args[]){
String sourceFileName = "C:\\source.xhtml";
String outputXhtml = "C:\\output.xhtml";
String xslFilePath = "C:\\xslTemplate.xsl";
String result = "-1";
try {
result = processXHTML(sourceFileName, outputXhtml, xslFilePath);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
System.out.println("Result : "+ result);
}
}
您在哪個平臺上使用哪個XSLT處理器,您究竟如何運行轉換? –
我正在使用XSLT1.0,Apache Xalan處理器。我正在使用Java調用轉換。請檢查上面的更新。 –