XSD Verify attribute is set on only one element - xsd

I have a pre-existing XML that I'm writing a XSD for. The relevant section is basically:
<color a='1' default='true'>Red</foo>
<color a='2'>Yellow</foo>
<color a='3'>Blue</foo>
I want to validate that only one of the foo elements has an attribute default='true'. Note the other elements can either have default='false' or not have it at all (in which case it defaults to false).
I tried to use a key but it doesn't work in the example above because there's multiple default='false' values which is not unique.
Can such validation be done with XSD 1.0?
(I can't change the XML format unfortunately.)

Can such validation be done with XSD 1.0?
No. I suspect that XSD 1.1 could do it, but 1.0 defintely cannot.

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

Element dependency on sibling element value

I need to define a validation rule under XML Schema 1.0 that allows an element to occur (once) among a set of sibling elements only if another specific sibling has a certain value.
For example, given the instance XML document snippet,
<root>
<parent>
<child1>A</child1>
</parent>
<parent>
<child1>B</child1>
<chlld2>C</child2>
</parent>
</root>
I'd like the rule to allow the child2 element to occur only if the required child1 element has a value of 'B', otherwise, the child1 element should occur by itself under a given parent.
This is quite easy to achieve under XML Schema 1.1 using an xs:assert, but the solution under version 1.0 evades me.
Any insights are most appreciated.
The usual approach in XSD 1.0 is to design the XML differently: if we have one particular value B for child1 which makes the occurrence of child2 possible, then we can split child1 into two element types: child1-notB and child1-B. And since in the case of child1-B we know the value, the value doesn't actually need to be present. The XML becomes:
<root>
<parent>
<child1-notB>A</child1-notB>
</parent>
<parent>
<child1-B/>
<chlld2>C</child2>
</parent>
</root>
It's simple to write a content model in which the parent element contains either a child1-notB or a child1-B followed by an optional child2.
As Dijkgraaf has already observed, the specific design you describe cannot be expressed in XSD 1.0. XSD 1.1 added assertions in part because so many people want designs like the one you describe, in which two quite different elements, which have quite different effects on what is and is not allowed, are nevertheless given the same name so as to mask their difference in meaning, instead of being called by different names to make their difference in semantics explicit.

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.

setting minOccurs-value with the value of another attribute in a xsd-file

I am writing a xsd-schema file and I need to ensure that a value of an attribute (positve integer) is also the value of minOccurs and maxOccurs of another attribute. Is this somehow possible?
Not with XSD 1.0 alone.
If you could use another schema language with XSD 1.0, Schematron can do it. If you can move to XSD 1.1, an xsd:assert would do the same thing here (as the Schematron).

how to write xml schema(XSD)

I want to right one XML schema(XSD)
in which
element have multiple occurences and have one attribute ID
I want validate if borrower's ID attribute have a value equal to 1 then all its subelement must have some value.
is this possible with XSD?
Please suggest me if their is a way to achieve this
thanks
Not possible as far as I can remember, but it is totally OK to first validate the XML using XSD and then have some additional, application-specific validation logic on top of that.

Resources