Thursday, 15 September 2011

xsd - In XML Schema complex type, how does restriction influence set of allowed attributes? -



xsd - In XML Schema complex type, how does restriction influence set of allowed attributes? -

so xml schema restriction can derive new complex type has subset of elements of parent type. example, if have base of operations type:

<complextype name="basetype"> <complexcontent> <restriction base="anytype"> <attribute name="number" type="decimal"/> <attribute name="quantity" type="positiveinteger"/> <sequence> <element name="first" minoccurs="0" maxoccurs="1" type="string"/> <element name="second" minoccurs="0" maxoccurs="1" type="string"/> </sequence> </restriction> </complexcontent> </complextype>

and create next new type:

<complextype name="newtype"> <complexcontent> <restriction base="anytype"> <attribute name="number" type="decimal"/> <sequence> <element name="first" type="string"/> </sequence> </restriction> </complexcontent> </complextype>

my question attributes , elements allowed new type? number , first? occurs limits on first element? default (minoccurs="1" maxoccurs="1") or parent (minoccurs="0" maxoccurs="1")?

and if have additional type:

<complextype name="newtype2"> <complexcontent> <restriction base="anytype"> <sequence> <element name="first" type="string"/> </sequence> </restriction> </complexcontent> </complextype>

which attributes allowed here?

in restriction must include elements , attributes, set declared in base of operations type, wish allow in new type. element declarations in restriction override previous ones.

your schema correct. within complextype element, sequence must appear before attribute declaration. if have schema:

<?xml version="1.0" encoding="utf-8"?> <schema xmlns="http://www.w3.org/2001/xmlschema" elementformdefault="qualified" targetnamespace="tns" xmlns:tns="tns"> <complextype name="basetype"> <complexcontent> <restriction base="anytype"> <sequence> <element name="first" minoccurs="0" maxoccurs="1" type="string"/> <element name="second" minoccurs="0" maxoccurs="1" type="string"/> </sequence> <attribute name="number" type="decimal"/> <attribute name="quantity" type="positiveinteger"/> </restriction> </complexcontent> </complextype> <complextype name="newtype"> <complexcontent> <restriction base="tns:basetype"> <!-- restriction of basetype --> <sequence> <element name="first" type="string"/> </sequence> <!-- attribute declarations not necessary, since allow same types --> </restriction> </complexcontent> </complextype> <element name="root"> <complextype> <choice maxoccurs="unbounded"> <element name="base" type="tns:basetype" minoccurs="0"/> <element name="new" type="tns:newtype" minoccurs="0"/> </choice> </complextype> </element> </schema>

it validate 2 first <base> elements, fail validation in 2 <new> elements reasons explained in comments:

<base number="123.5" quantity="3"> <!-- validate --> <first></first> <second></second> </base> <base> <!-- validate: min occurs <first> , <second> 0 ok --> </base> <new number="123.5"> <!-- fail validation: min occurs of <first> 1 --> </new> <new number="123.5" quantity="3"> <!-- validate: quantity attribute inherited --> <first></first> <second></second> <!-- fail validation: no <second> element allowed --> </new>

the element , attribute declarations in derived type must equal or more restrictive: can't have in derived type:

<element name="first" minoccurs="0" maxoccurs="unbounded" type="string"/>

because in base of operations type there no explicit maxoccurs value 1, , unbounded wouldn't restriction. if declare atributes decimal, example, in base of operations type, can declare 1 time again integer restrict more, not declare attribute integer decimal since wouldn't restriction.

xml xsd

No comments:

Post a Comment