Restricting an Elements value

This should cover W3C XML Schema, Relax NG and DTD related problems.
Posts: 2
Joined: Fri Mar 30, 2012 11:15 am

Restricting an Elements value

Post by glawson » Fri Mar 30, 2012 1:39 pm

I am trying to restriction a value that is allowed to be captured for a particular feature. My current schema has the following

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="" xmlns:gml="">

<!-- The following simplyType feature has been added to constrain the allowed value range supplied
VARCHAR2 (38 Byte) i.e. Programming: GUID (including brackets using parentheses)) -->
<xs:simpleType name="ReferenceGUID">
<xs:restriction base="xs:token">
<xs:pattern value="\{[A-Z0-9]{8}(\-[A-Z0-9]{4}){3}\-[A-Z0-9]{12}\}"/>


Validating against the following examples:


It will successfully 'fail' the second and third example, but pass the fourth! What i trying to validate is that the value range captured must be alphanumeric.

Posts: 740
Joined: Mon Dec 05, 2011 6:04 pm

Re: Restricting an Elements value

Post by Costin » Tue Apr 03, 2012 2:43 pm


All the values in the examples you specified are alphanumeric - including the fourth one.
The validation works OK and the behaviour is completely normal.

From the three examples you specified (2,3 and 4), only the fourth one is in accordance with the accepted pattern you set in the restriction.
That is because, regarding the character numbers, your pattern value should be like : 8 - 4 - 4 - 4 - 12. The 2nd and the 3rd examples do not follow the characters number restriction in the pattern (as they are: 8-5-4-12 and 8-2-4-4-12).

Do you want the validation to also "fail" for the 4th example ?

Costin Sandoi
oXygen XML Editor and Author Support

Posts: 2
Joined: Fri Mar 30, 2012 11:15 am

Re: Restricting an Elements value

Post by glawson » Tue Apr 03, 2012 3:04 pm

Hello Colin

The only example i want to pass would be the first one i.e. with the format '{8-4-4-4-12}' (e.g. {BCB385F9-2A19-5ADC-E040-A40A272C6A8A}). Whilst i am aware that technically the 4th one does follow that rule, and is again technically alphanumeric. What i am trying to trap is when only zero's <0> are used in all none '{' or '-' positions, instead of a mixture of letters (ideally in the range 'A-F') and numbers '0-9'.


Posts: 2740
Joined: Tue May 17, 2005 4:01 pm

Re: Restricting an Elements value

Post by adrian » Tue Apr 03, 2012 4:51 pm


To easily exclude that particular value(all zeros), you would need a regular expression with negative lookahead. e.g.

Code: Select all

Unfortunately XML Schema 1.0 does not support negative lookahead in the regular expression patterns, which makes an expression that does this very tedious to write. Actually, considering the length of your string, I consider the alternative impractical.

There are a few articles on StackOverflow that exemplify how this can be done, but I don't see these as a proper solution in your case: ... xml-schema ... ema-flavor

Are you limited to XML Schema, or are you allowed to use other types of schema(e.g. XML Schema + Schematron or embedded Schematron) to accomplish this?

Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger

Post Reply