Restricting to a list

This should cover W3C XML Schema, Relax NG and DTD related problems.
senseiwa
Posts: 5
Joined: Wed May 04, 2011 4:03 pm

Restricting to a list

Post by senseiwa »

Hi all!

I am wondering if it is possible to make this work with a standard schema. I'd like to create, in the XML, a list of strings, and restrict some elements to these.

For example:

Code: Select all


<!-- these are the allowed names -->
<allowed>
<name>Bob</name>
<name>John</name>
<name>Alice</name>
</allowed>

<!-- this is ok since "Bob" is listed above -->
<who name="Bob" />
<!-- this is invalid since "Martha" isn't in the list! -->
<who name="Martha" />
I can also live without the attribute "name" restricted to the list, it can be whatever it works, a child element of string type, or anything! The important piece here is: can I restrict something to a part of the same XML, and yet being compliant with the XSD?
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Restricting to a list

Post by george »

You can use identity constraints to enforce that. Here it is a working sample

test.xsd

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="allowed"/>
<xs:element maxOccurs="unbounded" ref="who"/>
</xs:sequence>
</xs:complexType>
<xs:key name="allowed">
<xs:selector xpath="allowed/name"/>
<xs:field xpath="."/>
</xs:key>
<xs:keyref name="checkAllowed" refer="allowed">
<xs:selector xpath="who"/>
<xs:field xpath="@name"/>
</xs:keyref>
</xs:element>

<xs:element name="allowed">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element ref="name"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="name" type="xs:string"/>

<xs:element name="who">
<xs:complexType>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<allowed>
<name>Bob</name>
<name>John</name>
<name>Alice</name>
</allowed>
<who name="Bob" />
<who name="Martha" />
</root>
Best Regards,
George
George Cristian Bina
senseiwa
Posts: 5
Joined: Wed May 04, 2011 4:03 pm

Re: Restricting to a list

Post by senseiwa »

Thanks! I think I've been also able to reproduce your example xsd with the graphic interface.

Thank you!
juanpmtz
Posts: 3
Joined: Thu Feb 02, 2012 5:34 pm

Re: Restricting to a list

Post by juanpmtz »

is it possible to do this , but importing the list from another xml file ?
adrian
Posts: 2850
Joined: Tue May 17, 2005 4:01 pm

Re: Restricting to a list

Post by adrian »

Hello,

@juanpmtz: This topic was discussing a schema issue. Can you please provide more details about your problem?

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
juanpmtz
Posts: 3
Joined: Thu Feb 02, 2012 5:34 pm

Re: Restricting to a list

Post by juanpmtz »

I would like to restrict an attribute using a list but using the schema. I have done it using enumeration, like in the example below:

Code: Select all

<xs:attributeGroup name="functionAttributes">
<xs:attribute name="functionName" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="function1"/>
<xs:enumeration value="function2"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
but i don't want to add each enumeration manually but to import the list from another xml file that i already have.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Restricting to a list

Post by george »

You can use Schematron to implement such a check, that the values are found in a specified list of values in an external XML document.
In fact there is an entire framework created on this idea, using Schematron for implementation, called genericode, developed by Anthony Coates and used in OASIS UBL standard so you may look into that.

Best Regards,
George
George Cristian Bina
juanpmtz
Posts: 3
Joined: Thu Feb 02, 2012 5:34 pm

Re: Restricting to a list

Post by juanpmtz »

thanks a lot George,

I have done some work with schematron, but i wanted to use xsd so i could use the dropdownlist that oxygens generates.

Thanks anyway!
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Restricting to a list

Post by george »

Then you may consider generating the XSD schemas. For example you can add an annotation in the schema when you want to have those enumerations, that point for instance to the document that contain the values and then you can have a simple XSLT that run on the schema transforms those annotations in the corresponding enumerations.

Best Regards,
George
George Cristian Bina
Post Reply