how to add validation in xsd for blank spaces - java

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>

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.

How to handle array in xml / xsd for generating a POJO class?

Sample XML response from REST WS -
<UserInfoDataContract xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<DisplayName>Test User</DisplayName>
<Email>test#test.com</Email>
<FirstName>Test</FirstName>
<IsSuperUser>false</IsSuperUser>
<LastName>User</LastName>
<Password>testuser1</Password>
<PortalID>0</PortalID>
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>Registered Users</a:string>
</Roles>
<UserID>43</UserID>
<Username>testuser</Username>
</UserInfoDataContract>
XSD generated using http://xmlgrid.net/xml2xsd.html
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- XML Schema Generated from XML Document on Thu Apr 09 2015 11:18:33 GMT-0500 (CDT) -->
<!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->
<xs:element name="UserInfoDataContract">
<xs:complexType>
<xs:sequence>
<xs:element name="DisplayName" type="xs:string"></xs:element>
<xs:element name="Email" type="xs:string"></xs:element>
<xs:element name="FirstName" type="xs:string"></xs:element>
<xs:element name="IsSuperUser" type="xs:string"></xs:element>
<xs:element name="LastName" type="xs:string"></xs:element>
<xs:element name="Password" type="xs:string"></xs:element>
<xs:element name="PortalID" type="xs:int"></xs:element>
<xs:element name="Roles">
<xs:complexType>
<xs:sequence>
<xs:element name="a:string" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="UserID" type="xs:int"></xs:element>
<xs:element name="Username" type="xs:string"></xs:element>
</xs:sequence>
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
When i try to create JAXB Classes from the above XSD in eclipse, it gives me errors related to
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
and
<xs:attribute name="xmlns:a" in Roles element
and
<xs:element name="a:string" type="xs:string"></xs:element>
So i removed them and added
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" targetNamespace="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified" attributeFormDefault="unqualified">
on the top.
How will the "Roles" from my XML be referenced in the XSD so that i can create a correct POJO class?
Taking reference from http://www.w3.org/TR/xmlschema-0/#ListDt, lists should be declared this way
<xsd:simpleType name="listOfMyIntType">
<xsd:list itemType="myInteger"/>
</xsd:simpleType>
I am not able to figure out how i can apply this to my XSD. Any help will be appreciated.
I think in xml in
<Roles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
the xmlns:a
is considered as attribute by the ide that generates xsd, that's why in generated schema you have
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
So I agree with deleting
<xs:attribute name="xmlns:a" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns" type="xs:string"></xs:attribute>
<xs:attribute name="xmlns:i" type="xs:string"></xs:attribute>
from xsd.
My only guess is try setting
targetNamespace="http://schemas.datacontract.org/2004/07/Interzoic.SSO.Shared"
as it is used explicitly in your xml

Refering another xsd element in another xsd?

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>

validating xml data in java

My transaction xml is shown below
<?xml version= "1.0"?>
<transactionlist>
<transaction action="c">
<transactionid>t004</transactionid>
<transactiondate>11/06/2013</transactiondate>
<merchantdetails>Sony wholesale Dealer</merchantdetails>
<itempurchased>3</itempurchased>
<amount>40399</amount>
<description>sony laptops</description>
</transaction>
<transaction action="d">
<transactionid>t003</transactionid>
</transaction>
<transaction action="u">
<transactionid>T001</transactionid>
<transactiondate>20/08/2013</transactiondate>
<merchantdetails>samsung Axses</merchantdetails>
<itempurchased>1</itempurchased>
<amount>40000</amount>
<description>samsung smart phone</description>
</transaction>
</transactionlist>
I have parsed the element itempurchased in above xml and stored it in integer variable. How to validate itempurchased only for numbers. that is i want to check whether itempurchased is number. pls provide suggestions
the best way should be validate xml against an xsd, where itempurchased would be of type xsd:int
below the 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="transactionlist">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="transaction">
<xs:complexType>
<xs:sequence>
<xs:element name="transactionid" type="xs:string" />
<xs:element minOccurs="0" name="transactiondate" type="xs:string" />
<xs:element minOccurs="0" name="merchantdetails" type="xs:string" />
<xs:element minOccurs="0" name="itempurchased" type="xs:int" />
<xs:element minOccurs="0" name="amount" type="xs:int" />
<xs:element minOccurs="0" name="description" type="xs:string" />
</xs:sequence>
<xs:attribute name="action" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here Validating XML against XSD the code for validate xml against xsd
If you are marshaling your xml to a java bean then you may try using the Java6 Bean Validation Framework. Read more about it here:
http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html
It is as simple as putting an annotation on your bean:
public class MyXMLBean {
#Max(10)
#Min(5)
private int itempurchased;
}
The above bean will allow setting the value of itempurchased between min and max values mentioned in the annotations.

Categories