Default value for text in SimpleContent of XSD - xsd

I'm trying to specify an element that extends a simple type (say xs:string) with an optional attribute in XSD 1.1. If the element's content/text is empty, I want it to take a default value instead. My current draft is
<xs:element name="pose" default="0 0 0 0 0 0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="relative_to" type="xs:string" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
What I expect is that <pose></pose> should become <pose>0 0 0 0 0 0</pose> and similarly, <pose relative-to="something" /> should become <pose relative-to="something">0 0 0 0 0 0</pose>
Is this the correct way to do so?
I'm asking, because I'm trying to generate data-bindings from this XSD (using xsData) and the generated bindings don't pick up the default I specified in xs:element. I assume this is due to me misspecifying the schema.

Related

XSD validation error - choice is invalid, misplaced, or occurs too often [duplicate]

This question already has answers here:
Where does xsd:attribute declaration go? (A problem was found starting at: attribute.)
(1 answer)
How to make type depend on attribute value using Conditional Type Assignment
(1 answer)
Value of XML element depends on values of other elements in XSD?
(1 answer)
Closed 17 days ago.
I have a problem with validation of the case that if attribute "brand" has value "x" then it is required to use attribute "model" otherwise attribute "model" should not be used.
XSD part which I am using for that is as below :
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name = "deviceList" type="deviceType"/>
<xs:complexType name="deviceType">
<xs:attribute name="brand" type="xs:string"/>
<xs:attribute name="model" type="xs:string" use="prohibited"/>
<xs:choice>
<xs:sequence>
<xs:attribute name="A" use="required" fixed="1"/>
<xs:attribute name="B" use="required"/>
</xs:sequence>
<xs:attribute name="A" use="required"/>
</xs:choice>
</xs:complexType>
</xs:schema>
When validating the result is :
The content of 'variableType' is invalid. Element 'choice' is invalid, misplaced, or occurs too often.
To be fair I don't know why it is not validating properly ...
XML part which I am trying to validate:
<?xml version="1.1"?>
<!-- <deviceMappingFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="XSD file.xsd"> -->
<deviceList brand="x" model="yyy( model required if brand=x , if other not in use))"/>

How to define Enumeration Constants in XSD?

I was looking to add system defined constants through an XSD. Here is a complex type that I have defined.
<xs:complexType name="app_job">
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="1"/>
<xs:element name="cron" type="xs:string" minOccurs="1"/>
<xs:element name="description" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
I am looking for adding system defined constants like
DAILY (1, "0 0 0 * * ?", "Daily Job")
HOURLY (2, "0 0 0/1 * * ?", "Hourly Job")
I was trying enumeration here
<xs:simpleType name="app_jobs">
<xs:restriction base="app_job">
<xs:enumeration id="1" cron="0 0 0 * * ?" description="Daily Job"/>
</xs:restriction>
</xs:simpleType>
But that does not seem right. Is there any way instance of the complex type with predefined values can be defined in same schema so that xsd2java can convert it to Java enum and I can use it in application? Instead of hand writing the enum, I wanted to source it from XSD.
Thanks in advance

JAXB: xjc and fixed values for attributes of derived types

I'm trying to create an xsd with a type hierarchy.
What I'm hoping to archieve is that the subtypes fill the base type's attributes with a certain value.
This is my best result:
<xs:complexType name="base_type">
<xs:attribute name="att" use="required" type="xs:string" />
</xs:complexType>
<xs:complexType name="type1_t">
<xs:complexContent>
<xs:restriction base="base_type">
<xs:attribute name="att" fixed="value1" use="required" type="xs:string" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="type2_t">
<xs:complexContent>
<xs:restriction base="base_type">
<xs:attribute name="att" fixed="value2" use="required" type="xs:string" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>
The "att" attribute is made available by base_type, and in elements of type1_t and type2_t, I'd like them set to "value1" and "value2", respectively.
The container element looks like this:
<xs:element name="root">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="type1" type="type1_t" />
<xs:element name="type2" type="type2_t" />
</xs:choice>
</xs:complexType>
</xs:element>
Now I'm running xjc to generate Java classes for this code, but the "fixed" attribute is not represented in what I get in the Type1T.java, which consists only of
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "type1_t")
public class Type1T extends BaseType { }
I would have expected
{
#Override public String getAtt() { return "value1"; }
}
?
That is (almost) what gets generated when the type is generated by itself (ie not as an extension of a base type).
I'm I doing something wrong? Is what I'm trying to do just not possible?
My goal is to automatically generate all java code from xsd; otherwise I suppose a way to implement this would be to code the fixed values in the created ObjectFactory methods for each class manually. However, then I would need to prevent ObjectFactory from getting generated every time...
Any hints? Tips? Thoughts?

