Reflexive Schema Validation & Definition

Can can someone help me on how to define a xsd for this.

I also want enforce validation at schema level such that based on the attribute value given, only the corresponding child tags will be displayed (reflexive based on the attribute value).

<?xml version="1.0" encoding="UTF-8"?> <root> <sample-tag type="one"> <one></one> </sample-tag> <sample-tag type="two"> <two></two> </sample-tag> <sample-tag type="three"> <three></three> </sample-tag> <sample-tag type="onetwo"> <one></one> <two></two> </sample-tag> </root>

-------------Problems Reply------------

Not possible with XSD. Check if this is possible with Schematron.

If I take your sample XML as an approximation, then strictly speaking there is one way to achieve the effect you want: it is called xsi:type. I personally dislike xsi:type, so I consider this answer rather an academic exercise.

If you agree with this XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sample-tag xsi:type="one">
<one></one>
</sample-tag>
<sample-tag xsi:type="two">
<two></two>
</sample-tag>
<sample-tag xsi:type="three">
<three></three>
</sample-tag>
<sample-tag xsi:type="onetwo">
<one></one>
<two></two>
</sample-tag>
</root>

... then this XSD would give you what you want:

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="sample-tag" type="sample-tag"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="sample-tag" abstract="true"/>
<xsd:complexType name="one">
<xsd:complexContent>
<xsd:extension base="sample-tag">
<xsd:sequence>
<xsd:element name="one"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="two">
<xsd:complexContent>
<xsd:extension base="sample-tag">
<xsd:sequence>
<xsd:element name="two"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="three">
<xsd:complexContent>
<xsd:extension base="sample-tag">
<xsd:sequence>
<xsd:element name="three"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="onetwo">
<xsd:complexContent>
<xsd:extension base="sample-tag">
<xsd:sequence>
<xsd:element name="one"/>
<xsd:element name="two"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>

If xsi:type is out of question, and XSD 1.1, then you have to go with @Pangea's answer.

Category:xsd Views:0 Time:2012-03-26
Tags: xsd

Related post

  • Custom Schema Validation behaviour - XmlReaderSettings 2009-01-23

    I am parsing XML with an XMLReader using a XMLReaderSettings object with the event handling setup to carry out schema validation where appropriate. However it seems that the error catching only occurs once per level of XML. This means that any subseq

  • Tool for XML XSD schema validation 2009-02-21

    Does anybody know of a command line tool for validating XML with XSD schema? --------------Solutions------------- xmllint from the Libxml project xmllint --schema schema.xsd doc.xml On http://www.w3.org/XML/Schema under "Tools" you should find the on

  • Schema validation error / Thread safety of XmlSchemaSet? 2009-09-28

    Good afternoon, An XML schema validation snippet is working fine on development/q&a environments, but is yielding some odd validation results in Production. The usual suspect would be that the code is unsafe for threading, and that the additional

  • Schema validation, how to display user friendly validation messages? 2009-09-28

    Is there a way to avoid or set up a schema to display better user friendly messages? I am parsing the string and using reg ex to interpret them, but there might be a better way. Ex. "cvc-complex-type.2.4.b: The content of element 'node' is not comple

  • Complete XML Schema Validation 2009-10-18

    I am looking for a tool that will tell me all of the XML Schema validation failures. All the other tools I have looked at so just tell me the first couple, and then I have to fix those before it will tell me the next errors. I realize that some error

  • JSON schema validation using java 2010-03-23

    I'm writing some acceptance tests for a java webapp that returns JSON objects. I would like to verify that the JSON returned validates against a schema. Can anyone suggest any tools for this? --------------Solutions------------- The JSON Tools projec

  • how can i unmarshall in jaxb and enjoy the schema validation without using an explicit schema file 2010-04-08

    I am using jaxb for my application configurations I feel like I am doing something really crooked and I am looking for a way to not need an actual file or this transaction. As you can see in code I: 1.create a schema into a file from my JaxbContext (

  • Database Schema Validation - deployable/compilable 2010-05-06

    Looking for a tool that will allow building database schema validation that can be compiled into a standalong tool or as a module to be used in another application. The scenario is - a client/server application that is sold to customers, and maintain

  • Multiple schema validation in Java 2010-05-18

    I am trying to do multiple schema validation in Java. I don't understand where I am doing wrong. Any help will be appreciated. abc.xsd <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xn="

  • Hibernate Schema Validation Fails on Oracle Table Synonyms 2010-06-11

    I'm developing a Java web application that uses Hibernate (annotations-based) for persisting entities to an Oracle 11g database. The DBA created synonyms for the tables and requested that I use these synonyms instead of the physical tables. (Eg: Tabl

  • "No suitable XML Schema Validator could be found" 2010-08-07

    Whilst trying to update through the Android AVD Manager, I receive the message: "No suitable XML Schema Validator could be found in your Java environment" It therefore can't parse the repository.xml and I can't download any updates. How can I get rid

  • Solve: XML Schema validation ignored when fragment-level conformance is enabled? 2010-09-03

    From my sojourn with XML and Schema validation, it seems that when fragment-level conformance is enabled for an XMLReader instance, the XML source stops being validated against any included Schemas. However I cannot verify this from the MSDN document

  • XML Schema / Validation Query - Missing Elements 2010-11-30

    If you have an XML schema which dictates an element has a default value, and then you have an XML file following that schema which completely omits that element is it still valid? i.e. if the element is missing does the validator just say ok the elem

  • SQL Server 2008: What is lazy schema validation? 2010-12-15

    Saw this option when I made a linked server --------------Solutions------------- Basically it just enables you to improve performance for remote tables by not requesting meta data for each request, only when data is actually needed from the tables. L

  • BizTalk message fails schema validation but processes just fine 2011-01-05

    I have a BizTalk schema that looks something like this: <Root> <Header> <SomeData></SomeData> <Detail> <MoreData1></MoreData1> <MoreData2></MoreData2> <MoreData3></MoreData3> </Deta

  • JSON schema validation 2011-01-13

    Is there a stable library that can validate JSON against a schema? json-schema.org provides a list of implementations. Notably C and C++ are missing. Is there a reason I can't easily find a C++ JSON schema validator? Doesn't anyone else want a quick

  • Schema Validator "nillable" For Child Nodes Doesn't Work? 2011-01-14

    For some reason I can't quite get nillable to work correctly with the .Net schema validator. I'm trying to find a way to make a parent node optional but at the same time prevent an empty node from passing through the validator. Here's the current ele

  • schema validation with trax 2011-03-15

    If I want to apply schema validation to XML read when apply XSLT with the Transformer interface, do I need to first build a DOM with a DOMSource with validation enabled, or is there some way to persuade it to do this with a StreamSource? ------------

  • XMLSerialization and XML Schema validation 2011-03-28

    Is it possible to use the XML Schema validation and XMLSerializer together? My project currently uses XMLSerializer. To validate the schema, we are programatically checking the values like: if(String.IsNullOrEmpty(person.Name)) throw new Exception();

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.088 (s). 11 q(s)