xml-validation

Validate an XML against an XSD in Java / Getting a hold of the schemaLocation

江枫思渺然 提交于 2019-11-29 04:53:47
How can one validate an XML file using an XSD in Java? We don't know the schema in advance. I would like to be able to get the schemaLocation , download the XSD, cache it and then perform the actual validation. The problem is, that with javax.xml.parsers.DocumentBuilder / DocumentBuilderFactory classes I can't seem to be able to get a hold of the schemaLocation in advance. What's the trick for this? Which classes should I look into? Perhaps there's a more suitable API I can use? The whole problem is that we need to validate dynamically, without (necessarily) having the XSDs locally. How could

How to validate an XML document?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 04:22:02
My C#/.NET application reads XML files that are manually edited by the users. The allowed elements and tags are described in the application's documentation. I'm using LINQ to extract data from the XML file. Before extracting data from the XML file, I'd like to validate it to see if it has the expected structure. If not, it would be nice to have information about what is wrong so that I can give some feeback to the user. What's the simplest way to do this in C#? CoderHawk You can validate xml files against XSD. First you have to create Xml Schema Definition file. See example use XML Schema

Getting started with XSD validation with .NET

别说谁变了你拦得住时间么 提交于 2019-11-28 20:35:03
Here is my first attempt at validating XML with XSD. The XML file to be validated: <?xml version="1.0" encoding="utf-8" ?> <config xmlns="Schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="config.xsd"> <levelVariant> <filePath>SampleVariant</filePath> </levelVariant> <levelVariant> <filePath>LegendaryMode</filePath> </levelVariant> <levelVariant> <filePath>AmazingMode</filePath> </levelVariant> </config> The XSD, located in "Schemas/config.xsd" relative to the XML file to be validated: <?xml version="1.0" encoding="utf-8" ?> <xs:schema xmlns:xs="http:

Possible to validate xml against xsd using code at runtime?

耗尽温柔 提交于 2019-11-28 19:42:26
I have xml files that I read in at runtime, is is possible to validate the xml against an xsd file at runtime? using c# Try this: public void ValidateXmlDocument( XmlReader documentToValidate, string schemaPath) { XmlSchema schema; using (var schemaReader = XmlReader.Create(schemaPath)) { schema = XmlSchema.Read(schemaReader, ValidationEventHandler); } var schemas = new XmlSchemaSet(); schemas.Add(schema); var settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = schemas; settings.ValidationFlags = XmlSchemaValidationFlags

XML (.xsd) feed validation against a schema

人走茶凉 提交于 2019-11-28 19:04:50
问题 I have a XML file and I have a XML schema. I want to validate the file against that schema and check if it adheres to that. I am using python but am open to any language for that matter if there is no such useful library in python. What would be my best options here? I would worry about the how fast I can get this up and running. 回答1: Definitely lxml. Define an XMLParser with a predefined schema, load the the file fromstring() and catch any XML Schema errors: from lxml import etree def

Validating XML with XSDs … but still allow extensibility

蹲街弑〆低调 提交于 2019-11-28 17:28:51
Maybe it's me, but it appears that if you have an XSD <?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="User"> <xs:complexType> <xs:sequence> <xs:element name="GivenName" /> <xs:element name="SurName" /> </xs:sequence> <xs:attribute name="ID" type="xs:unsignedByte" use="required" /> </xs:complexType> </xs:element> </xs:schema> that defines the schema for this document <?xml version="1.0" encoding="utf-8" ?> <User ID="1"> <GivenName></GivenName> <SurName></SurName> <

Setting minOccurs and maxOccurs in XSD based on value of other XML field?

醉酒当歌 提交于 2019-11-28 12:59:41
I have an XSD to validate an XML file. The structure is as follows: <root> <child> <size>2</size> <childElement>Element 1</childElement> <childElement>Element 2</childElement> </child> </root> The number of childElement s is dependent on the size provided i.e. if size was set as 3, not more than 3 childElement s can be added. I have tried using xs:alternative but it does not seem to work: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="child" minOccurs="1" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs

Enforce further constraints on xsd:any?

白昼怎懂夜的黑 提交于 2019-11-28 10:36:00
问题 I have an XSD 1.0 sequence that defines a set of elements, some of which may be optional but none of which can occur more than once, and which also ends with an <xsd:any minOccurs="0" maxOccurs="unbounded" namespace="##any" processContents="lax"/> tag in order to enable users to add their own data elements in the easiest possible way when programmatically exporting conforming XML (i.e. ideally without requiring them to define their own namespace/XSD). When processing XML that users generate,

XSD allowing both simpleType and complexType content for same element?

隐身守侯 提交于 2019-11-28 10:04:40
问题 I have a situation where I have different XMLs that will have different types of properties. Sometimes the element HEADER could have just a node or some XMLs could have elements within the HEADER node and values inside. Example 1 ( HEADER with just text): <Details HeaderLabel="DETAILS"> <HEADER Label="Header">2.5%</HEADER> </Details> Example 2 ( HEADER with two child elements): <Details HeaderLabel="DETAILS"> <HEADER Label="Header"> <HEAD Label="H1a">2.88%</HEAD> <HEAD Label="H2b">3.24%</HEAD

Restrict complexType with attributes in XSD?

。_饼干妹妹 提交于 2019-11-28 09:46:14
问题 I'm working with an XSD such as: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="algo"> <xs:complexType> <xs:sequence> <xs:element name="nota" type="t_algo" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="t_algo"> <xs:restriction base="xs:string"> <xs:pattern value="[1][0]|[0-9]" /> </xs:restriction> <xs:attribute name="modul" type="t_modul