×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

XSD with "required" attribute option related query

XSD with "required" attribute option related query

XSD with "required" attribute option related query

(OP)
Hello All,

I am new to XSD and XML and need to explore if there is option to make sure that any one attribute in a set of attributes within same element is required.

Example:

<xs:element name="where" maxOccurs="1" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="condition" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="alias1" use="required"/>
<xs:attribute type="xs:string" name="col1" use="required"/>
<xs:attribute type="xs:string" name="operator" use="required"/>
<xs:attribute type="xs:string" name="string" use="optional"/>
<xs:attribute type="xs:string" name="number" use="optional"/>
<xs:attribute type="xs:string" name="date" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

Here I want to ensure that minimum one attribute in a set of 3 attributes (mentioned as optional in above) are required. All can not be optional , however any one (can be more than one also) is required.

Thanks,
Rajneesh

RE: XSD with "required" attribute option related query

Rajneesh,

With what version of XML Schema are you working?

RE: XSD with "required" attribute option related query

(OP)
Hi atlopes,

Thanks for your reply.

version="1.0"

This XSD is generated through online tool , by providing XML as input file. XML is also 1.0 version.

Thanks,
Rajneesh

RE: XSD with "required" attribute option related query

Ok, Rajneesh, I don't think you'll be able to enforce that rule with version 1.0 of the XML Schema.

But even if the online tool you're using does not support version 1.1, and as long as your validator/parser does, you can post-process the schema to mandate the presence of one of the attributes.

The highlighted areas of the schema require your attention. Basically, you say version 1.1 is required and then include an assertion that returns true when one of the optional attributes is included in the document.

CODE --> XML

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
  targetNamespace="info:xml-forum" xmlns="info:xml-forum"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
  
  <xs:element name="where">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="condition" maxOccurs="unbounded" minOccurs="1">
        <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute type="xs:string" name="alias1" use="required"/>
            <xs:attribute type="xs:string" name="col1" use="required"/>
            <xs:attribute type="xs:string" name="operator" use="required"/>
            <xs:attribute type="xs:string" name="string" use="optional"/>
            <xs:attribute type="xs:string" name="number" use="optional"/>
            <xs:attribute type="xs:string" name="date" use="optional"/>
            <xs:assert test="@string or @number or @date"/>
          </xs:extension>
        </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema> 

An invalid XML document:

CODE --> XML

<?xml version="1.0" encoding="UTF-8"?>
<f:where xmlns:f="info:xml-forum" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="info:xml-forum forum.xsd">
  <f:condition alias1="alias1" col1="col1" operator="op"></f:condition>
</f:where> 

A valid XML document (at least one of the attributes is present):

CODE --> XML

<?xml version="1.0" encoding="UTF-8"?>
<f:where xmlns:f="info:xml-forum" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="info:xml-forum forum.xsd">
  <f:condition alias1="alias1" col1="col1" operator="op" string="---"></f:condition>
</f:where> 

RE: XSD with "required" attribute option related query

(OP)
Thank you

RE: XSD with "required" attribute option related query

(OP)
Hi Atlopes,
I am working inside the database, it won't be possible.
Oracle XML DB only supports XML Schema 1.0.

Thanks,
Rajneesh

RE: XSD with "required" attribute option related query

Quote (Rajneesh)

Oracle XML DB only supports XML Schema 1.0

Then you can't use assertions to extend the validation of your schemas.

I'm not familiar with your environment, but can't you submit your XML documents to an XSLT? A transformation stylesheet can be used as a validator, also.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close