XML schema for element whose simple type is driven by an attribute

I want to write an XML schema (xsd 1.1) for a document containing options. Each option has a name and a type (like boolean, integer, string, etc ...) and a datum matching that type. The list of types is fixed, but quiet long. (Only 3 are listed in listing 3 for simplicity.)
How do I do this without a ridiculous amount of repetition?
Use case 1
Here is a valid document for this schema..
Listing 1:
<abc:options>
<abc:option name="is-enabled" type="boolean">false</abc:option>
<abc:option name="wing-span" type="float">1.2</abc:option>
</abc:options>
Use case 2
This document is not valid for this schema because the simple type bit is wrong for the #type attribute.
<abc:options>
<abc:option name="is-enabled" type="boolean">24</abc:option>
<abc:option name="wing-span" type="float">this-is-not-a-number!</abc:option>
</abc:options>
What I have tried so far ...
Listing 3 is my attempt so far. But it is bad because I have to re-declare the #name attribute for each datum type. Is there a better solution? That is to say, one where I don't have to re-declare the #name attribute for each possible datum type.
Listing 3:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:abc="http://www.example.com"
targetNamespace="http://www.example.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="options">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="abc:option" type="option-Type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="option-Datum-Type">
<xs:restriction base="xs:string">
<xs:enumeration value="boolean"/>
<xs:enumeration value="integer"/>
<xs:enumeration value="float"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="option-Type-boolean">
<xs:simpleContent>
<xs:extension base="xs:boolean">
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="type" type="abc:option-Datum-Type" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="option-Type-string">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="type" type="abc:option-Datum-Type" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="option-Type-float">
<xs:simpleContent>
<xs:extension base="xs:double">
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="type" type="abc:option-Datum-Type" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="option-Type">
<xs:alternative test="#type='boolean'" type="abc:option-Type-boolean"/>
<xs:alternative test="#type='string'" type="abc:option-Type-string" />
<xs:alternative test="#type='float'" type="abc:option-Type-float" />
<xs:alternative type="xs:error"/>
</xs:complexType>
</xs:schema>
If the type can only be one of the atomic types you can use xs:assert like this:
<xs:complexType name="option-Type">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:token" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
<xs:assert
test="if (#type='boolean') then . castable as xs:boolean
else if (#type='float') then . castable as xs:float
else if (#type='int') then . castable as xs:int
else if (#type='string') then . castable as xs:string
else false()"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
Notes:
You don't need to declare any new type. If you want you can even also skip the declaration of the enumeration.
Using this approach you need a new line for every new possible type (you don't really need a new line but it is easy to read with every type in a different line).
You can use text() instead of . if you found it to be more clear
Notice how simply this approach would be if XPath 2.0 had an eval function similar to javascript and other languages eval function:
<xs:assert test="eval(text() || ' castable as xs:' || #type)"/>
I though an eval/parse function was going to be added to XPath 3.0 spec but I think it finally has not been added.
Unlike instance of you cannot use lists (*,+) other than ? with castable as operator. You can only use atomic types using this approach.
Cast to string string should always succed as the type is declared as xs:string.

xs:anyAttributes is rejecting an attribute

In my schema I have:
<xs:element name="html-script">
<xs:annotation>
<xs:documentation>Element used to specify pass-through Javascript</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:anyAttribute/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
In a document I have:
<html-attributes target="_new"/>
When I validate, I get ...
Validation error: cvc-complex-type.3.2.2: Attribute 'target' is not allowed to appear in element 'html-attributes'. at file:/Users/benson/x/btweb/web_2_0/./content/about-us/about-us.xml line 35 character 38
What am I missing?
Possible Typo? Your schema defines the element <html-script> whereas your document uses <html-attributes>.
Also try adding the processContents directive to your anyAttribute:
<xs:anyAttribute processContents="skip" />
or
<xs:anyAttribute processContents="lax" />

Resources