简体   繁体   English

将setParameter与节点集一起使用时,会引发异常“从java.lang.String到节点集的无效转换”

[英]When using setParameter with a node-set, it raises exception “Invalid conversion from java.lang.String to node-set”

I am trying to transform an XML by invoking an XSLT from my java code. 我试图通过从我的java代码调用XSLT来转换XML。 I am facing an issue in passing a XML string as parameter to the XSLT. 我在将XML字符串作为参数传递给XSLT时遇到了问题。 This causes an exception: Invalid conversion from 'java.lang.String' to 'node-set'. 这会导致异常:从“java.lang.String”到“node-set”的转换无效。

This is the method to invoke the XSLT: 这是调用XSLT的方法:

Transformer l_transformer
=TransformerFactory.newInstance().newTransformer(xslt_file_path);
l_transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
l_transformer.setParameter("collateralDoc", param_xmlString);

StringWriter l_writer = new StringWriter();
StringReader l_reader = new StringReader(inputXML);

Source l_in = new StreamSource(l_reader);
Result l_out = new StreamResult(l_writer);

l_transformer.transform(l_in, l_out);

After searching for some solutions I even tried creating a Document object from the param XML string and passed the Document object to the setParameter object. 在搜索了一些解决方案之后,我甚至尝试从param XML字符串创建一个Document对象,并将Document对象传递给setParameter对象。 Then I got this exception: 然后我得到了这个例外:

Invalid conversion from 'com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl' to 'node-set'. 从'com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl'到'node-set'的转换无效。

The XSLT code which processes this input XML param and the line which throws the exception: <xsl:variable name="infoList" select="$paramXML/a/b"/> 处理此输入XML参数的XSLT代码和抛出异常的行: <xsl:variable name="infoList" select="$paramXML/a/b"/>

The param XML which I need to pass as param looks like this: 我需要作为参数传递的参数XML如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
    <b>
        <c>
            <d>blah</d>
            <e>blah</e>
        </c>
        <f>
            <g>blah</g>
            <h>blah</h>
        </f>
    </b>
</a>

Please help me in resolving the issue. 请帮我解决这个问题。

This is an old question, though I think it still deserves an answer. 这是一个老问题,但我认为它仍然值得回答。

The default implementation of the JDK uses Xalan-J. JDK的默认实现使用Xalan-J。 Back in 2005, an issue was raised in Jira against Xalan 2.7 for asking to support passing nodes or document objects. 早在2005年, Jira针对Xalan 2.7提出了一个问题 ,要求支持传递节点或文档对象。 In the passed 10 years, this issue has not yet been resolved, even though the comments suggest that it is "easy enough to do". 在过去的10年中,这个问题尚未得到解决,尽管评论表明它“很容易做到”。

The above issue suggests, however, that it is possible to pass a DOM Tree. 但是,上述问题表明可以传递DOM树。 In fact, the following appears to work: 事实上,以下似乎有效:

String doc = "<root>Hello world!</root>";
transformer.setParameter("mydoc", new StreamSource(new StringReader(doc)));

If for some reason you cannot switch to a more capable XSLT processor, like Saxon, you may also consider another relative easy workaround , I quote: 如果出于某种原因你无法切换到更强大的XSLT处理器,比如Saxon,你也可以考虑另一种相对简单的解决方法 ,我引用:

One workaround would be to use the document function inside your stylesheet with a URI of your choosing. 一种解决方法是使用样式表中的文档函数和您选择的URI。 Then install a URIResolver on the Transformer. 然后在Transformer上安装URIResolver The URIResolver.resolve method should be implemented to look for that URI and return a DOMSource like the one you've described above. 应该实现URIResolver.resolve方法来查找该URI并返回一个类似于上面描述的DOMSource

In addition, one could override the setParameter method to register a Node with your URIResolver to make its use orthogonal. 此外,可以覆盖setParameter方法以使用URIResolver注册Node以使其正确使用。

A few alternative workarounds have been given in this answer on SO on the same subject . 关于同一主题的SO的答案中给出了一些替代解决方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 // 在节点集中迭代后代时出现意外结果 - Unexpected result when iterating descendants in node-set using // 创建自定义XPath函数以修改Java中的节点集 - Creating a custom XPath function to modify a node-set in Java 为什么不能在XPath-Expression中使用通过exsl:node-set / set:distinct检索的节点的值? - Why can't I use values of nodes I retrieve by using exsl:node-set/set:distinct in an XPath-Expression? java.lang.illegalargumentexception 无法将 java.lang.string 字段设置为 java.lang.String - java.lang.illegalargumentexception cannot set java.lang.string field to java.lang.String 无法将java.lang.String字段设置为java.lang.String - Can not set java.lang.String field to java.lang.String 禁用从char *到java.lang.String的转换 - Disable conversion from char* to java.lang.String 当另一个BeanPostProcessor时,Spring bean java.lang.String设置为null,而不是提供的字符串 - Spring bean java.lang.String gets set to null instead of the provided string when another BeanPostProcessor 无法设置JSTL用户变量而不是java.lang.String - Cannot set JSTL User variable instead of java.lang.String 从空的sharedpreference中获取json数组时,出现java.lang.String无法转换为JSONObject的异常 - When retrieving json array from empty sharedpreference, java.lang.String cannot be converted to JSONObject exception comes 值的java.lang.String布局无效 - Invalid layout of java.lang.String at value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM