Refering another xsd element in another xsd? - java

I have below xsd.
AccountDetails.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://webservices.samples.blog.com" targetNamespace="http://webservices.samples.blog.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Account" type="Account"/>
<xs:complexType name="Account">
<xs:sequence>
<xs:element name="AccountNumber" type="xs:string"/>
<xs:element name="AccountName" type="xs:string"/>
<xs:element name="AccountBalance" type="xs:double"/>
<xs:element name="AccountStatus" type="EnumAccountStatus"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="EnumAccountStatus">
<xs:restriction base="xs:string">
<xs:enumeration value="Active"/>
<xs:enumeration value="Inactive"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Generic.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://com/blog/samples/webservices/accountservice" xmlns:account="http://webservices.samples.blog.com" targetNamespace="http://com/blog/samples/webservices/accountservice" elementFormDefault="qualified">
<xsd:import namespace="http://webservices.samples.blog.com" schemaLocation="AccountDetails.xsd"/>
<xsd:element name="AccountDetailsResponse">
<xsd:complexType>
***//TO DO : here i need to refer the element name 'Account' which is there in AccountDetails.xsd. Here i can have list of Accounts. How can i refer that?***
</xsd:complexType>
</xsd:element>
<xsd:element name="AccountDetailsEnRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="AccountDetailsEnum" type="account:EnumAccountStatus"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
In above Generic.xsd, in TO DO part i need to refer the element name 'Account' which is there in AccountDetails.xsd. here i can have list of 'Account's. How can i write that code in To DO part of Generic.xsd? Please help me too fill the TO DO part in Generic.xsd
Thanks!

You can use type="account:Account". That means you'll be referencing the Account element in the namespace with the alias account. This alias has already been defined in Generic.xsd:
xmlns:account="http://webservices.samples.blog.com"
This alias has the right value, because the Account element defined in AccountDetails.xsd belongs precisely to that namespace
targetNamespace="http://webservices.samples.blog.com"
So you could reference the element like this:
<xsd:element name="AccountDetailsResponse">
<xsd:complexType>
<xs:element name="acc" type="account:Account" maxOccurs="unbounded"/>
</xsd:complexType>
</xsd:element>

Related

Issue with XSD schema in transforming data from XML to JSON format

I am trying to transform data from xml to json format, For that I have written xsd schema from the xml output to transform data from xml to json.
Following is my xml which needs to be transformed to json
<?xml version="1.0" encoding="UTF-8"?>
<SyncData>
<Employerid>12345</Employerid>
<ImporterEmail>abcdef#xyz.com</ImporterEmail>
<ReportEmail>abcdefggggg#xyz.com</ReportEmail>
<Employees>
<wd:WorkerSummary xmlns:is="java:com.workday.esb.intsys.xpath.
ParsedIntegrationSystemFunctions"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xdiff="urn:com.workday/esb/xdiff" xmlns:wd="urn:com.workday/bsvc">
<wd:ReferenceID/>
<wd:Name>abcdef,ghijklm</wd:Name>
<wd:Title/>
<wd:EmployeeId>JG00889</wd:EmployeeId>
<wd:EMail>sderf.rtyui#xyzz.com</wd:EMail>
<wd:AddressLine1>1400 Post Alm Tyui</wd:AddressLine1>
<wd:City>Bostonn</wd:City>
<wd:State>MC</wd:State>
<wd:Zip>11067</wd:Zip>
<wd:Country>AUS</wd:Country>
</wd:WorkerSummary>
</Employees>
</SyncData>
Following are the two different xsd schema’s that I have defined from the above stated xml and I am getting different error’s for each one.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.your-company.com/WorkermapSchema"
xmlns:tns="http://www.your-company.com/WorkermapSchema"
elementFormDefault="qualified">
<xs:element name="WorkerSummary">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="ReferenceID" />
<xs:element type="xs:string" name="Name" />
<xs:element type="xs:string" name="Title" />
<xs:element type="xs:string" name="EmployeeId" />
<xs:element type="xs:string" name="EMail" />
<xs:element type="xs:string" name="AddressLine1" />
<xs:element type="xs:string" name="City" />
<xs:element type="xs:string" name="State" />
<xs:element type="xs:string" name="Zip" />
<xs:element type="xs:string" name="Country" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The error I was getting with this schema is as following
com.workday.esb.xmltojson.XmlToJsonException: Root Element type not found "SyncData"
Since the above error says the Root Element “SyncData” not found, I have tried to define schema in other way with the Root Element as following.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-
microsoft-com:xml-msdata" xmlns:app1="urn:com.workday/bsvc">
<xs:import namespace="urn:com.workday/bsvc"></xs:import>
<xs:element name="SyncData">
<xs:complexType>
<xs:sequence>
<xs:element name="Employerid" type="xs:string" minOccurs="0" />
<xs:element name="ImporterEmail" type="xs:string" minOccurs="0" />
<xs:element name="ReportEmail" type="xs:string" minOccurs="0" />
<xs:element name="Employees" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="WorkerSummary" type="xs:string"/>
<xs:element name="ReferenceID" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="SyncData" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
After using the above xsd schema, I am not getting error related to Root Element “SyncData”, But I am getting a different error which I am not understanding what it is about, The error is as following.
org.codehaus.stax2.typed.TypedXMLStreamException: ParseError at [row,col]:[2,359] Message: Element content can not contain child START_ELEMENT when using Typed Access methods
Is there anything wrong in the schema or what is the error is about?
I have tried multiple attempts on xsd schema to execute it perfectly as required, but seems something is missing which I am not sure that is causing the failure.
Can you use this schema
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="SyncData">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Employerid" type="xsd:int" />
<xsd:element name="ImporterEmail" type="xsd:string" />
<xsd:element name="ReportEmail" type="xsd:string" />
<xsd:element name="Employees">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="wd:WorkerSummary">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="wd:ReferenceID" type="xsd:string" />
<xsd:element name="wd:Name" type="xsd:string" />
<xsd:element name="wd:Title" type="xsd:string" />
<xsd:element name="wd:EmployeeId" type="xsd:string" />
<xsd:element name="wd:EMail" type="xsd:string" />
<xsd:element name="wd:AddressLine1" type="xsd:string" />
<xsd:element name="wd:City" type="xsd:string" />
<xsd:element name="wd:State" type="xsd:string" />
<xsd:element name="wd:Zip" type="xsd:int" />
<xsd:element name="wd:Country" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

