xsd validation is failing - java

My xml validation is failing with the following reason:
Failed to execute goal se.ericsson.jcat.ebs:xml-validation-plugin:1.0.2:validate (default) on project ebs-extensions: Unable to execute mojo: While parsing /home/xchoshu/latestWorkspace/ebs-extensions/src/main/resources/log4j2.xml, at file:/home/xchoshu/latestWorkspace/ebs-extensions/src/main/resources/log4j.xsd, line 18, column 64: src-resolve.4.1: Error resolving component 'ConfigurationType'. It was detected that 'ConfigurationType' has no namespace, but components with no target namespace are not referenceable from schema document 'file:/home/xchoshu/latestWorkspace/ebs-extensions/src/main/resources/log4j.xsd'. If 'ConfigurationType' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'ConfigurationType' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:/home/xchoshu/latestWorkspace/ebs-extensions/src/main/resources/log4j.xsd'. -> [Help 1]
my xds looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://logging.apache.org/log4j/2.0/config" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Configuration" type="ConfigurationType"/>
<xs:complexType name="ConfigurationType">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="CustomLevels" type="CustomLevelsType"/>
<xs:element name="CustomLevel" type="CustomLevelType"/>
</xs:choice>
<xs:element name="Properties" type="PropertiesType"/>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="Filters" type="FiltersType"/>
<xs:element name="Filter" type="FilterType"/>
</xs:choice>
<xs:element name="ThresholdFilter" type="ThresholdFilterType"/>
<xs:element name="Appenders" type="AppendersType"/>
<xs:element name="Loggers" type="LoggersType"/>
</xs:sequence>
<xs:attribute name="packages" type="xs:string"/>
<xs:attribute name="status" type="xs:string"/>
<xs:attribute name="strict" type="xs:string"/>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="advertiser" type="xs:string"/>
<xs:attribute name="schema" type="xs:string"/>
</xs:complexType>
<xs:complexType name="PropertiesType">
<xs:sequence>
<xs:element name="Property" type="PropertyType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="AppenderType">
<xs:sequence>
<xs:element name="Layout" type="LayoutType" minOccurs="0"/>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="Filters" type="FiltersType"/>
<xs:element name="Filter" type="FilterType"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="fileName" type="xs:string" use="optional"/>
</xs:complexType>
<xs:complexType name="RootType">
<xs:sequence>
<xs:element name="AppenderRef" type="AppenderRefType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="level" type="xs:string"/>
</xs:complexType>
<xs:complexType name="PropertyType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="KeyValuePairType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="key" type="xs:string"/>
<xs:attribute name="value" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="AppendersType">
<xs:sequence>
<xs:element name="Appender" type="AppenderType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="AppenderRefType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ref" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="LoggerType">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="Filters" type="FiltersType"/>
<xs:element name="Filter" type="FilterType"/>
</xs:choice>
<xs:element name="AppenderRef" type="AppenderRefType"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="level" type="xs:string" use="optional"/>
<xs:attribute name="additivity" type="xs:string" use="optional"/>
</xs:complexType>
<xs:complexType name="FilterType" mixed="true">
<xs:sequence>
<xs:element name="KeyValuePair" type="KeyValuePairType" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="level" type="xs:string" use="optional"/>
<xs:attribute name="marker" type="xs:string" use="optional"/>
<xs:attribute name="onMatch" type="xs:string" use="optional"/>
<xs:attribute name="onMismatch" type="xs:string" use="optional"/>
</xs:complexType>
<xs:complexType name="FiltersType">
<xs:sequence>
<xs:element name="Filter" type="FilterType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="CustomLevelType">
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="intLevel" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="CustomLevelsType">
<xs:sequence>
<xs:element name="CustomLevel" type="CustomLevelType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="LoggersType" mixed="true">
<xs:sequence>
<xs:element name="Logger" type="LoggerType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="Root" type="RootType" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="LayoutType" mixed="true">
<xs:sequence>
<xs:element name="Pattern" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="pattern" type="xs:string" use="optional"/>
</xs:complexType>
<xs:complexType name="ThresholdFilterType">
<xs:attribute name="level" type="xs:string" use="optional"/>
<xs:attribute name="onMatch" type="xs:string" use="optional"/>
<xs:attribute name="onMismatch" type="xs:string" use="optional"/>
</xs:complexType>
</xs:schema>
my xml looks like:
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/2.0/config"
xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config https://ei-switching.rnd.ki.sw.ericsson.se/browser/jcat-bsp/log4j.xsd">
<Appenders>
<Console name="JCATDefaultConsoleAppender" target="SYSTEM_OUT"
follow="true">
<PatternLayout pattern="%d{default} %m [%t] %l%n%ex{full}" />
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="JCATDefaultConsoleAppender" />
</Root>
</Loggers>
can anyone help me in correcting the xsd.
TIA
Regards,
Shubhankar

