考虑一个XML文档,它像下面的多个模式一样开始(这不是一个特定于Spring的问题;这只是一个方便的XML文档):
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
我想验证文档,但我事先不知道文档作者将使用哪些名称空间.我相信文档作者,所以我愿意下载任意模式URL.我如何实现我的验证器?
我知道我可以使用DocumentBuilderFactory实例指定我的模式我调用setAttribute(“http://java.sun.com/xml/jaxp/properties/schemaSource”,new String [] {…})但我不知道知道架构URL,直到解析文档.
当然,我可以在解析文档之后自己提取XSD URL,然后通过验证器运行它,如上所述指定“http://java.sun.com/xml/jaxp/properties/schemaSource”,但肯定已经有了自动执行此操作的实现?
最佳答案
我没有证实这一点,但你可能会发现Use JAXP Validation API to create a validator and validate input from a DOM which contains inline schemas and multiple validation roots很有用.
特别是,
factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
factory.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_ID, honourAllSchemaLocations);
相关文章
转载注明原文:java – 针对多个任意模式验证XML - 代码日志