How to include an XML schema (.xsd) in a JSON schema? - xsd

I want to define a JSON API response using JSON Schema.
Embedded in part of the API response is a complete, well-formed, schema valid XML string. The XSD of this XML string is a given.
Two part question:
How do I include the XSD in the JSON Schema such that the JSON Schema will also require the XML string to be schema valid in order for the whole API response to be valid?
If this is not possible, does anyone have another suggestion how to include the XSD at least in the specification? I'm working in RAML 0.8.

How do I include the XSD in the JSON Schema such that the JSON Schema
will also require the XML string to be schema valid in order for the
whole API response to be valid?
You cannot. The only thing you can do is validate the JSON and then at a later point extract the XML and validate it separately.
If this is not possible, does anyone have another suggestion how to
include the XSD at least in the specification? I'm working in RAML
0.8.
I've only used Swagger, not RAML. Swagger is also based on JsonSchema.
The only thing you can do here is to include a detailed specification that the contained XML should be compliant against such-and-such an XSD. You can do this by using the "description" functionality in swagger (or equivalent if it exists in RAML). This allows you to create a description (which supports markdown) and attach it to any element in the definition

Related

How to count values in a flat file based on the spaces? [duplicate]

Since we can query on the XML file from C# (.NET), why do we need an XSD file? I know it is metadata file of particular XML file. We can specify the relationships in XSD, but what is its functioning then?
XML
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Customers>
<Customer CustomerID="GREAL">
<CompanyName>Great Lakes Food Market</CompanyName>
<ContactName>Howard Snyder</ContactName>
<ContactTitle>Marketing Manager</ContactTitle>
<Phone>(503) 555-7555</Phone>
<FullAddress>
<Address>2732 Baker Blvd.</Address>
<City>Eugene</City>
<Region>OR</Region>
<PostalCode>97403</PostalCode>
<Country>USA</Country>
</FullAddress>
</Customer>
</Customers>
<Orders>
<Order>
<CustomerID>GREAL</CustomerID>
<EmployeeID>6</EmployeeID>
<OrderDate>1997-05-06T00:00:00</OrderDate>
<RequiredDate>1997-05-20T00:00:00</RequiredDate>
<ShipInfo ShippedDate="1997-05-09T00:00:00">
<ShipVia>2</ShipVia>
<Freight>3.35</Freight>
<ShipName>Great Lakes Food Market</ShipName>
<ShipAddress>2732 Baker Blvd.</ShipAddress>
<ShipCity>Eugene</ShipCity>
<ShipRegion>OR</ShipRegion>
<ShipPostalCode>97403</ShipPostalCode>
<ShipCountry>USA</ShipCountry>
</ShipInfo>
</Order>
<Order>
<CustomerID>GREAL</CustomerID>
<EmployeeID>8</EmployeeID>
<OrderDate>1997-07-04T00:00:00</OrderDate>
<RequiredDate>1997-08-01T00:00:00</RequiredDate>
<ShipInfo ShippedDate="1997-07-14T00:00:00">
<ShipVia>2</ShipVia>
<Freight>4.42</Freight>
<ShipName>Great Lakes Food Market</ShipName>
<ShipAddress>2732 Baker Blvd.</ShipAddress>
<ShipCity>Eugene</ShipCity>
<ShipRegion>OR</ShipRegion>
<ShipPostalCode>97403</ShipPostalCode>
<ShipCountry>USA</ShipCountry>
</ShipInfo>
</Order>
</Orders>
</Root>
I want to get data from the Order elements according to a provided CustomerID.
Also: What is the purpose of giving the relationships in XSD?
XSD files are used to validate that XML files conform to a certain format.
In that respect they are similar to DTDs that existed before them.
The main difference between XSD and DTD is that XSD is written in XML and is considered easier to read and understand.
Without XML Schema (XSD file) an XML file is a relatively free set of elements and attributes. The XSD file defines which elements and attributes are permitted and in which order.
In general XML is a metalanguage. XSD files define specific languages within that metalanguage. For example, if your XSD file contains the definition of XHTML 1.0, then your XML file is required to fit XHTML 1.0 rather than some other format.
You mention C# in your question so it may help to think of as XSD as serving a similar role to a C# interface.
It defines what the XML should 'look like' in a similar way that an interface defines what a class should implement.
XSDs constrain the vocabulary and structure of XML documents.
Without an XSD, an XML document need only follow the rules for being well-formed as given in the W3C XML Recommendation.
With an XSD, an XML document must adhere to additional constraints placed upon the names and values of its elements and attributes in order to be considered valid against the XSD per the W3C XML Schema Recommendation.
XML is all about agreement, and XSDs provide the means for structuring and communicating the agreement beyond the basic definition of XML itself.
Also questions is: What is the purpose
of giving the relationships in xsd.
Suppose you want to generate some XML for an external party's tool, or similar - how would you know what structure it is allowed to follow to be used correctly for their tool? you write to a schema. Likewise if you want other people to use your tool, you would write a schema for them to follow. It may also be useful for validating your own XML.
Before understanding the XSD(XML Schema Definition) let me explain;
What is schema?
for example; emailID: peter#gmail
You can identify the above emailID is not valid because there is no #, .com or .net or .org.
We know the email schema it looks like peter#gmail.com.
Conclusion: Schema does not validate the data, It does the validation of structure.
XSD is actually one of the implementation of XML Schema. others we have relaxng
We use XSD to validate XML data.
An XSD is a formal contract that specifies how an XML document can be formed. It is often used to validate an XML document, or to generate code from.
An XSD file is an XML Schema Definition and it is used to provide a standard method of checking that a given XML document conforms to what you expect.
An .xsd file is called an XML schema. Via an XML schema, we may require a certain structure in a given XML - which elements in which order, how many times, with which attributes, how they are nested, etc. If we have a schema for our XML input, we can verify that it contains the data we need it to contain, and nothing else, with a few lines invoking a schema validator.
The xsd file is the schema of the xml file - it defines which elements may occur and their restrictions (like amount, order, boundaries, relationships,...)