The XSD is missing the namespace declaration xmlns="http://logging.apache.org/log4j/2.0/config". That makes the XSD valid.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://logging.apache.org/log4j/2.0/config"
targetNamespace="http://logging.apache.org/log4j/2.0/config"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Configuration" type="ConfigurationType"/>
But then your XML files has lots of issues
You are missing the mandatory elements Properties and ThresholdFilters and Appenders should contain at least 1 Appender (not a Console)
A Sample document would look something like this
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML 2017 Liquid Studio - Data Designer Edition 15.0.0.6978 (https://www.liquid-technologies.com) -->
<ns:Configuration xmlns:ns="http://logging.apache.org/log4j/2.0/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config C:\Temp\StackOverflow\41177776\Schema.xsd" packages="string" status="string" strict="string" name="string" advertiser="string" schema="string">
<ns:CustomLevels><!--optional-->
<ns:CustomLevel name="string" intLevel="string" />
<ns:CustomLevel name="string" intLevel="string" />
<ns:CustomLevel name="string" intLevel="string" />
</ns:CustomLevels>
<ns:Properties>
<ns:Property>string</ns:Property>
<ns:Property>string</ns:Property>
</ns:Properties>
<ns:Filters><!--optional-->
<ns:Filter type="string" level="string" marker="string" onMatch="string" onMismatch="string">
<ns:KeyValuePair key="string" value="string">string</ns:KeyValuePair>
</ns:Filter>
</ns:Filters>
<ns:ThresholdFilter level="string" onMatch="string" onMismatch="string" />
<ns:Appenders>
<ns:Appender type="string" name="string" fileName="string" />
<ns:Appender type="string" name="string" />
</ns:Appenders>
<ns:Loggers>
<ns:Logger name="string" level="string" additivity="string">
<ns:AppenderRef ref="string">string</ns:AppenderRef>
</ns:Logger>
<ns:Root>
<ns:AppenderRef ref="string">string</ns:AppenderRef>
</ns:Root>
</ns:Loggers>
</ns:Configuration>

Related

Eclipse Java - Jaxb2 plugin and WSDL

Hi I have to generate a WSDL. I have this xsd schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.ima.eu/xml/cattolica"
targetNamespace="http://www.ima.eu/xml/cattolica" elementFormDefault="qualified">
<xs:element name="CreateRequestInput">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="xs:string" minOccurs="1"/>
<xs:element name="user" type="xs:string" minOccurs="1"/>
<xs:element name="company" type="xs:string" minOccurs="1"/>
<xs:element name="insuranceNumber" type="xs:string"/>
<xs:element name="number" type="xs:string"/>
<xs:element name="creationDate" type="xs:string" minOccurs="1"/>
<xs:element name="answer" type="xs:string"/>
<xs:element name="answerType" type="xs:string"/>
<xs:element name="description" type="xs:string" minOccurs="1"/>
<xs:element name="idquintuple" type="xs:string" minOccurs="1"/>
<xs:element name="status" type="xs:string"/>
<xs:element name="priority" type="xs:string"/>
<xs:element name="idOTRS" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CreateRequestOutput">
<xs:complexType>
<xs:sequence>
<xs:element name="CreateRequest" type="tns:CreateRequest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="CreateRequest">
<xs:sequence>
<xs:element name="idCRM" type="xs:string"/>
<xs:element name="status" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Maven plugin Jaxb2 generate java objects correctly.
I did also soap config java file.
but the wsdl generated doesn't have xs:message part so with for example soap ui i cannot make request:
Why method CreateRequest is not generated in wsdl....
Thx
maybe there is a standard for naming the Responses and Requests?

jaxb, xsd substitution complextype

I have the following xsd
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="controls" type="Controls"></xs:element>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Controls">
<xs:sequence>
<xs:element name="control" type="Control" minOccurs="0" maxOccurs="unbounded"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Control" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Button">
<xs:complexContent>
<xs:extension base="Control">
<xs:sequence>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TextField">
<xs:complexContent>
<xs:extension base="Control">
<xs:sequence>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
How can I generate the following xml document
<?xml version = "1.0"?>
<screen ref="10_20_25_vwopstellen">
<name>VWOpstellen</name>
<controls>
<button>
<name>btnA</name>
<id>btn_10</id>
<action>click</action>
</button>
<textfield>
<name>fldA</name>
<id>fld_20</id>
</textfield>
</controls>
</screen>
Now it is generating the following xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<screen ref="scrVWOpstellen">
<name>VWOpstellen</name>
<controls>
<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Button">
<name>btnA</name>
</control>
<control xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Button">
<name>btnB</name>
</control>
</controls>
</screen>
And when i read my xml document I want a list of contols containing either buttons or textfields
List --> having Button and TextField
I have no idea, i think I should use substitution group but how. I tried something but the xsd is not generating any java code.
thanks
Johan
I'd suggest using an xs:choice, and also defining a target namespace, (http://my.target.namespace in my example), which can make working with JAXB easier in general.
E.g.:
<xs:schema targetNamespace="http://my.target.namespace" xmlns:tns="http://my.target.namespace" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="button" type="tns:Button" />
<xs:element name="textfield" type="tns:TextField" />
</xs:choice>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Control" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Button">
<xs:complexContent>
<xs:extension base="tns:Control">
<xs:sequence>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="TextField">
<xs:complexContent>
<xs:extension base="tns:Control">
<xs:sequence>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Another thought, sometimes the type derivation isn't really worth it. For something like this you might also just do:
E.g.:
<xs:schema targetNamespace="http://my.target.namespace" xmlns:tns="http://my.target.namespace" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="screen">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="button" type="tns:Button" />
<xs:element name="textfield" type="tns:TextField" />
</xs:choice>
</xs:sequence>
<xs:attribute name="ref" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="Button">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
<xs:element name="action" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TextField">
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="id" type="xs:string"></xs:element>
<xs:element name="value" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>

Java XML Schema Validation elementFormDefault="qualified"

I have a XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="Structure">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="Tag1"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
<xs:element minOccurs="0" ref="Properties"/>
<xs:element minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="url"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag1">
<xs:complexType>
<xs:attribute name="attr"/>
<xs:attribute name="attr2"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag2">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element minOccurs="0" ref="Object"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
</xs:sequence>
<xs:attribute name="filter"/>
</xs:complexType>
</xs:element>
<xs:element name="Object">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="tag35">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="attr4"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attr48"/>
</xs:complexType>
</xs:element>
<xs:element name="element52">
<xs:annotation>
<xs:documentation>Text54</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="el58"/>
</xs:sequence>
<xs:attribute name="vector"/>
</xs:complexType>
</xs:element>
<xs:element name="el66" type="el66Type"/>
<xs:element name="el58">
<xs:complexType mixed="true">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag3">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" name="MetaProperty">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="value"/>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
<xs:attribute name="ID"/>
<xs:attribute name="language" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="Property">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any minOccurs="0" processContents="skip"/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="type"/>
</xs:complexType>
</xs:element>
<xs:element name="Properties">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType mixed="true" name="el66Type">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:schema>
And an xml:
<Structure url="/Test/url">
<Tag1/>
<Tag2>
<Object>
<tag35/>
</Object>
</Tag2>
<Properties>
<Property name="bla" type="value1"/>
<Property name="bla2" type="value2"/>
</Properties>
</Structure>
I am doing a validation, and I am getting the following error:
org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'Tag2'. One of '{Tag1, Tag2, Tag3}' is expected.
I checked in previous questions and always the solution is related with elementFormDefault="qualified" but now it is not the case so, could it be possible that there is a bug there? Any clue, will be grateful.
Your XML is valid against your XSD as posted in your question.
The error message in your question would not be returned. It must have been from different XML or XSD documents.
As Michael Kay states in the comments:
elementFormDefault only affects local elements declarations.
elementFormDefault only comes into play when the XSD has a targetNamespace.
For more details about elementFormDefault see this answer.

XSD validation error: Cannot find the declaration of element 'xs:schema'

I saw that this question was asked many times but I didn't find a solution to my problem. So the error is:
Error on line 2 of document file...doc.xsd: cvc-elt.1: Cannot find the declaration of element 'xs:schema'
The code in the XSD file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Collection" >
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string"/>
<xs:element name="Recipe" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Ingredients">
<xs:complexType>
<xs:element name="Ingredient" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="amount" type="xs:integer" use="required"/>
<xs:attribute name="unit" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
<xs:element name="Preparation">
<xs:complexType>
<xs:element name="Step" type="xs:string minOccurs="0" maxOccurs="unbounded"/>"
</xs:complexType>
</xs:element>
<xs:element name="Comment" type="xs:string" minOccurs="0"/>
<xs:element name="Nutrients">
<xs:complexType>
<xs:attribute name="proteins" type="xs:integer" use="required"/>
<xs:attribute name="carbohidrati" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="fat" type="xs:integer" use="choice"/>
<xs:attribute name="vitamins" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="calories" type="xs:float" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The code in the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="src/doc.xsd">
<Description>Nu prea merge</Description>
<Recipe>
<Title>Paste</Title>
<Ingredients>
<Ingredient name="paste" amount="1" unit="pachet"></Ingredient>
</Ingredients>
<Preparation>
<Step>Fierbere</Step>
<Step>Servire</Step>
</Preparation>
<Nutrients proteins="2" carbohidrati="2" fat="da" vitamins="4" calories="1000.9"></Nutrients>
</Recipe>
</Collection>
The code in my Java class:
import java.io.File;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class Main {
public static void main(String[] args) {
File newFile = new File("src/doc.xsd");
Document doc = null;
SAXBuilder sbd = new SAXBuilder(XMLReaders.XSDVALIDATING);
try {
doc = sbd.build(newFile);
} catch (JDOMException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XMLOutputter outputDoc = new XMLOutputter();
outputDoc.setFormat(Format.getPrettyFormat());
try{
outputDoc.output(doc,System.out);
//outputDoc.output(doc, new FileWriter("src/myXmlDoc2.xml"));
}
catch(Exception e){
System.out.println("Eroare la parsarea documentului XML!");
}
}
}
If anyone has any idea about the possible problem please help me!
Assuming that your XML is fixed, then you should change your XSD as follows:
Fix the extra " as mentioned by #Andreas.
Add xs:sequence under xs:complexType in two places.
Change the type of #fat to xs:string.
Change use="choice" to use="optional".
Altogether, this XSD will successfully validate your XML:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Collection" >
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string"/>
<xs:element name="Recipe" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Ingredients">
<xs:complexType>
<xs:sequence>
<xs:element name="Ingredient" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="amount" type="xs:integer" use="required"/>
<xs:attribute name="unit" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Preparation">
<xs:complexType>
<xs:sequence>
<xs:element name="Step" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Comment" type="xs:string" minOccurs="0"/>
<xs:element name="Nutrients">
<xs:complexType>
<xs:attribute name="proteins" type="xs:integer" use="required"/>
<xs:attribute name="carbohidrati" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="fat" type="xs:string" use="optional"/>
<xs:attribute name="vitamins" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="calories" type="xs:float" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Of course, you will also have to check your Java code. (Hint: make sure you're not validating your XSD as XML when you really want to validating your XML against your XSD.)

Java jaxb xsd how to allow maxOccurence

I want following xml output and have following xsd
<msgBody>
<Contato>
<cd_id>11</cd_id>
<property key="name" value="Adde" />
<property key="Phone" value="34343" />
<property key="Address" value="address" />
</Contato>
<Contato>
<cd_id>12</cd_id>
<property key="name" value="BJ" />
<property key="Phone" value="7899" />
<property key="Address" value="sdfkjsdfsdf" />
</Contato>
</msgBody>
I have following xsd where i don't know how can i allow to have multiple property tags
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="msgBody">
<xs:complexType>
<xs:sequence>
<xs:element name="Contato" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="cd_id"/>
<xs:element type="property_data_type" name="property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="property_data_type">
<xs:sequence>
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
Try this
<xs:element name="msgBody">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Contato"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="contao">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:int" name="cd_id"/>
<xs:element maxOccurs="unbounded" ref="propertytype"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="propertytype">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Please add maxOccurs.
<xs:element type="property_data_type" name="property" maxOccurs="unbounded"/>
Alternatively, you can put any concrete limit like:
<xs:element type="property_data_type" name="property" maxOccurs="10"/>
It means you can only have a maximum of 10 property elements. The <maxOccurs> indicator specifies the maximum number of times an element can occur.

Categories