473,480 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Use of required and optional

LJ
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

Feb 8 '06 #1
3 1882


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/
Feb 8 '06 #2
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" <lj********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr 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

Feb 8 '06 #3
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 xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sch="http://www.ascc.net/xml/schematron">
<xs:element name="test">
<xs:annotation>
<xs:appinfo>
<sch:pattern 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>
</sch:pattern>
</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

Feb 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2941
by: Phil Powell | last post by:
I am having trouble retrieving URLs using curl for PHP whereby the URL requires a cookie to produce proper data. I wrote a wrapper class called Timer that will time the execution/download of a...
6
6758
by: yzzzzz | last post by:
Hi, In which cases is the <?xml version="1.0" encoding="UTF-8"?> processing instruction required at the beginning of an XML document, for the document to be valid? e.g. does it depend on the...
8
2329
by: MENTAT | last post by:
Hi Guys, Newbie question here. Part of my xml looks like this. <node> <description/> <config/> <log/> <transition/> <node>
2
2627
by: siliconpiNOSPAM | last post by:
Hi, I'm writing a program that should accept the following parameters: XMLfile (required) Logfile (required) /A (append flag, optional) /D 123 (delay value, optional, but # should follow /D...
3
1511
by: VB Programmer | last post by:
Can you make a required field validator optional? I know it sounds crazy, but.... Let's say I have a signup form. It consists of 2 sections. The top section is a "GOLD MEMBER" section. It...
2
3648
by: Alan Silver | last post by:
Hello, I'm getting an odd validation error from VWD. As I understand it, an opening ASP.NET for tag is supposed to look like... <form runat="server"> with an optional ID attribute. VWD...
3
5312
by: CindyRob | last post by:
I am using .NET framework 1.1 SP1, .NET framework SDK 1.1 SP1, with hotfix 82202, Visual studio .NET 2003 with hotfix 823639. I have generated a proxy class using wsdl.exe from a schema that has an...
12
2644
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
13
1796
by: robert10 | last post by:
Hi, I have recently added some code to my form in regards to resizing an image on Print. I have an image that does print correctly after its been resized, howver i'm needing to use it as a...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4472
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.