如何引用 CLASSPATH 之外的 XSD 来验证我的 XML?
作为 XML 转换(使用 XSL)的一部分,我正在为架构位置创建一个属性,以便可以验证转换的结果。但是,当我尝试将其作为独立测试运行时,我希望能够为模式位置设置适当的属性/值以使其脱离我的 CLASSPATH。
经过一番谷歌搜索后,我尝试了各种 schemaLocation 属性,最新的尝试是:
..
<xsl:attribute name="xsi:external-noNamespaceSchemaLocation">
<xsl:value-of select="$schemaLocation"/>
</xsl:attribute>
..
转换后的结果是:
<?xml version="1.0" encoding="UTF-8"?>
<emrException
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:external-noNamespaceSchemaLocation="emrException_1_0.xsd">
..
<!-- more well-formed XML -->
</emrException>
当验证器运行时,我仔细检查了我的 emrException_1_0.xsd 文件是否明确位于 CLASSPATH 中。它是一个 DOM 验证器,我使用的是 Xerces 2.9.1/Xalan 2.7.1,我得到的结果是:
Caused by: org.xml.sax.SAXParseException:
cvc-elt.1: Cannot find the declaration of element 'emrException'.
但是,当我切换到使用从文件系统或外部 URL 读取 XSD 文件的架构位置时,然后验证器运行起来很有趣。
如何从我的 CLASSPATH 中读取 XSD?
As part of my XML transform (using XSL), I'm creating an attribute for the schema location so the the result of the transform can be validated. However, as I'm trying to run this as a self-contained test I want to be able to set the appropriate attribute/value for the schema location to be off my CLASSPATH.
After a bit of Googling I've tried various schemaLocation attributes, the latest attempt being:
..
<xsl:attribute name="xsi:external-noNamespaceSchemaLocation">
<xsl:value-of select="$schemaLocation"/>
</xsl:attribute>
..
Which after the transform results in:
<?xml version="1.0" encoding="UTF-8"?>
<emrException
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:external-noNamespaceSchemaLocation="emrException_1_0.xsd">
..
<!-- more well-formed XML -->
</emrException>
I've double checked that my emrException_1_0.xsd file is clearly in the CLASSPATH when the validator runs. It's a DOM validator and I'm using Xerces 2.9.1/Xalan 2.7.1, the result I get is:
Caused by: org.xml.sax.SAXParseException:
cvc-elt.1: Cannot find the declaration of element 'emrException'.
However, when I swap to using a schema location where the XSD file is read off the file system or an external URL, then the validator runs fun.
How can I get the XSD to be read off my CLASSPATH?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是实现您自己的 LSResourceResolver 和 LSInput,以便可以按照您想要的方式解析传入的 systemId。
One way is to implement your own LSResourceResolver and LSInput, so that the systemId that is passed in can be resolved however you would want.