Server-Side XML Validation with CXF Webservice

You can override validation error messages, inserting a line number, by using a custom ValidationEventHandler: package example; import javax.xml.bind.ValidationEvent; import javax.xml.bind.helpers.DefaultValidationEventHandler; public class MyValidationEventHandler extends DefaultValidationEventHandler { @Override public boolean handleEvent(ValidationEvent event) { if (event.getSeverity() == ValidationEvent.WARNING) { return super.handleEvent(event); } else { throw new RuntimeException(event.getMessage() + ” [line:”+event.getLocator().getLineNumber()+”]”); } } } If you configure … Read more

What is the difference between xsd and xsi?

xsd and xsi Similarities Both are XML namespace prefixes, abbreviations for an XML namespace. Both are, as are all namespace prefixes, arbitrarily named; other namespace prefix abbreviations could equally well be used. However, both prefixes are conventional and therefore recommended. (An also-conventional alternative to xsd is xs.) xsd and xsi Differences The xsd (or xs) … Read more

XML validation with XSD: how to avoid caring about the sequence of the elements?

<xsd:complexType name=”questions”> <xsd:all> <xsd:element name=”location” type=”location”/> <xsd:element name=”multipleChoiceInput” type=”multipleChoiceInput”/> <xsd:element name=”textInput” type=”textInput”/> <xsd:element name=”pictureInput” type=”pictureInput”/> </xsd:all> </xsd:complexType> NOTE: I have changed “sequence” to “all” Sequence forces order (as defined). if order doesn’t matter then all is used. If there are chances of element occurence more than once then xsd:any can be used. <xsd:complexType name=”questions”> <xsd:sequence> … Read more

cvc-elt.1: Cannot find the declaration of element ‘MyElement’

Your schema is for its target namespace http://www.example.org/Test so it defines an element with name MyElement in that target namespace http://www.example.org/Test. Your instance document however has an element with name MyElement in no namespace. That is why the validating parser tells you it can’t find a declaration for that element, you haven’t provided a schema … Read more

Require XML element in XSD when another element has certain value?

XSD 1.1 Here’s how use xs:assert to make IBAN be mandatory when TYPE = ‘INTERNATIONAL’: <?xml version=”1.0″ encoding=”UTF-8″?> <xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:vc=”http://www.w3.org/2007/XMLSchema-versioning” vc:minVersion=”1.1″> <xs:element name=”root”> <xs:complexType> <xs:sequence> <xs:element name=”TYPE” type=”TestEnum” /> <!– // This Element should only required when TYPE = INTERNATIONAL –> <xs:element name=”IBAN” minOccurs=”0″/> </xs:sequence> <xs:assert test=”not(TYPE = ‘INTERNATIONAL’) or IBAN”/> </xs:complexType> </xs:element> <xs:simpleType … Read more

External referenced DTD in XML

You have duplicate DOCTYPE declarations. If you want to reference an external DTD: test.xml <?xml version=’1.0′ encoding=’UTF-8′?> <!DOCTYPE email SYSTEM “test.dtd”> <email> <von>[email protected]</von> <zu>[email protected]</zu> <titel>Hello</titel> <text>Dear John….;-).</text> <prior type=”schnell”/> </email> test.dtd <!ELEMENT email (von,zu,titel,text,prior)> <!ELEMENT von (#PCDATA)> <!ELEMENT zu (#PCDATA)> <!ELEMENT titel (#PCDATA)> <!ELEMENT text (#PCDATA)> <!ELEMENT prior EMPTY> <!ATTLIST prior type CDATA #REQUIRED > … Read more

What does elementFormDefault do in XSD?

ElementFormDefault has nothing to do with namespace of the types in the schema, it’s about the namespaces of the elements in XML documents which comply with the schema. Here’s the relevent section of the spec: Element Declaration Schema Component Property {target namespace} Representation If form is present and its ·actual value· is qualified, or if … Read more

xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

Namespace related attributes in XML and XML Schema (XSD) xmlns is part of the W3C Namespaces in XML Recommendation: The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/. In your example, it declares that http://maven.apache.org/POM/4.0.0 is the default namespace for the elements in your … Read more

How to fix error: The markup in the document following the root element must be well-formed

General case The markup in the document following the root element must be well-formed. This error indicates that your XML has markup following the root element. In order to be well-formed, XML must have exactly one root element, and there can be no further markup following the single root element. One root element example (GOOD) … Read more