我想根据此模式(即在 zip 中)验证 XML 文件;它导入另外两个 XSD 文件。
<import namespace="http://www.w3.org/2000/09/xmldsig#"
schemaLocation="xmldsig-core-schema.xsd"/>
<import namespace="http://www.w3.org/2001/04/xmlenc#"
schemaLocation="xenc-schema.xsd"/>
Run Code Online (Sandbox Code Playgroud)
这两个文件也可以在这里找到:
在验证时,我收到此错误:
Src-resolve: Cannot Resolve The Name 'xenc:EncryptedData' To A(n) 'element Declaration' Component.
Run Code Online (Sandbox Code Playgroud)
我的验证/解组代码如下所示(使用 moxy 作为 JAXB 提供程序):
jaxbContext = JAXBContext.newInstance(type.getRequestType().getPackage().getName());
Unmarshaller um = jaxbContext.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new StreamSource(this.getClass().getResourceAsStream("/xsd/" + type.getXsdName())));
um.setSchema(schema);
root = um.unmarshal(new StreamSource(new ByteArrayInputStream(xmlData)), type.getRequestType());
Run Code Online (Sandbox Code Playgroud)
在您询问类型有什么作用之前:我编写了可以从http://www.forum-datenaustausch.ch/导入所有类型发票的代码。但是 4.3 及更高版本使用了两个额外的架构文件。如何验证 XML 文件?
我已经在互联网上搜索了 24 小时,但找不到有效的解决方案。
我有一个包含导入行的架构文件:
<xsd:import namespace="http://www.w3.org/2000/09/xmldsig#"
schemaLocation=
"http://www.w3.org/TR/2001/PR-xmldsig-core-20010820/xmldsig-core-schema.xsd"/>
Run Code Online (Sandbox Code Playgroud)
这是我验证 Xml 的代码:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, @"C:\TEMP\myschema.xsd");
XmlReader xmlReader = XmlReader.Create(new StringReader(document.InnerXml), settings);
while (xmlReader.Read()) { }
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到:The 'http://www.w3.org/2000/09/xmldsig#:Signature' element is not declared.
如果我将代码(根据搜索建议)更改为:
settings.ValidationType = ValidationType.DTD;
settings.DtdProcessing = DtdProcessing.Parse;
Run Code Online (Sandbox Code Playgroud)
然后我没有收到错误,但验证不起作用,因为我故意插入了一个无效值来测试验证是否正常工作。
我尝试添加直接导入的架构:
settings.Schemas.Add(null, @"C:\TEMP\xmldsig-core-schema.xsd");
Run Code Online (Sandbox Code Playgroud)
但收到错误:For security reasons DTD is prohibited in this XML document. To enable DTD processing...
我已经尝试了我能想到的以及搜索建议的 XmlReaderSettings 设置的所有组合。
我现在真的很困惑。
我指的是下面的代码,用于从 XSD 进行 XML 验证,从功能的角度来看,它似乎工作正常。
var schemas = new XmlSchemaSet();
schemas.Add("http://microsoft.com/HealthCare/HL7/2X", xsdFilePath);
Boolean result = true;
xdocXml.Validate(schemas, (sender, e) =>
{
result = false;
});
Run Code Online (Sandbox Code Playgroud)
现在,如您所见,我必须明确指定架构名称,即使此信息本身包含在 XSD 中。有没有办法从 XSD 中提取该信息,这样我就不必特别指定?
我有一个应用程序,只需将XML文件作为BLOB上传到数据库.我知道有几种方法可以使用XML验证DocumentBuilderFactory,DocumentBuilder然后解析它等,但我所说的数据很大,输入XML文件中出现异常的可能性较小.
但是,只是为了确保语法正确,有没有办法捕获文件中的异常而不实际解析每个文件?
我知道这个相关问题,我理解我的问题与答案相矛盾,但以下XML文件完全符合以下XMLSchema
我的XML数据:
<?xml version="1.0" encoding="utf-8"?>
<myElements>
<el1>bla</el1>
<el1>bla</el1>
<el1>blabla</el1>
<el2>bgt</el2>
<el2>pel</el2>
<el3>sdf</el3>
</myElements>
Run Code Online (Sandbox Code Playgroud)
我的XMLSchema:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myElements">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="el1" />
<xs:element name="el2" />
<xs:element name="el3" />
<xs:element name="el4" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
W3学校和其他消息来源说:
XML Schema choice元素只允许声明中包含的一个元素出现在contains元素中.
在我看来,这意味着我的XML数据应该只接受其中一个:
但是,如果您尝试将我的xml数据验证到我的xmlschema,它是有效的,我不明白为什么.
我是否完全误解了文档?
我使用的验证器是否松散且非标准?如果是的话,什么是一个好的验证器,为什么有人会在我描述的情况下使用选择?(是的,我确实遇到过这个)
在我的 XSD 中,我尝试使用alternative标签。由于某些原因,我在 IDE (PHPStorm) 中遇到此错误:
无效内容以元素“xs:alternative”开头...
XSD
<xs:complexType name="tableType">
<xs:sequence>
<xs:element type="columnType" name="column" maxOccurs="unbounded" minOccurs="0"/>
<xs:element type="keyType" name="key" maxOccurs="unbounded" minOccurs="0">
<xs:alternative test="@type='index'" type="keyIndexType"/>
<xs:alternative test="@type='unique'" type="KeyUniqueType"/>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="required"/>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)
我发现我不应该添加更多东西来使用 1.1 xsd 版本,但是我需要一些东西来支持alternative标签吗?
我想验证以下数组中的数据:
input_array = array(
"boy"=> array("boy_id"=>1),
"first_name=>"First Name",
"last_name"=>"Last Name"
);
Run Code Online (Sandbox Code Playgroud)
input_array 内部的第一个索引可以替换为 girls 数组,如下所示
"girl"=>array("girl_id"=>2)
Run Code Online (Sandbox Code Playgroud)
我想创建 xsd 来验证信息,如下所示:
<xs:element name="xml">
<xs:complexType>
<xs:all>
<xs:element ref="boy" minOccurs="0"/>
<xs:element ref="girl" minOccurs="0"/>
<xs:element ref="first_name"/>
<xs:element ref="last_name"/>
</xs:all>
</xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
问题 - 我想确保男孩或女孩信息存在,first_name并且last_name将永远存在,我如何将它们(女孩,男孩)作为选择或选项。我更愿意使用xs:all这样元素顺序不应该成为问题。
我推荐使用此链接,以便尝试在内部使用选择xs:all,但无法使其工作。我将不胜感激任何回应。谢谢。
我正在尝试运行一个junit测试用例来测试applicationcontext是否已初始化并抛出以下错误.它是与xml还是java版本相关的东西.我该如何解决?
org.springframework.beans.factory.BeanDefinitionStoreException: Parser configuration exception parsing XML from class path resource [tasospring-online-context.xml]; nested exception is javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [org.apache.crimson.jaxp.DocumentBuilderFactoryImpl@82701e] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.roger.tas.manager.util.OnlineSpringBootStrapper.initialize(OnlineSpringBootStrapper.java:23) …Run Code Online (Sandbox Code Playgroud)