Solved

XSD Schema and XML, newbie

Posted on 2015-02-03
11
118 Views
Last Modified: 2015-02-05
Hi,
I have XML with one element having value in percentage, like:
      <Contrib_Percent>15.5%</Contrib_Percent>
How do I define that in XSD? My knowledge in XSD is close to zero.
I also have a requirement that :
If Element A has value, Element B must have value, otherwise it can be empty value.
How do I define such relationship in XSD?
Is Visual Studio 2010 good tool for this or can you suggest?
Thanks!
0
Comment
Question by:vakils
  • 7
  • 4
11 Comments
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 40587531
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:simpleType name="percentage">
        <xs:restriction base="xs:string">
            <xs:pattern value="\d+(\.\d+)?%"/>
        </xs:restriction>
    </xs:simpleType>
    
    <xs:element name="Contrib_Percent" type="percentage"></xs:element>
</xs:schema>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40587532
I made a simple type for percentage
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 40587534
If Element A has value, Element B must have value, otherwise it can be empty value.
 How do I define such relationship in XSD?

You can't do that in XSD 1.0, you will need XSD 1.1 for that
Then you can use xs:assert
First check if your validator supports xsd 1.1

I like www.oxygenxml.com a lot for xsd development, highly recommended
0
 

Author Comment

by:vakils
ID: 40587595
Thanks. Works!
How can I restrict value between 0 & 100?
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 40588127
   <xs:simpleType name="percentage">
        <xs:restriction base="xs:string">
            <xs:pattern value="\d{1,2}(\.\d+)?%"/>
            <xs:pattern value="100(.0*)?%"/>
        </xs:restriction>
    </xs:simpleType>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40588143
And here is the XSD1.1 (note the vc:minVersion="1.1")
it requires B to have a value when A has a value

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
	<xs:element name="root">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="A"/>
				<xs:element name="B"/>
			</xs:sequence>
			<xs:assert test="not(normalize-space(A)) or normalize-space(B)"/>
		</xs:complexType>
	</xs:element>
</xs:schema>

Open in new window

0
 

Author Comment

by:vakils
ID: 40590265
Thanks! Works! You are real Guru! I downloaded oxygenxml.com.
One question, how do you write in xsd if a parent element is null:
<Contract_Information>
    <Contract_Year>2011</Contract_Year>
      <Product_Information/>  ---> this is null, how to define in xsd?
    	
      <Employee_Information>
          <Employee_Number>123456</Employee_Number>
          <SSN>123456789</SSN>

Open in new window

0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 40590533
You can explicitely state it is empty,
but best approach is to leave the declaration empty

<xs:element name="Product_Information"></xs:element>

If you add a complexType, you can add xs:attribute declarations

if it is empty but can have child elements, make the child elements optional
0
 

Author Comment

by:vakils
ID: 40592720
I've requested that this question be closed as follows:

Accepted answer: 125 points for Geert Bormans's comment #a40588143
Assisted answer: 125 points for Geert Bormans's comment #a40587531
Assisted answer: 125 points for Geert Bormans's comment #a40587534
Assisted answer: 0 points for vakils's comment #a40587595
Assisted answer: 0 points for vakils's comment #a40590265
Assisted answer: 125 points for Geert Bormans's comment #a40590533

for the following reason:

Thanks for your excellent help. I learned a lot and was able to finish my project in time!
0
 

Author Closing Comment

by:vakils
ID: 40592721
Thanks for your excellent help. I learned a lot and was able to finish my project in time!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40592725
welcome
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now