Unmarshall to an existing model

Based on this example Keep child-parent relationship after unmarshalling I'd like to know if it's possible to unmarshall xml file into an existing model (not having the JAXB annotations). My idea is to directly unmarshall into Primefaces Menu Model (https://www.primefaces.org/docs/api/6.0/org/primefaces/model/menu/package-summary.html). Is it possible?
JAXB is configuration by exception, so if your model matches the xml, annotations are not necessary.
Here a blog post and a stackoverflow answer by Blaise Doughan on the topic.
It is possible (if your model is straightforward enough to be JAXB-compatible or with MOXy external mappings), but I would really not recommend it.
If you map to existing model, your XML representation becomes dependent on this existing model. And should the existing model change (like, you update the version of the library you use), you won't be able to unmarshal existing XML and will need migration mechanisms.
From my point of view, it is better to write an XML Schema and compile it to schema-derived classes. Then have a conversion routine to transfrom unmarshalled object structure to the target existing model.

CSV to XML trnformation using cloveretl

I want to transform my csv file to XML using cloveretl.
I gone through the basic tutorial, all explains abound direct mapping from csv to xml, csv header column names are used for xml element name.
I have one complex XSD, and I want to map csv to that XSD generated xml.
When I generate metadata using my XSD 213 fmt file generated in cloveretl.
How do map all these together ?
I saw an option to map individually, one csv metadata to one fmt. Like this I have to do 213 mapping and combine all ?
I assume you have two components: UniversalDataReader and XMLWriter. The edge between them should not have the metadata from the XSD schema (you do not have to extract metadata from the XSD at all), it should have metadata extracted from the input CSV file. Otherwise, you would not be able to read the file in the first place.
Then, in XMLWriter, you can set the XSD schema or create the mapping manually. For more information, see http://doc.cloveretl.com/documentation/UserGuide/topic/com.cloveretl.gui.docs/docs/extxmlwriter.html#xsd-mapping
If the CSV and the result XML are simple enough, you do not need the XSD schema at all.

Camel Jetty response is Object ID instead of XML generated by JAXB

I am working on an Apache Camel project. Basically the Jetty endpoint takes a Http Post request and the message goes through a few steps of transformation in the route. The last step of transformation is through JAXB, which converts Java object into XML. The Java DSL is below
final DataFormat jaxb = new JaxbDataFormat("sample");
from("jetty:http://localhost:8888/foo")
.unmarshal(format).split(body()).marshal(jaxb)
My problem is that when I send a POST request to the localhost URL, the HTTP response is string
[sample.Claims#b68e0e], not the XML I expected. This is the JAXB object ID. When I changed the DSL to
from("jetty:http://localhost:8888/foo")
.unmarshal(format).split(body()).marshal(jaxb).to("stream:out")
I can see the XML print out correctly in the stdout. I don't know how to make the HTTP Response to contain XML instead of the Object ID. Any help is appreciated.
Update:
I want to clarify what I try to accomplish. I need to convert a delimited string to an xml document. The post message to Jetty endpoint is a delimited string. The route first uses BeanIO to convert the string into a POJO and the POJO into XML using JAXB. Even though the post message is a single line string, I have to use split() because BeanIO by default dealing with multi-line flat file. I followed #Peter's suggestion by adding aggegate after the jaxb marshaling as below
from("jetty:http://localhost:8888/transformProxy/ECSProxy")
.unmarshal(format).split(body()).marshal(jaxb)
.aggregate(constant("1"),new MyAggregationStrategy())
.completionSize(1).to("stream:out");
but it does not seem to make any difference. I still get [sample.Claims#1a631c2] as the Http response body, while the stdout prints out the correct xml document. I am not sure how/when the response body of the jetty endpoint is set.
I made it work by moving the aggregate strategy as the second parameters of the split. For details, please see https://camel.apache.org/splitter.html. Search for the "Split aggregate request/reply sample"

XML Validation: XSD or Schematron?

I need to tighten validation on a moderately complex schema (SAML Metadata) comprising approx. 10 XSD files giving 1000 lines of schema definition. The validation should primarily require optional elements and attributes, restrict node and attribute contents to certain values, issue warnings on recommended or deprecated elements and attributes, and refuse anything that is not part of the schema, including stuff in extension elements of the original schema.
What is the best approach for this? Plain XSD, XSD + assertion, Schematron, or using some language and parse the document?

Resources