Java Code for creating child elements within an element - xsd

I want to generate the following XSD Schema using JAXB and ObjectFactory class of org/w3/_2001/xmlschema
<xs:element name="result">
<xs:complexType>
<xs:sequence>
<xs:element name="sfobject" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
But I am getting XSD as :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="result">
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
<xs:element name="sfobject" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="type"/>
</xs:schema>
I am not able to add one element inside other element. How do I do that in Java using JAXB and ObjectFactory methods.
Sample Code:
List schemaElementList = schema.getSimpleTypeOrComplexTypeOrGroup();
TopLevelElement resultElement = xsdObjFactory.createTopLevelElement();
resultElement.setName("result");
LocalComplexType resultCompType = xsdObjFactory.createLocalComplexType();
resultElement.setComplexType(resultCompType);
resultCompType.setSequence(expGroup);
Element sfElement = (Element)xsdObjFactory.createLocalElement();
sfElement.setName("sfobject");
sfElement.setMaxOccurs("unbounded");
sfElement.setMinOccurs(new BigInteger("0"));
LocalComplexType sfCompType = xsdObjFactory.createLocalComplexType();
sfElement.setComplexType(sfCompType);
schemaElementList.add(resultElement);

Related

Make element required when attribute is set to some value using XSD (1.1)

I have a following schema (1.1). I would like to have schema that will check that a sequence of parameters has mendatory parameter of type '1' and optional parameter of type '2'. I did it using alternatives because different types have different list of attributes.
How write an assertion to check that parameter of type 1 is mendatory?
<?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" elementFormDefault="qualified" vc:minVersion="1.1">
<xs:element name="rule">
<xs:complexType>
<xs:sequence>
<xs:element name="conditions" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="condition" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" maxOccurs="unbounded" minOccurs="1">
<xs:alternative test="#type = '1'" type="typeOne"/>
<xs:alternative test="#type = '2'" type="typeTwo" />
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="typeOne">
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="typeTwo">
<xs:attribute name="type" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>
I did it using alternatives because different types have different list of attributes.
It would be like this:
<xs:element name="condition" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" maxOccurs="unbounded" minOccurs="1">
<xs:alternative test="#type = '1'" type="typeOne"/>
<xs:alternative test="#type = '2'" type="typeTwo" />
</xs:element>
</xs:sequence>
<xs:assert id="type-1-mandatory" test="parameter[#type eq '1']"></xs:assert>
</xs:complexType>
</xs:element>

XML Schema with a combination of choice and all

I want to do the validations like:
<A>
<B> or <C>
<D>
</A>
In the , the first element should be one of the B and C. The second element is D. And at the same time, the first element B or C and the second element do not in sequence. It could become and . Anybody know how to do it?
To match your example, please try:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="A">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="B" type="xs:string"/>
<xs:element name="C" type="xs:string"/>
</xs:choice>
<xs:element name="D" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
You have to use two things here, the first one is <xs:choice> which allows only one elements to be present in xml.
In order to make an element optional (D) you have to specify minOccurs="0".
Edited (after feedback): All valid cases are covered with this XSD.
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="B" type="xs:string"/>
<xs:element name="C" type="xs:string"/>
<xs:element name="D" type="xs:string"/>
<xs:complexType name="OurType">
<xs:choice>
<xs:sequence>
<xs:element ref="D"/>
<xs:choice>
<xs:element ref="B"/>
<xs:element ref="C"/>
</xs:choice>
</xs:sequence>
<xs:sequence>
<xs:choice>
<xs:element ref="B"/>
<xs:element ref="C"/>
</xs:choice>
<xs:element ref="D"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:element name="A" type="OurType"/>
</xs:schema>
I get the best practice. It should be as below:
<xs:element name="A">
<xs:complexType>
<xs:all>
<xs:element ref="Choice" minOccurs="1" maxOccurs="1"/>
<xs:element ref="D" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="D" type="xs:string"/>
<xs:element name="Choice" abstract="true"/>
<xs:element name="B" substitutionGroup="Choice">
</xs:element>
<xs:element name="C" substitutionGroup="Choice">
</xs:element>

XSD/WSDL mapping in Smooks using WSO2 ESB

