• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 792
  • Last Modified:

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

0
famoso
Asked:
famoso
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now