Reputation: 23084
This is (part of) the XML that I want to define in my schema. I have already added a unique constraint for the name
attribute of the <add />
element.
<parameters>
<add name="one" value="1" />
<add name="two" value="2" />
</parameters>
But I don't know how to prevent <add name="" value="" />
. I have tried the following schema, but it's not honored:
<xs:attribute name="name" use="required" type="config:NonEmptyString" />
<xs:simpleType name="NonEmptyString">
<xs:restriction base="xs:string">
<xs:minLength value="1" />
</xs:restriction>
</xs:simpleType>
config
is the target namespace for the schema.
Edit: I'm using Visual Studio to validate XML as I write it in the XML editor.
Upvotes: 0
Views: 625
Reputation: 11953
Visual Studio 2010 does the validation correctly - it produces the warning:
Warning 4 The 'name' attribute is invalid - The value '' is invalid according to its datatype 'config:NonEmptyString' - The actual length is less than the MinLength value. XMLFile1.xml 3 14 Miscellaneous Files
but for some reason (bug?) it does not underline the error position in the XML editor.
Upvotes: 1
Reputation: 21638
I did a quick test; created an XSD out of the XML and added your constraint:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="parameters">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="add">
<xsd:complexType>
<xsd:attribute name="name" type="NonEmptyString" use="required"/>
<xsd:attribute name="value" type="xsd:unsignedByte" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="NonEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
I've validated this XML:
<parameters>
<add name="" value="1" />
<add name="two" value="2" />
</parameters>
And I've got this error:
Error occurred while loading [], line 2 position 8
The 'name' attribute is invalid - The value '' is invalid according to its datatype 'NonEmptyString' - The actual length is less than the MinLength value.
I would say that the problem is rather with your XSD processor, as opposed to the XSD itself. Maybe you could update the post with the processor you're using?
Upvotes: 2