Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a Constant in XSD

Tags:

constants

xsd

Is there a way to define a constant value and use that constant in the preceeding XSD? I have a common value I want to use for various xs:element tag's maxOccurs attributes. Like constants in other languages, I want to make the change in one place should the value backing MyConst were to ever change.

<!-- Can I do this? -->
<ConstantValue id="MyConst" value="10"/>
...
<xs:element name="sandwich_meat" type="xs:string" minOccurs="0" maxOccurs="MyConst"/>
<xs:element name="sandwich_name" type="xs:string" minOccurs="0" maxOccurs="MyConst"/>
like image 796
Stealth Rabbi Avatar asked Jun 02 '11 17:06

Stealth Rabbi


People also ask

How can we define within an XSD?

Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.

How do you declare a repeated element in XSD?

If you want to only allow, say, 3 <url> elements, use maxOccurs="3" instead, and so forth. If you instead want to specify that all the elements in the sequence can appear more than once, you can set the maxOccurs attribute on the <xs:sequence> tag instead.

What does minOccurs mean in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What does REF mean in XSD?

ref. Optional. Refers to the name of another element. The ref attribute can include a namespace prefix. This attribute cannot be used if the parent element is the schema element.


2 Answers

You can try to define a simpleType with a restriction:

  <xs:simpleType name="AConstantHere">
    <xs:restriction base="xs:string">
      <xs:enumeration value="CONSTANT_VALUE_HERE"/>
    </xs:restriction>
  </xs:simpleType>

It allows only one value.

like image 109
zygimantus Avatar answered Nov 08 '22 18:11

zygimantus


No it is not allowed that way. However you can define your own type with a fixed value in it somewhere on top of your XSD (place dosen matters) and use that type for the elements.

like image 34
Faisal Nasim Avatar answered Nov 08 '22 18:11

Faisal Nasim