how to create xsd for xml which has namespace for Webmethod schema

My xml is like below:-
<?xml version="1.0"?>
<create xmlns:xsi="https://csu.service-now.com">
<sys_id xsi:type="xsd:string">30b78e589d5d0a00eba30ec92748d7fa</sys_id>
<number xsi:type="xsd:string">INC0135185</number>
</create>
I wants to creating an xsd by which the validation will be successful. So basically that by that xsd first I have to create a schema in webmethods and validate xml against that xml.
what I tried so far is :
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="create">
<xs:complexType>
<xs:sequence>
<xs:element name="sys_id">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="csu:type" xmlns:csu="https://csu.service-now.com"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="number">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="csu:type" xmlns:csu="https://csu.service-now.com"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
but its failing there to create schema in webmethods.Looks like this is not a valid xsd as per webmethod.i tried many but no luck.
Please help here.
If you want to suffer then writing an XSD that webMethods is able to understand is the way to go.
Is there any reason why you have to make an XSD?
The easiest way to learn what is acceptable for webMethods is to create a web service and then copy paste the WSDL URL in the browser and observe how webMethods defines everything within the "<xsd:schema ..> ... </xsd:schema>" then try to apply the same structure to define your own custom XSD.
The following is off the top of my head:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
targetNamespace="http://some.target.namespace/test"
xmlns:tns="http://some.target.namespace/test"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="ServiceRequest" type="tns:ServiceRequest"/>
<xsd:complexType name="ServiceRequest">
<xsd:sequence>
<xsd:element name="Create" nillable="false" type="tns:Create"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Create">
<xsd:sequence>
<xsd:element name="sys_id" nillable="true" type="xsd:string"/>
<xsd:element name="number" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Again, if you can, try to steer away from XSDs. WebMethods is extremely picky when it comes to XSDs.
If you use some kind of GUI software to generate an XSD then it's almost certain that the generated XSD won't be compatible in webMethods.
Often what I do, instead of importing an XSD within webMethods, is to read the XSD in notepad and manually reproduce the structure in webMethods by manually defining the documents and fields.

jaxb: xsd to java, a parent element that can have one of two other elements

