Avatar of mmarinov
mmarinov

asked on 

Validate node in Xml file using XSD schema against value of attribute

Hello to all experts.
The problem that i have is related with this xml:

<RootNode>
 <SubNode type="test1">
  <AgainSubNode />
  <AgainSubNode />
 </SubNode>
 <SubNode type="test2">
  <AgainSubNode />
  <AgainSubNode />
 </SubNode>

I want to validate the xml using only xsd schema. The validation i have problem with is validating the xml file against the value of the attribute type. I have declared it as

  <xs:simpleType name="Types">
    <xs:restriction base="xs:string">
      <xs:enumeration value="test1" />
      <xs:enumeration value="test2" />
    </xs:restriction>
  </xs:simpleType>
</RootNode>

Is it possible to write something like this:
- if type = test1 then SubNode must have only one AgainSubNode node
- if type = test2 then subNode must have at least 3 AgainSubNode nodes

Thanks
.NET ProgrammingXML

Avatar of undefined
Last Comment
Gertone (Geert Bormans)
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

This cannot be done with W3C schema alone

You want the content type to be different depending on the value of an attribute
That is a so called "co-occurence constraint"
You cannot model that in W3C schema

For that you should use Schematron (www.schematron.com)
or add a validation layer using XSLT or something

cheers

Geert
Avatar of mmarinov
mmarinov

ASKER

Thanks Geert,

Do you have some links that i can see how xslt validation layer can be used.
Also is Schematron is dependent on the OS? I have requirement to use w3c schema and if I have to use something in addition it must be ok to run on windows, mac, linux

Regards
Martin
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of mmarinov
mmarinov

ASKER

Thank you geert
I've got your point!

Martin
welcome
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo