Solved

XSD Schema and XML, newbie

Posted on 2015-02-03
11
122 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RSS Feed Parsing for Images 5 61
Dialogbox API leak? 18 82
Using MS Code on my Mac 6 66
Post xml via PHP Soap Client 3 38
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

18 Experts available now in Live!

Get 1:1 Help Now