Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XSD Schema and XML, newbie

Posted on 2015-02-03
11
Medium Priority
?
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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