|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Conditions in XSD
I have an XML with following tags
<root tag> <A>xxx </A> <B>xxx </B> <C>xxx </C> <D>xxx </D> <E>xxx </E> </root tag> I want to mention in my XSD, the following things: 1) If tag <A>is present, then <B> is optional and the tags <C>, <D>, <E> should not be present. 2) If tag <C>is present, then <D> is optional and the tags <A>, <B> should not be present. How to do it?
__________________
Contact info: Primary email: advanced.programmer@gmail.com MSN/email: superprg@hotmail.com AIM: superprg |
|
#2
|
||||
|
||||
|
hi superprogrammer,
hope it helps. Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:group ref="t1"/>
<xs:group ref="t2"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:group name="t1">
<xs:sequence>
<xs:element name="a"/>
<xs:element name="b" minOccurs="0"/>
</xs:sequence>
</xs:group>
<xs:group name="t2">
<xs:sequence>
<xs:element name="c"/>
<xs:element name="d" minOccurs="0"/>
<xs:element name="e" />
</xs:sequence>
</xs:group>
</xs:schema>
cheers, Sandeep Intrasphere |
|
#3
|
|||
|
|||
|
regular expressions
ab? | cd?e? | b?d?e? are you allowed to have elements bde, or no elements at all, or b and e, but not d? |
|
#4
|
||||
|
||||
|
hi superprogrammer,
schema provided above would only allow Code:
<root_tag> <A>xxx </A> <B>xxx </B> (optional) </root_tag> or Code:
<root_tag> <C>xxx </C> <D>xxx </D> (optional) <E>xxx </E> </root_tag> I hope this is what you wanted ? cheers mate, sandeep |
|
#5
|
|||
|
|||
|
question about conditions
Hi
My question is a bit similar to superprg question. My xml looks as follows: <mySth> <a></a> <b></b> <c_is_optional></c_is_optional> </mySth> <otherSth> <a></a> <b></b> </otherSth> How can I do make my xsd guarantee that <c_is_optional> can only occur in element with prefix 'my'? It cannot occur in element starting with 'other'. Anna |
|
#6
|
|||
|
|||
|
About Conditions
Hi,
I am new XSD's. Could anyone guide how to draw an XSD with the following conditions. My requirement i slike this. <root> <a/> <b/> <c/> <d/> <e/> </root> Here If <c> is present then only <d> should be present otherwize <d> should not. How to make this possible. Help would be appreciated. Thanks. |
|
#7
|
|||
|
|||
|
-> sikorka.
Make multiple types. done. ->newtechie Have a nested sequence. eg. something like: (pseudo) Code:
<sequence>
<a>
<b>
<sequence minoccurs=0>
<c>
<d>
</sequence>
<e>
</sequence>
add minoccurs=0 to the other elements (a b e) as necessary Assuming you mean the "if and only if" case. |
|
#8
|
|||
|
|||
|
Conditions in XSD
Yep, got it....
Thank you very much. Quote:
|
|
#9
|
|||
|
|||
|
Hi, I have a similiar but different problem. The condition doesn't refers to an element, but a value of an element, is it possible ?
If element 'A' has value other than '1', then element B has to appear: <Data> <A>2</A> <B>yes</B> </Data> But if Element 'A' has the value '1', then Element 'B' must not appear: <Data> <A>1</A> </Data> Thank you in advanced ! |
|
#10
|
|||
|
|||
|
hi all
I have two question: 1. For schema bellow : Code:
<xsd:schema>
...
<xsd:element name="root" nillable="false">
<xsd:complexType>
<xsd:all>
<xsd:element maxOccurs="1" minOccurs="1" name="el1" type="xsd:int"/>
<xsd:element maxOccurs="1" minOccurs="1" name="el2" type="xsd:int"/>
</xsd:all>
<xsd:attribute name="m" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="r"/>
<xsd:enumeration value="ra"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>
How will have to be modified that : - if mode ( attribute m ) value is r , element el2 to be mandatory( minOccurs to be 1 ) - if mode ( attribute m ) value is ra, element el2 to be optional ( minOccurs to be 0 ) 2.Second question : I have this element Code:
<xsd:element maxOccurs="unbounded" minOccurs="0" name="Description" nillable="true">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="Sequence" type="types:ValidInt_1_4" use="required"/>
<xsd:attribute name="Use" type="enum:ArticleDescriptionTypeEnumeration" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
where validInt1_4 is a integer from 1 to 4, and ArticleDescriptionTypeEnumeration is a enumeration with 2 values, TYPE1 and TYPE2 Until now all were fine; but now i have to put one condition more, i can have only maxim 2 descriptions of type TYPE1 and max 2 descriptions of type TYPE2. How cand be done this ? Thanks all and best regards, Marian |
|
#11
|
|||
|
|||
|
arm4n, play4fun q1: no. Cooccurence constraint. You'd need another validation method besides xml schema.
q2, can you instead limit Description with maxoccurs = 4? |
|
#12
|
|||
|
|||
|
XSD constraints / validations
Hi techies,
I hv one question to share.. on xsd's here is the prob desc I have a element "Name" which has enum values say "kumar" and "Miller" and i had another element "Address" with enum values "Inida" and "US" Now when I create xml file and pass Name = Kumar and Address= "US", when Validates it shld throw me err. and when I pass Name=Kumar and Address = India it shld validate the xml file. Now question is how can I achieve this..? Please post ur suggestions. Regards Bharat |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Conditions in XSD |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|