Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create an element with a restriction AND an attribute?

Status
Not open for further replies.

oracle70

MIS
Sep 12, 2003
15
LK
Hi,

I have this problem. I want to create an element in an XML schema that has a restriction AS WELL as an attribute. I tried several methods using SIMPLECONTENT AND simpletype but found out that you can create an element with an attribute BUT not a restriction. If you create an element with a restriction then you cannot have an attribute to it.
For example: How to create an element called quantity which should have an integer value between 50 and 100 and also have an attribute unit_of_measure which should be either kg or lbs?


Thanks & regards,
Francis
 
Create a ComplexType that has your attribute in it, then declare your element to be of that type. It will then allow you to set your restriction.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hi, I would be very grateful if you could give me an example. I have been trying this out for the last couple of hours on XMLspy but without any success.
 
Hi, I am trying to create the XML schema for following very simple XML file. I would be very grateful if you could specify the schema def for this.

---------------------

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<orders
xmlns:xsi =&quot; xsi:noNamespaceSchemaLocation =&quot;ele_with_attribs_and_restrictions.xsd&quot;
>
<!-- qty must be >0 and <100 -->
<!-- Unit must be either lbs or kgs -->
<qty unit=&quot;lbs&quot;>99</qty>
<qty unit=&quot;kgs&quot;>68</qty>


</orders>
 
Hi Chiph,

I did as you told me but I am unable to put the restriction on the element. Can u tell me how to in this code.

.XSD FILE
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xs:schema
xmlns:xs =&quot; elementFormDefault =&quot;qualified&quot;
attributeFormDefault =&quot;unqualified&quot;
>

<xs:complexType name=&quot;unit_type&quot;>
<xs:simpleContent>
<xs:extension base=&quot;xs:positiveInteger&quot;>

<xs:attribute name=&quot;unit&quot; use=&quot;required&quot;>
<xs:simpleType>
<xs:restriction base=&quot;xs:string&quot;>
<xs:enumeration value=&quot;lbs&quot;/>
<xs:enumeration value=&quot;kgs&quot;/>
<xs:enumeration value=&quot;stn&quot;/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>

</xs:extension>
</xs:simpleContent>
</xs:complexType>


<xs:element name=&quot;orders&quot;> <!-- ROOT ELEMENT -->
<xs:complexType>
<xs:sequence>

<xs:element name=&quot;qty&quot; type=&quot;unit_type&quot; >
</xs:element>
<xs:element name=&quot;qty2&quot; type=&quot;unit_type&quot;/>


</xs:sequence>
</xs:complexType>
</xs:element> <!-- END OF ROOT ELEMENT -->

</xs:schema>
.XML FILE
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<orders
xmlns:xsi =&quot; xsi:noNamespaceSchemaLocation =&quot;ele_with_attribs_and_restrictions.xsd&quot;
>
<!-- qty must be >0 and <100 -->
<!-- Unit must be either lbs or kgs -->
<qty unit=&quot;stn&quot;>44</qty>
<qty2 unit=&quot;lbs&quot;>66</qty2>



</orders>
 
GOT IT MAN FINALLY! IT'S LIKE THIS. FIRST CREATE A GLOBAL SIMPLE TYPE FOR YOUR ELEMENT RESTRICTION AND PUT THE ATTRIBUTE'S RESTRICTION IN THE ELEMENT DEFINITION. HERE ARE THE 2 FILE BELOW

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xs:schema xmlns:xs=&quot; elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;unqualified&quot;>

<xs:simpleType name=&quot;xxx&quot;>
<xs:restriction base=&quot;xs:positiveInteger&quot;>
<xs:maxInclusive value=&quot;1&quot;/>
<xs:maxInclusive value=&quot;50&quot;/>
</xs:restriction>
</xs:simpleType>

<xs:element name=&quot;testing_types&quot;> <!-- ROOT ELEMENT -->
<xs:complexType name=&quot;T1&quot; id=&quot;T1_ID1&quot;>
<xs:sequence>


<!-- complex ele containing ONLY text + attributes (simplecontent)-->
<xs:element name=&quot;shoesize&quot; maxOccurs=&quot;4&quot;>

<xs:complexType>
<xs:simpleContent>

<xs:extension base=&quot;xxx&quot;>
<xs:attribute name=&quot;country&quot;>
<xs:simpleType>
<xs:restriction base=&quot;xs:string&quot;>
<xs:enumeration value=&quot;france&quot;/>
<xs:enumeration value=&quot;sri lanka&quot;/>
<xs:enumeration value=&quot;germany&quot;/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>

</xs:simpleContent>
</xs:complexType>

</xs:element>

</xs:sequence>
</xs:complexType>
</xs:element> <!-- ROOT ELEMENT -->
</xs:schema>

<!--
* complexType elements can contain elements AND/OR attributes
* i.e. it could contain ATTRIBUTES only.
* The complexContent element defines extensions or restrictions on a complex type
that contains mixed content or elements only. WHAT ARE MIXED CONTENTS?
Mixed contents is ELEMENTS + TEXT. e.g. is a letter.



-->

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<testing_types xmlns:xsi=&quot; xsi:noNamespaceSchemaLocation=&quot;D:\Users\Channa\MSc\XML2\JACKPOT.xsd&quot;>
<shoesize country=&quot;france&quot;>50</shoesize>
<shoesize country=&quot;sri lanka&quot;>1</shoesize>
<shoesize country=&quot;germany&quot;>45</shoesize>
</testing_types>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top