Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Use of required and optional

Reply
Thread Tools

Use of required and optional

 
 
LJ
Guest
Posts: n/a
 
      02-08-2006
I am trying to define two xml attribute in my xsd so that they are
mutual exclusive and one of them is required.

For example, if I have two attributes, they are either

<xsd:attribute name="p" type="xsd:string" use="required" />
<xsd:attribute name="c" type="xsd:string" use="optional" />
or

<xsd:attribute name="p" type="xsd:string" use="optional" />
<xsd:attribute name="c" type="xsd:string" use="required" />

how can I specify this in the xsd?

Thx

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-08-2006


LJ wrote:

> I am trying to define two xml attribute in my xsd so that they are
> mutual exclusive and one of them is required.
>
> For example, if I have two attributes, they are either
>
> <xsd:attribute name="p" type="xsd:string" use="required" />
> <xsd:attribute name="c" type="xsd:string" use="optional" />
> or
>
> <xsd:attribute name="p" type="xsd:string" use="optional" />
> <xsd:attribute name="c" type="xsd:string" use="required" />
>
> how can I specify this in the xsd?


XSD does not allow you to define such constraints between different
attributes.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Stan Kitsis [MSFT]
Guest
Posts: n/a
 
      02-08-2006
You have a few options.
1. If they are trully mutually exclusive, you might be able to only use one
of them
2. Create two attributes: type (which can be either p or c) and value (which
will be a string representing the value)
3. Convert your attributes to elements and use <xs:choice>

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"LJ" <> wrote in message
news: oups.com...
>I am trying to define two xml attribute in my xsd so that they are
> mutual exclusive and one of them is required.
>
> For example, if I have two attributes, they are either
>
> <xsd:attribute name="p" type="xsd:string" use="required" />
> <xsd:attribute name="c" type="xsd:string" use="optional" />
> or
>
> <xsd:attribute name="p" type="xsd:string" use="optional" />
> <xsd:attribute name="c" type="xsd:string" use="required" />
>
> how can I specify this in the xsd?
>
> Thx
>



 
Reply With Quote
 
George Bina
Guest
Posts: n/a
 
      02-09-2006
If you really want to keep the document structure and none of the above
alternatives solve your problem then you have also the following
possibilities:

A. Make both attributes optional and use Schematron embedded rules in
XML Schema to check that you have only one of them
B. Use Relax NG instead of XML Schema

Here it is a sample for case A. XML Schema with embedded Schematron
rules:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlnss="http://www.w3.org/2001/XMLSchema"
xmlns:sch="http://www.ascc.net/xml/schematron">
<xs:element name="test">
<xs:annotation>
<xs:appinfo>
<schattern name="Check that we have only one attribute.">
<sch:rule context="test">
<sch:assert test="count(@p|@c)=1">Ony one of the attributes
p and c can be
specified.</sch:assert>
</sch:rule>
</schattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:attribute name="p" type="xs:string" use="optional"/>
<xs:attribute name="c" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:schema>

on the document
<test c="1" p="2"/>

will give:
SystemID: C:\george\workspace\oXygen\samples\test.xml
Location: 1:0
Description: Ony one of the attributes p and c can be specified.
(count(@p|@c)=1)

And here it is a Relax NG sample schema:

<?xml version="1.0" encoding="UTF-8"?>
<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element name="test">
<choice>
<attribute name="c">
<data type="string"/>
</attribute>
<attribute name="p">
<data type="string"/>
</attribute>
</choice>
</element>
</start>
</grammar>

And in compact syntax that is:

default namespace = ""

start =
element test {
attribute c { xsd:string }
| attribute p { xsd:string }
}


Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Required and optional elements Spaulding XML 1 08-15-2009 10:14 AM
W3C Schema for required and optional elements Philipp XML 2 04-11-2008 09:17 AM
Vista reactivation required after Microsoft Update optional update Colin Barnhorst Windows 64bit 32 05-26-2007 04:36 PM
XSD question: Allowing one required element and many optional elements. MENTAT XML 8 04-04-2005 04:16 AM
Required & Optional software and utilities for creating html, Javascript etc Sean HTML 8 02-15-2004 11:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57