处理xslt包含中的非法URI字符

时间:2016-12-07 14:29:43

标签: java xslt uri xsl-fo invalid-characters

在xsl转换中,我有一个xslt文件,其中包含一些其他xslt。问题是这些xslt的URI包含非法字符,特别是' ##'。 xslt看起来像这样:

<xsl:include href="/appdm/tomcat/webapps/sentys##1.0.0/WEB-INF/classes/xslt/release_java/xslt/gen.xslt" />

当我尝试实例化java Transformer时,我收到错误:

javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: org.xml.sax.SAXException: org.apache.xml.utils.URI$MalformedURIException: Fragment contains invalid character:#

这是java代码:

public String xslTransform2String(String sXml, String sXslt) throws Exception {
    String sResult = null;
    try {
        Source oStrSource = createStringSource(sXml);

        DocumentBuilderFactory oDocFactory = DocumentBuilderFactory.newInstance();
        oDocFactory.setNamespaceAware(true);

        //sXslt is the xslt content with the inclusions 
        //<xsl:include href="/appdm/tomcat/webapps/sentys##1.0.0/WEB-INF/classes/xslt/release_java/xslt/gen.xslt" />"
        Document oDocXslt = oDocFactory.newDocumentBuilder().parse(new InputSource(new StringReader(sXslt)));
        Source oXsltSource = new DOMSource(oDocXslt);

        StringWriter oStrOut = new StringWriter();
        Result oTransRes = createStringResult(oStrOut);
        Transformer oTrans = createXsltTransformer(oXsltSource);

        oTrans.transform(oStrSource, oTransRes);
        sResult = oStrOut.toString();
    } catch (Exception oEx) {
        throw new BddException(oEx, XmlProvider.ERR_XSLT, null);
    }
    return sResult;
}

private Transformer createXsltTransformer(Source oXsltSource) throws Exception {
    Transformer transformer = getXsltTransformerFactory().newTransformer(
            oXsltSource);
    ErrorListener errorListener = new DefaultErrorListener();
    transformer.setErrorListener(errorListener);

    return transformer;
}

有没有办法可以使用相对路径而不是绝对路径?

谢谢

1 个答案:

答案 0 :(得分:1)

要避免MalformedURIException,请将wrapPromise(fun) : Promise<any>{ try{ var value = fun() return Promise.resolve(value); } catch (err){ console.error(err); return Promise.reject(err); } } wrapPromise(action1).then() 中的第二个或#替换为%23

请参阅https://stackoverflow.com/a/5007362/4092205