Given the following XML:
<results>
<result type="1">pass</result>
<result type="2">pass</result>
<result type="3">pass</result>
</results>
How do I create the XSD that forces the "result" elements to be ordered by the value of the "type" attribute? Also note, I don't necessarily need an ambiguous sort on the "type" attribute. I currently have them as enum values and I am expecting exactly one of each value in this specific order.
IMHO XSD doesn't supports what you want. You can not express relationship between attributes contained in elements of list
Related
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.
I have run a sql query with order by clause with 2 column names to get the results in given order. Now I copy the values from resultset to jaxb objects and marshall it to xml.
My requirement is to get the xml in the same order as in resultset.
Eg: Select * from emp order by date,employeeid;
output
id Date
4 22/01/2012
10 10/03/2012
Now I expect the xml as
<Employees>
<Employee id="4" date="22/01/2012">
<Employee id="10" date="10/03/2012">
</Employees>
Is there a way to get it done?
Regards
Harish
I have added new integer variable "order" in jaxbObject. While reading the resultset and updating the jaxbobject, I update the order variable with counter. So from the xml generated from marshalling the jaxb objects, I can rely on the "order" to list in the same order as sql query returns.
Order of id and date Attributes
You can use the propOrder attribute on the #XmlType annotation to specify an order for fields/properties mapped to XML elements. There isn't a way to specify the order in which XML attributes appear in the XML output (the order of attributes is not considered significant in XML).
Order of Employee Elements
The order in which Employee elements are marshalled out will depend upon the characteristics on the Collection used to store them in the Java model.
For More Information
http://blog.bdoughan.com/2012/08/jaxbs-xmltransient-and-property-order.html
My XML file has root element and then many item elements as root's children.
So it looks like this:
<root>
<item type=type1 id=1>...</item>
<item type=type1 id=2>...</item>
<item type=type2 id=3>...</item>
<item type=type3 id=4>...</item>
</root>
Every item element has attribute that says what type of item we are dealing with.
In current XSD
xs:element name="root" is as complexType which has sequnce of item complexType.
I'm using JAXB to map my XML file to Java objects. Now I have to get all items and according to their types, create new, specific object. It get more and more complicated, as each item type has its own set of fields (child nodes). Is there any chance I can tell JAXB (by XSD) that there are different types of items and according to item's name attribute create object which I need? So for each item there should be separate complexType (which would be mapped to java object).
It would be all ok if my XML looked like this:
<root>
<item1 id=1>...</item1>
<item1 id=2>...</item1>
<item2 id=3>...</item2>
<item3 id=4>...</item3>
</root>
If you can change the XML so that instead of the unqualified type attribute you can have a qualified type attribute xsi:type (xsi is the prefix associated with the XML Schema Instance namespace) then:
your schema needs to provide definitions for those types
That would be it...
If you can't (worth a try) maybe this post on SO could help you.
Is there a way in an xsd schema to require that an element have another element somewhere as a descendant?
For example, element parent requires a descendant desc. This is valid:
<parent>
<a>
<b>
<desc></desc>
</b>
</a>
</parent>
As is this:
<parent>
<c>
<desc></desc>
</c>
</parent>
but this isn't:
<a>
<parent>
<b/>
</parent>
</a>
The potential child elements for parent are many and complicates, so it would be difficult to enumerate every possible valid configuration.
Something like the key/selector schema elements seems like it would work, where I could provide an xpath expression defining the valid locations for desc element, but all of the examples I've found are aimed at matching up the value of attributes.
No, (almost) all XML Schema validation is shallow, called "local" in the spec. Here's one excerpt that emphasizes type validation as "local" validation.
Element Validated by Type If an
element information item is ·valid·
with respect to a ·type definition· as
per Element Locally Valid (Type)
(§3.3.4), [it is marked as] ·validated·.
The only exception is for the identity constraints like uniqueness and key-references which have a broad scope in an XML document but narrow uses.
I don't know if XSD supports what you are trying to do, but there is a work-around.
You could do complex validations with a two-step process:
First simply use your XSD schema for basic validation
Next use an XSLT which does more complex validations, and outputs the result of that validation
This may not plug in well to whatever framework you are working with, but might work well for (partially) custom code. It also has the advantage (over doing the extra validations in code) that you can publish both documents.
From a quick google search, one effort towards this end is Schematron. It actually foregos XSD entirely, and just uses XSLT. It appears to be a published standard:
http://www.schematron.com/
Let's say I can get XML like this:
<Property Name="Title"/>
<Property Name="Content"/>
<Property Name="Address"/>
<Source properties="Title,Content,Address"/>
How coud I validate the "properties" attribute of "Source", so that any composition of the above listed "Property" items could be checked? (For example: "Title", "Title,Content", all of these concatenations are correct, while "Title, URL" is not correct.)
You can't do that within XML Schema. You can do it with your own higher level of validation based on XSLT, XQuery or Schematron, for example.
xan is right; validating always means, to match a XML file against a given schema. But there is no schema involved here, your problem is instead, to read a data file, and validate later entries against earlier ones (if the box above is supposed to represent one file) or one data file against another data file (if the gap is supposed to be a file separator). Beyond that, a schema defines the structure of elements and attributes and optionally data types (values only, if there is a strict enumeration of valid values). Also no match here, instead you want to verify data against data. Sorry, the tool of a schema mismatches the problem to solve.