Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSD with "required" attribute option related query 1

Status
Not open for further replies.

rajneesh4u

Systems Engineer
Feb 21, 2020
5
IN
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
 
Rajneesh,

With what version of XML Schema are you working?
 
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
 
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.

XML:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] elementFormDefault="qualified"
  targetNamespace="info:xml-forum" xmlns="info:xml-forum"
  [highlight #FCE94F]xmlns:vc="[URL unfurl="true"]http://www.w3.org/2007/XMLSchema-versioning"[/URL] vc:minVersion="1.1"[/highlight]>
  
  <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"/>
            [highlight #FCE94F]<xs:assert test="@string or @number or @date"/>[/highlight]
          </xs:extension>
        </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

An invalid XML document:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<f:where xmlns:f="info:xml-forum" xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL]
  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):
XML:
<?xml version="1.0" encoding="UTF-8"?>
<f:where xmlns:f="info:xml-forum" xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL]
  xsi:schemaLocation="info:xml-forum forum.xsd">
  <f:condition alias1="alias1" col1="col1" operator="op" string="---"></f:condition>
</f:where>
 
Hi Atlopes,
I am working inside the database, it won't be possible.
Oracle XML DB only supports XML Schema 1.0.

Thanks,
Rajneesh

 
Rajneesh said:
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top