我看到我们有多个url作为此属性的值,如spring:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
Run Code Online (Sandbox Code Playgroud)
为什么需要它以及它用于什么?Spring会进入网址并验证吗?xmlns和xsi:schemaLocation有什么区别?
我需要使用给定的XSD文件验证XML文件.如果验证正常,我只需要方法返回true,否则返回false.
我一直在试图弄清楚如何检查XML文件的语法,确保所有标签都关闭,没有随机字符等...我现在关心的是确保没有破坏的XML文件.
我一直在看一些这样的SO帖子......
...但我意识到我不想验证XML文件的结构; 我不想验证XML Schema(XSD)...我只是想检查XML语法并确定它是否正确.
我正在使用Java 5 javax.xml.validation.Validator来验证XML文件.我已经为一个仅使用导入的模式完成了它,一切正常.现在我正在尝试使用另一个使用import和一个include的模式进行验证.我遇到的问题是主模式中的元素被忽略,验证表明它无法找到它们的声明.
以下是我构建Schema的方法:
InputStream includeInputStream = getClass().getClassLoader().getResource("include.xsd").openStream();
InputStream importInputStream = getClass().getClassLoader().getResource("import.xsd").openStream();
InputStream mainInputStream = getClass().getClassLoader().getResource("main.xsd").openStream();
Source[] sourceSchema = new SAXSource[]{includeInputStream , importInputStream,
mainInputStream };
Schema schema = factory.newSchema(sourceSchema);
Run Code Online (Sandbox Code Playgroud)
现在这里是main.xsd中声明的摘录
<xsd:schema xmlns="http://schema.omg.org/spec/BPMN/2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:import="http://www.foo.com/import" targetNamespace="http://main/namespace" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.foo.com/import" schemaLocation="import.xsd"/>
<xsd:include schemaLocation="include.xsd"/>
<xsd:element name="element" type="tElement"/>
<...>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)
如果我在main.xsd中复制我包含的XSD的代码,它可以正常工作.如果我不这样做,验证不会找到"元素"的声明.
是否可以使用StAX(特别是woodstox)使用换行符和制表符格式化输出xml,即以下列形式:
<element1> <element2> someData </element2> </element1>
代替:
<element1><element2>someData</element2></element1>
如果在woodstox中无法做到这一点,是否有其他轻量级库可以做到这一点?
当我尝试针对Java中的XSD验证XML文件时(请参阅此示例),XSD文件中给出的正则表达式与java中的正则表达式之间存在一些不兼容性.
如果有一个像"[ab-]"XSD中的正则表达式(意思是任何字符"a","b"或" - ",则java会抱怨表达式中存在语法错误.
这是自2005年3月28日以来的已知错误,请参阅Sun bug数据库.
我该怎么做才能解决这个错误?到目前为止,我尝试通过替换"[ab-]"by 来"纠正"XSD文件"[ab\-]",但有时这不是一个选项.
如果你也遇到这个bug的问题,请在Sun bug数据库中投票!
注意:我提到的测试项目可以下载:
git clone https://github.com/mperdikeas/so-spring-di-appcontext-schemalocation.git
..并使用'ant run'运行.
我理解'XML命名空间名称仅用作不透明标识符,并不打算用作URI(维基百科).我还"理解"XML模式位置旨在提供关于模式文档的实际位置的提示,并且是提示,而不是在实践中使用(w3.org).考虑到这一点,我一直在通过修改applicationContext.xml来试验一个简单的Spring DI应用程序(在简单的J2SE设置中使用).这是起始版本:
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:p = "http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="atm"/>
<context:property-placeholder location="classpath:META-INF/spring/atm.properties"/>
<bean id="soapTransport_" class="atm.SoapATMTransport" p:retries="${transport.retries}"/>
Run Code Online (Sandbox Code Playgroud)
当我执行'sudo ifconfig eth0 down'时,项目运行得很好,这与运行时没有从schemaLocations中获取任何内容一致.但是,当我通过向每对中的第二个URL添加一个简单的下划线来修改schemaLocations时,我收到了以下投诉:
[java] org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [META-INF/spring/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 100; cvc-elt.1: Cannot find the declaration of element 'beans'.
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:771)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:221) …Run Code Online (Sandbox Code Playgroud) 使用JDK 6将XML文件加载到DOM中.必须针对XSD验证XML文件.XSD文件位置因运行环境而异.确保XML可以针对XSD进行验证,无论目录结构如何,都需要目录解析器.一旦验证了XML,就可以对其进行转换.
我的理解是DocumentBuilderFactory可用于配置此类验证.这是通过使用带有XMLCatalogResolver的DocumentBuilder来查找与XML文件关联的XSD文件(以及任何包含的文件)来实现的.
有关使用目录派生的XSD验证XML文档的问题包括:
这些问题和答案中的大多数引用了硬编码的XSD文件路径,或者使用SAX执行验证,或者与DTD相关,或者需要JDOM依赖项,或者没有转换.
没有规范的解决方案描述如何使用JAXP DOM进行XSD验证的XML目录,随后通过XSLT进行转换.有一个号码的片段,但编译和运行(JDK 6下)没有完整的,独立的例子.
我发布了一个答案似乎在技术上有效,但过于冗长.
什么是规范方式(使用JDK 1.6库)来验证和转换XML文档?这是一种可能的算法:
我创建了一个XML模式(foo.xsd)并用于xjc为JAXB创建我的绑定类.假设根元素是collection,我正在写N个document对象,它们是复杂的类型.
因为我打算写出大型XML文件,我使用Stax写出collection根元素,而JAXB 使用编写文档子树Marshaller.marshal(JAXBElement, XMLEventWriter).这是jaxb非官方用户指南推荐的方法.
我的问题是,如何在编组时验证XML?如果我将模式绑定到JAXB marshaller(使用Marshaller.setSchema()),我会得到验证错误,因为我只是编组一个子树(它抱怨它没有看到collection根元素").我想我真正想做的是将模式绑定到的Stax XMLEventWriter或类似的东西.
对这种整体方法的任何评论都会有所帮助.基本上我希望能够使用JAXB来编组和解组大型XML文档而不会耗尽内存,所以如果有更好的方法可以让我知道.