Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XSD Schema and XML, newbie

Posted on 2015-02-03
11
Medium Priority
?
137 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 2000 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 2000 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

773 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