开发者

xml validation: validating a URI type

I'm using python's lxml to validate xmls against a schema开发者_如何学编程. I have a schema with an element:

<xs:element name="link-url" type="xs:anyURL"/>

and I test, for example, this (part of an) xml:

<a link-url="server/path"/>

I would like this test to FAIL because the link-url doesn't start with http://. I tried switching anyURI to anyURL but this results in an exception - it's not a valid tag.

Is this possible with lxml? is it possible at all with schema validation?


(I'm pretty sure xs:anyURL is not valid. The XML Schema standard calls it anyURI. And since link-url is an attribute, shouldn't you be using xs:attribute instead of xs:element?)

You could restrict the URIs by creating a new simpleType based on it, and put a restriction on the pattern. For example,

#!/usr/bin/env python2.6

from lxml import etree
from StringIO import StringIO

schema_doc = etree.parse(StringIO('''
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="httpURL">
        <xs:restriction base="xs:anyURI">
            <xs:pattern value='https?://.+'/>
            <!-- accepts only http:// or https:// URIs. -->
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="a">
        <xs:complexType>
            <xs:attribute name="link-url" type="httpURL"/>
        </xs:complexType>
    </xs:element>
    </xs:schema>
''')) #/
schema = etree.XMLSchema(schema_doc)

schema.assertValid(etree.parse(StringIO('<a link-url="http://sd" />')))
assert not schema(etree.parse(StringIO('<a link-url="server/path" />')))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