I am trying to map XSD to another XSD that has some different elements in Smooks. When I add both XSDs in Smooks it is not allowing me to map the elements. Is it possible in Smooks to map two different XSDs?
XSD1:CDM
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:RequestResponse="http://example.com/cdm/xsd/Person_01_RequestResponse_001"
xmlns:prsn="http:/example.com/cdm/xsd/Person_01"
attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://example.com/cdm/xsd/Person_01_RequestResponse_001">
<!-- ************ import Employee Model ************ -->
<xs:import namespace="http://example.com/cdm/xsd/Person_01" schemaLocation="Person_01.xsd"/>
<!-- ********************************************* -->
<!-- Request Response Types -->
<!-- ********************************************* -->
<!-- For CreatePerson operation-->
<xs:element name="CreatePersonRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonDetail" type="prsn:Person"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CreatePersonResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="ResponseCode" type="xs:string"/>
<xs:element name="ResponseMessage" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- For GetPerson operation-->
<xs:element name="GetPersonRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonId" type="prsn:PersonIdType" nillable="false"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetPersonResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" type="prsn:Person" minOccurs="0" nillable="true"/>
<xs:element name="ResponseCode" type="xs:string"/>
<xs:element name="ResponseMessage" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XSD2:LEGACY
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:RequestResponse="http://example.com/xsd/Person_01_RequestResponse_001"
xmlns:prsn="http://example.com/xsd/Person_01"
attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://example.com/xsd/Person_01_RequestResponse_001">
<!-- ************ import Employee Model ************ -->
<xs:import namespace="http://example.com/xsd/Person_01" schemaLocation="Person_01.xsd"/>
<!-- ********************************************* -->
<!-- Request Response Types -->
<!-- ********************************************* -->
<!-- For CreatePerson operation-->
<xs:element name="CreatePersonRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="PersonDetail" type="prsn:Person"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CreatePersonResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="ResponseCode" type="xs:string"/>
<xs:element name="ResponseMessage" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- For GetPerson operation-->
<xs:element name="GetPersonRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="CNIC" type="xs:string" nillable="false" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetPersonResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" type="prsn:Person" minOccurs="0" nillable="true"/>
<xs:element name="ResponseCode" type="xs:string"/>
<xs:element name="ResponseMessage" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- For UpdatePerson operation-->
<xs:element name="UpdatePersonRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" type="prsn:Person"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="UpdatePersonResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="ResponseCode" type="xs:string"/>
<xs:element name="ResponseMessage" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- For deletePerson operation-->
<xs:element name="DeletePersonRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="CNIC" type="xs:string" nillable="false" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DeletePersonResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="ResponseCode" type="xs:string"/>
<xs:element name="ResponseMessage" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Why won't my XML schema element take a number?

In XMLSpy, there is a validation error, "Value '18' is not allowed for attribute 'name'. Hint: A valid value would be 'NCName'. Error location: xs:schema / xs:element / xs:complexType / xs:choice / xs:element / xs:complexType / xs:choice / xs:element / #name" here:
<xs:element name="Age">
<xs:complexType>
<xs:choice>
<xs:element name="18" type="xs:int"/>
If I enter letters it validates, but I need numbers in that field.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.urent.com/Elmhurst" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.urent.com/Elmhurst" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="URent">
<xs:annotation>
</xs:annotation>
<xs:complexType>
<xs:choice>
<xs:element name="RentalPeriod">
<xs:complexType>
<xs:all>
<xs:element name="StartDate" type="xs:date"/>
<xs:element name="EndDate" type="xs:date"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="Age">
<xs:complexType>
<xs:choice>
<xs:element name="18" type="xs:int"/>
<xs:element name="19" type="xs:int"/>
<xs:element name="20" type="xs:int"/>
<xs:element name="21" type="xs:int"/>
<xs:element name="22" type="xs:int"/>
<xs:element name="23" type="xs:int"/>
<xs:element name="24" type="xs:int"/>
<xs:element name="25+" type="xs:int"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="RateCode">
<xs:complexType>
<xs:choice>
<xs:element name="Corporate" type="xs:string"/>
<xs:element name="Leisure" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="TypeOfVehicle">
<xs:complexType>
<xs:all>
<xs:element name="Subcompact" type="xs:string"/>
<xs:element name="Compact" type="xs:string"/>
<xs:element name="Intermediate" type="xs:string"/>
<xs:element name="Standard" type="xs:string"/>
<xs:element name="Full Size" type="xs:string"/>
<xs:element name="Premium" type="xs:string"/>
<xs:element name="Luxury" type="xs:string"/>
<xs:element name="Standard Elite SUV" type="xs:string"/>
<xs:element name="Intermediate SUV" type="xs:string"/>
<xs:element name="Minivan" type="xs:string"/>
<xs:element name="Full Size SUV" type="xs:string"/>
<xs:element name="Standard SUV" type="xs:string"/>
<xs:element name="Premium SUV" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="Residence">
<xs:complexType>
<xs:choice>
<xs:element name="US" type="xs:string"/>
<xs:element name="OutsideUS" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="SpecialEquipment">
<xs:complexType>
<xs:choice>
<xs:element name="GPS Navigation" type="xs:string"/>
<xs:element name="XM Radio" type="xs:string"/>
<xs:element name="Fuel Service" type="xs:string"/>
<xs:element name="Child Safety Seats" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="Customer Info">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="LastName" type="xs:string"/>
<xs:element name="EmailAddress" type="xs:string"/>
<xs:element name="PhoneNumber" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
Taken literally, your declaration says the set of valid Age elements includes <Age><18>45</18></Age> and <Age><25+>33</25+></Age>, though not <Age><45>18</45></Age>.
But '18' and '25+' are not allowed by XML as names of element types; like names of variables in most programming languages, names of element types in XML must begin with a letter. (They are also not allowed to contain blanks, so the element names "Full Size" and "Standard Elite SUV" will also raise errors.) So the examples given in the preceding paragraph are not XML, and you cannot define an XSD schema against which they would be valid.
It's not clear whether you need to review the basics of XML or the basics of XSD, but your work developing a schema will be more pleasant and productive if you have a better grasp of the technologies you are trying to use; that helps you work with the grain of the technology and not against it.

