Solved

XSD Schema and XML, newbie

Posted on 2015-02-03
11
126 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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