views:

24

answers:

1

I have an xml file with an attribute that looks something like this:

<Element attribute="1234,2345,3413,6532" />

I need a way to validate that the attribute value is a comma separated list of integers within a certain range. Anyone know how to do this using XSD?

Thanks!

A: 

This should restrict the attribute's values to a comma-separated list of integers:

<xsd:element name="Element">
    <xsd:complexType>
        <xsd:attribute name="attribute">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:pattern value="\d+(,\d+)*" />
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:attribute>
    </xsd:complexType>
</xsd:element>

If the range you mention is simple enough you might be able to express that in the RE, for example [1-9]\d{3} for a 4-digit integer.

Richard Fearn
Thanks, this gets me partly there. What if the range was something like 0-183784. This would get tricky to validate using a regular expression. Is there any other way to do it? I already have a simple type to validate that an integer is in the right range, is there any way to combine that with your example?