0

I have this XSD and I am trying to create the XML file using NATIVEXML (Delphi).

<xs:element maxOccurs="unbounded" name="Program">
 <xs:complexType>
  <xs:attribute name="Provider" use="required">
   <xs:simpleType>
    <xs:restriction base="xs:string">
     <xs:minLength value="2" />
     <xs:maxLength value="6" />
    </xs:restriction>
   </xs:simpleType>
  </xs:attribute>
  <xs:more attributes\>
 </xs:complexType>
</xs:element>

I created the following code:

node := Adoc.Root.NodeNew('Student');
node.WriteAttributeInteger('Provider',var_with_data);

how can I create the restrictions?

  • Put the following `if (Length(var_with_data) < 2) or (Length(var_with_data) > 6) then raise Exception.Create('Hey, you cannot have that length for the Provider attribute!');` before you write the attribute. – TLama May 29 '14 at 21:08
  • @TLama when I generate the XLM and validate with the XSD, the result is: there is no min/maxLenght, so, its not valid. – user3688905 May 29 '14 at 21:12
  • 2
    That attribute is required and have those length bounds, so there's not much you can choose from. If the `var_with_data` value will be shorter than 2 chars or longer than 6, you must not save the XML since it would be invalid. The problem with the code you've shown seems to be in the element into which you store the attribute. The XSD you've shown is for `Program` element, not for `Student`, but without knowing more details (like your code, XML that you expect and XML that you get) it's just a wild guess. But you've asked how to *create restrictions* and one way is to raise an exception... – TLama May 29 '14 at 21:28
  • @David, about the [`suggested duplicate`](http://stackoverflow.com/q/17361799/960757) close; this question doesn't ask for XSD validator. It asks how to make a restriction for the value that is passed to the `WriteAttributeInteger` method. Yes, you can build the XML and run a validation on it, but I doubt you will be able to sanitize the input. Manual restriction implementing requires maintaining, but whenever the XSD changes, you should review the code that builds the XML since its structure might have totally change. Voted to reopen... – TLama May 30 '14 at 07:31
  • @TLama OK. I guess I don't understand what the question is asking then. – David Heffernan May 30 '14 at 07:36
  • @David, I think it just asks how to keep `var_with_data` within the XSD specified bounds. – TLama May 30 '14 at 07:37

0 Answers0