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

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,...)

Related

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

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

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?

Where is the XSD file for "http://www.w3.org/2001/XMLSchema-instance"?

Where is the XSD schema definition file for the namespace "http://www.w3.org/2001/XMLSchema-instance"?
Strange it may sound, but the XML schema for http://www.w3.org/2001/XMLSchema-instance namespace does exist and is found exactly by the very URL denoted by the namespace URI:
http://www.w3.org/2001/XMLSchema-instance
For a proof, just open that link (URL) in an HTML browser (e.g. FireFox).
You will probably see some HTML text, like: "XML Schema instance namespace ...".
Then, save that 'HTML' as a file on your computer (e.g. File | Save Page As).
When you look into this file, you will see that it is not HTML at all. Rather, it is a complete XML schema for that namespace!
Equally, you can import the http://www.w3.org/2001/XMLSchema-instance namespace into your own schema as the following:
<xs:import namespace="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://www.w3.org/2001/XMLSchema-instance"/>
See also this question: Error while parsing xsd using xjc, which although sounds very differently, actually very much related to the same problem.
Just to add fuel to the fire -- many XML tools have knowledge of http://www.w3.org/2001/XMLSchema-instance baked-in, so it looks like you never need the schema at all. In fact, you need the schema if you are using an XML tool that does not bake-in this knowledge.
So is for that reason that we find actually always beginning of xml documents where there ins't xml-schema xsd declaration at all? For example like this:
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
Here is some updated information on this topic.
XSD 1.1 part 1 §2.6 states:
XML Schema Definition Language: Structures defines several attributes
for direct use in any XML documents. These attributes are in the
schema instance namespace (http://www.w3.org/2001/XMLSchema-instance)
described in The Schema Instance Namespace (xsi) (§1.3.1.2) above. All
schema processors must have appropriate attribute declarations for
these attributes built in.
Further, §3.2.6.4 says:
The {target namespace} of an attribute declaration, whether local or
top-level, must not match http://www.w3.org/2001/XMLSchema-instance
(unless it is one of the four built-in declarations given in the next
section). Note: This reinforces the special status of these
attributes, so that they not only need not be declared to be allowed
in instances, but in consequence of the rule just given must not be
declared.
So, you can't declare attributes such as xsi:type or xsi:schemaLocation in a schema document, and therefore you can't import a schema document that attempts to declare such attributes.
This of course is XSD 1.1 and therefore doesn't directly constrain an XSD 1.0 processor. However, it's one of the many areas where XSD 1.1 issues guidance for cases where XSD 1.0 said nothing, and where different implementations went off in different directions.

Does JAXB support reading xml tags dynamcally

Is there any method in jaxb to read the xml tags dynamically.For example i have two xml files both have diffrent tagnames and attributes.I want to read these two xml files using a single method.I don want to use getelementByTagname and all.All i need to read the xml tags dynamically.Does JAXB support this or there is any other concepts which can do this.Thanks in advance
You can create a single JAXBContext that represents multiple models. As long as the root objects are annotated with #XmlRootElement (or #XmlElementDecl) then your JAXB (JSR-222) implementation will instantiate the correct objects based on the XML input.
http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html

Marshalling the same object in two different ways using standard JAXB?

I have a Person class (annotated with #XmlRootElement) in Java with two properties (name, birthday), which I need to marshal in two different ways, generating two different XML files.
In the first file, the Person object should contain only the name property:
<Person>
<name>John Doe</name>
</Person>
In the second file, the Person object should contain all properties.
<Person>
<name>John Doe</name>
<birthday>1980-01-01</birthday>
</Person>
Is there a way to achieve this with JAXB?
Regards,
Jochen
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
You can use JAXB annotations to provide one representation, and leverage MOXy's external binding document to provide alternate representations.
Use Case 1 - Minor Differences Between Representations
By default MOXy's external mapping document is used to override metadata provided by annotations. This allows you to tweak the representation between versions. Below is a link to an answer I gave with a detailed example:
https://stackoverflow.com/a/9432435/383861
Use Case 2 - Major Differences Between Representations
MOXy's external mapping document can also be used to override all the annotations. This is useful when the representations are not related. Below is a link to an example where I map the same object model to both the Google and Yahoo weather services:
http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
For More Information
http://blog.bdoughan.com/search/label/jaxb.properties
http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html

Resources