Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add pattern to anyURI data type in xsd

How can I guarantee that the url element starts with "http://"?

<xs:element name="url" type="xs:anyURI"/>
like image 935
grv Avatar asked Mar 11 '14 11:03

grv


People also ask

What is Uri in XSD?

xs:anyURI — URI (Uniform Resource Identifier).

What is minInclusive in XSD?

minExclusive. Specifies the lower bounds for numeric values (the value must be greater than this value) minInclusive. Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) minLength.

What is complex type in XSD?

A complex type is essentially a type definition for elements that may contain attributes and elements. An element can be declared with a type attribute that refers to a complexType element that defines the structure, content, and attributes of that element.

What is Xsd double?

Description. The value space of xsd:double is double (64 bits) floating-point numbers as defined by the IEEE (Institute of Electrical and Electronic Engineers). The lexical space uses a decimal format with optional scientific notation.


1 Answers

You can add a xs:restriction for a Regular Expression using xs:pattern:

<xs:element name="url">
    <xs:simpleType>
        <xs:restriction base="xs:anyURI">
            <xs:pattern value="http://.+" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>

This will match to anything that starts with http://. It will match:

http://www.stackoverflow.com
http://somethingsomethingsomething
http://123456789!!!!!
http://0

It will not match https URLs:

https://github.com

If you want to match https as well you can change the pattern to

https?://.+

which means that the s is allowed and is optional.

If you want to match only valid URLs then you need to improve it to check for characters, followed by a dot, more characters, a valid domain suffix, etc. If you search for URL validation via regex you will find several examples. You can also try this resource. And to experiment with Regex, Regex 101 is a great resource.

Pattern matching in XSD has some restrictions. Check this SO question/answer which discusses that.

like image 94
helderdarocha Avatar answered Sep 25 '22 01:09

helderdarocha