作者:JSHGDF5649 | 来源:互联网 | 2023-10-10 11:58
HowcanIpreserveentityreferenceswhentransformingXMLwithXSLT(2.0)?Withalloftheprocesso
How can I preserve entity references when transforming XML with XSLT (2.0)? With all of the processors I've tried, the entity gets resolved by default. I can use xsl:character-map
to handle the character entities, but what about text entities?
在使用XSLT(2.0)转换XML时,如何保存实体引用?使用我尝试过的所有处理器,实体将在默认情况下得到解析。我可以使用xsl:character-map来处理字符实体,但是文本实体呢?
For example, this XML:
例如,这个XML:
]>
Hello &so;!
&question;
transformed with the following XSLT:
使用以下XSLT进行转换:
produces the following output:
产生以下输出:
Hello stackoverflow!
How can I preserve the entity reference when transforming with XSLT??
The output should look like the input (minus the doctype declaration for now):
输出应该类似于输入(现在减去doctype声明):
Hello &so;!
&question;
I'm hoping that I don't have to pre-process the input by replacing all ampersands with &
(like &question;
) and then post-process the output by replacing all &
with &
.
我希望我不需要用& & &取代所有的符号来预处理输入。(像问题一样),然后用替换掉所有的输出来处理后的输出。&。
Maybe this is processor specific? I'm using Saxon 9.
也许这是处理器特有的?我使用Saxon 9。
Thanks!
谢谢!
5 个解决方案
0
You can keep EntityReference nodes in the document by using a DOM LS parser with "entities" parameter set to true. http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/DOMConfiguration.html
您可以通过使用“entity”参数设置为true的DOM LS解析器来保持文档中的EntityReference节点。http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/DOMConfiguration.html
The specification says the default value is true but depending on the parser, it could be false, be aware of that.
规范说默认值是true,但是根据解析器的不同,它可能是false,请注意这一点。
To load Xerces :
加载Xerces:
DOMImplementationLS domImpl = new org.apache.xerces.dom.CoreDOMImplementationImpl();
You can use registry as below too but personnaly, I would rather hardcode the implementation I want as above:
你也可以使用注册表如下,但就个人而言,我宁愿硬编码实现我想要的如上:
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImpl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
Then, to load your document :
然后,加载您的文档:
// XML parser with XSD schema
LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");
DOMConfiguration cOnfig= parser.getDomConfig();
config.setParameter("entities", true);
LSInput input = impl.createLSInput();
Document lDoc = parser.parse(your XML stream);
Then, your XML entities are not expanded in the DOM.
然后,XML实体不会在DOM中展开。
Then, because SAXON does not handle entities not expanded ('Unsupported node type in DOM! 5' error), you can not use net.sf.saxon.xpath.XPathFactoryImpl
, you have to set the default XPathFactory of Xerces with XPathFactory.newInstance()
然后,因为SAXON不会处理未展开的实体(“DOM中不支持的节点类型!”5’error),不能使用net.sf.saxon.xpath。XPathFactoryImpl,您必须使用XPathFactory. newinstance()设置Xerces的默认XPathFactory。