XML XSD How to define a string and decimal

I need to be able to define 2 data types in my xsd and make an element optional.

In the xml I have a V4 node.

I want my xml to still validate with the xsd when:
  * V4 is optional - I have minOccurs="0"
  * Is empty - Like <V4></V4> or <V4/>
  * A string - Like <V4>Hello</V4>
  * A decimal - Like <V4>0.00</V4> or <V4>0</V4>

I'm a newbie to xsd.

xml:
 
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="request_alltags_20080129_x.xsd">
	<Property>
		<StateQuestions>
			<V4>0</V4>
		</StateQuestions>
	</Property>
</Request>
 
 
xsd:
 
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="Request">
	</xs:element>
	<xs:element name="Property">
		<xs:complexType>
			<xs:sequence>
				<xs:element minOccurs="0" ref="StateQuestions"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="StateQuestions">
		<xs:complexType>
			<xs:sequence>
				<xs:element minOccurs="0" ref="V4"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="V4">
		<xs:simpleType>
			<xs:restriction base="xs:decimal"></xs:restriction>
		</xs:simpleType>
	</xs:element>
</xs:schema>

Open in new window

famosoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
   <xs:element name="V4">
        <xs:simpleType>
            <xs:union>
                <xs:simpleType>
                    <xs:restriction base="xs:decimal"></xs:restriction>
                </xs:simpleType>
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:pattern value=".*"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:union>
        </xs:simpleType>
    </xs:element>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert BormansInformation ArchitectCommented:
I made it a union of a string type and a decimal (so both types are allowed)
I restricted the xs:string type with a pattern ".*", meaning it can also have a zero length string, so empty elements are allowed

cheers

Geert
0
famosoAuthor Commented:
Much appreciated!
0
Geert BormansInformation ArchitectCommented:
welcome
0
famosoAuthor Commented:
I ended up with this because I didn't actually want a string allowed but just an empty element.  This was my confusion so I changed the patter to this:

      <xs:element name="V4">
            <xs:simpleType>
                  <xs:union>
                        <xs:simpleType>
                              <xs:restriction base="xs:decimal"/>
                        </xs:simpleType>
                        <xs:simpleType>
                              <xs:restriction base="xs:string">
                                    <xs:pattern value=""/>
                              </xs:restriction>
                        </xs:simpleType>
                  </xs:union>
            </xs:simpleType>
      </xs:element>


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.