I have a parent element called MESSAGE. The MESSAGE element can carry any kind of OBJECT (usually complexTypes), but only one object at a time. I'm starting from XML files to XSD then to java with something like this:
<xs:element name="MESSAGE">
<xs:complexType>
<xs:sequence>
<xs:element name="OBJECT"/>
<xs:complexType>
<xs:sequence>
<!-- Definition here -->
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>
I have lots of XML files which have MESSAGE types but contain different OBJECTs. I have produced their equivalent XSDs but only the first one is translated to java using xjc, while the rest are not because of the error Message is already defined. So how can I have generic MESSAGE element in my XSD which can take any OBJECTs?
If the element inside MESSAGE must be one of a subset of defined elements, use <choice>.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="MESSAGE">
<xsd:complexType>
<xsd:choice>
<xsd:element name="A" type="xsd:string"/>
<xsd:element name="B" type="xsd:string"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
If the element inside MESSAGE can be any defined element, use <any>.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="MESSAGE">
<xsd:complexType>
<xsd:sequence>
<any namespace="##targetNamespace"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="X" type="xsd:string"/>
<xsd:element name="Y" type="xsd:string"/>
</xsd:schema>
I asked a similar question to this one once, and I think it would give you an idea how to do this:
How to Override Xsd element inside of parent/extended element
You'll have to extend/restrict your root parent object.
I solved it using the minoccurs and maxoccurs like this. but it doesn't adhere precisely to what i was asking. the accepted answer is better
<xs:element name="MESSAGE">
<xs:complexType>
<xs:sequence>
<xs:element name="A" minOccurs="0" maxOccurs="1"/>
<xs:complexType>
<xs:sequence>
<!-- Definition here -->
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="B" minOccurs="0" maxOccurs="1"/>
<xs:complexType>
<xs:sequence>
<!-- Definition here -->
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
It was a bit cumbersome adding all the elements to one xsd file , but it worked, i have tested it. #Andreas answer seems more readable using <xsd:choice>. i have never used it, but i will look into it.

how to add validation in xsd for blank spaces

i have created basic xsd successfully however I want to add restriction for the element that it should be present and contains atleast one character. it also has 4 attributes. i am facing problem in adding restriction since I can not use simple type as the element has attributes.
Please suggest something
thanks in advance
Added XSD data posted by OP in comments (sic)
<xs:element name="Engines">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="Engine" />
</xs:sequence>
<xs:attribute name="Count" use="required" type="xs:integer" />
</xs:complexType>
</xs:element>
<xs:element name="Engine">
<xs:complexType>
<xs:sequence>
<xs:element name="Model" type="Model"/>
<xs:element ref="SerialNumber" />
</xs:sequence>
</xs:complexType>
</element>
<xs:simpleType name="trimValueType">
<xs:restriction base="xs:string">
<xs:minLength value="1"></xs:minLength>
<xs:whiteSpace value="collapse"></xs:whiteSpace>
</xs:restriction>
</xs:simpleType>
<xs:complexTyp‌​e name="Model">
<xs:simpleContent>
<xs:extension base="trimValueType">
<xs:attribute name="ATTRIBUTE" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<Engines count = 1> <Engine> <Model ATTRIBUTE = "r\w"> </Model> <SerialNumber ATTRIBUTE = "r/w">1234567</SerialNumber> <Engine> <Engines>
You have to first create a simple type that restricts xsd:string to specify your text constraints. Then you need to define a complex type, with a simple content, which extends the simple type you just created using the attributes you want. I threw in a whitespace constraint, just to match your title, even though you're not specifically mention it in your problem statement.
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:simpleType name="one">
<xsd:restriction base="xsd:string">
<xsd:whiteSpace value="collapse"/>
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="two">
<xsd:simpleContent>
<xsd:extension base="one">
<xsd:attribute name="one"/>
<xsd:attribute name="two"/>
<xsd:attribute name="etc"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:element name="root" type="two"/>
</xsd:schema>
Sample XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" one="anySimpleType" two="anySimpleType" etc="anySimpleType" xmlns="http://tempuri.org/XMLSchema.xsd">root1</root>

XSD validation error using choices

I need either element A or B or both. If i use choices, then it throws an exception Element 'A' cannot have character [children], because the type's content type is element-only.
How to achieve the desired result.
<xsd:complexType>
<xsd:sequence>
<xsd:element name="A">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="C"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="B"/>
</xsd:sequence>
</xsd:complexType>
sample XML is
<start>
<A>
<C>hhg</C>
</A>
</start>
<start>
<A>
<C>hhg</C>
</A>
<B>fgeg</B>
</start>
<start>
<B>fergf</B>
</start>
Use minOccurs="0", e.g.:
<xs:element name="A" minOccurs="0">
...
<xs:element name="B" minOccurs="0"/>
For XML:
<root>
<start>
<A>
<C>hhg</C>
</A>
</start>
<start>
<A>
<C>hhg</C>
</A>
<B>fgeg</B>
</start>
<start>
<B>fergf</B>
</start>
</root>
Appropriate XSD should be:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="start">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="A">
<xs:complexType>
<xs:sequence>
<xs:element name="C" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" name="B" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="test">
<xs:choice>
<xs:element name="A">
<xs:complexType>
<xs:sequence>
<xs:element name="C"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="B"/>
</xs:choice>
</xs:complexType>
</xs:schema>
This is validated ok in Oxygen ...

Categories