Creating multiple element name with sequence.

Below is the sample xml which has multiple <rulex> that starts with sequence 1 and it can end up to many rule like <rule1> , <rule2>, <rule3> etc....
<?xml version="1.0" encoding="UTF-8"?>
<AddressChange_181>
<rules>
<rule1>
<conditions>xya</conditions>
<response_path>abc</response_path>
</rule1>
<rule2>
<conditions>xxxx</conditions>
<response_path>aaaa</response_path>
</rule2>
<rule3>
<conditions>yyyyy</conditions>
<response_path>ffff</response_path>
</rule3>
<rule4>
<conditions>zzzz</conditions>
<response_path>yyyy</response_path>
</rule4>
<default>
<response_path>uuuuu</response_path>
</default>
</rules>
</AddressChange_181>
Below is the schema where i tried creating dynamic <rulex> element name for the above xml.
when i generate xml from this schema i do not get the same xml format as above xml.
can you please let me know how to create schema with multiple element name that starts with a sequence number.
My requirement is to add more than one rule (<rule1>,<rule2>,<rule3> etc...) in xml file and this xml file should be validated against schema.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="default" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rule">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:anyType">
<xs:pattern value="rule/d{1,3}"></xs:pattern>
</xs:restriction>
</xs:simpleContent>
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
</xs:schema>
Thanks,
Madhu
There is no way to define such a structure using XSD if the number of ruleX tags is arbitrarily defined. If you can constrain the upper bound to a maximum, and you really have to stick with ruleX naming convention, than you can define a complex type such as ruleType, then a bunch of rule1, rule2,... , ruleN elements of that type - I would call this messy... I wouldn't recommend this.
XSD (with a maximum of 6):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule1" minOccurs="0"/>
<xs:element ref="rule2" minOccurs="0"/>
<xs:element ref="rule3" minOccurs="0"/>
<xs:element ref="rule4" minOccurs="0"/>
<xs:element ref="rule5" minOccurs="0"/>
<xs:element ref="rule6" minOccurs="0"/>
<xs:element ref="default"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="ruleType">
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
<xs:element name="rule1" type="ruleType"/>
<xs:element name="rule2" type="ruleType"/>
<xs:element name="rule3" type="ruleType"/>
<xs:element name="rule4" type="ruleType"/>
<xs:element name="rule5" type="ruleType"/>
<xs:element name="rule6" type="ruleType"/>
</xs:schema>
Alternatively, you could have a tag named "rule" with a #sequence attribute.
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="default"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="SequenceKey">
<xs:selector xpath="rule"/>
<xs:field xpath="#sequence"/>
</xs:unique>
</xs:element>
<xs:complexType name="ruleType">
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="sequence" type="xs:int" use="required"/>
</xs:complexType>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
<xs:element name="rule" type="ruleType"/>
</xs:schema>
Sample XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<rules xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rule sequence="1">
<conditions>conditions1</conditions>
<response_path>response_path1</response_path>
</rule>
<rule sequence="2">
<conditions>conditions2</conditions>
<response_path>response_path2</response_path>
</rule>
<default>
<response_path>response_path1</response_path>
</default>
</rules>
Or it could also be that one could simply use the index of the <rule/> within the parent collection .

Resources