开发者

xsd restriction of type javascript

开发者 https://www.devze.com 2023-03-11 00:44 出处:网络
I want to validate my xml against a schema, however for some nodes I want a smarter validation than what xsd schemas can offer.

I want to validate my xml against a schema, however for some nodes I want a smarter validation than what xsd schemas can offer.

For example, I have a Person.xml file:

<?xml version="1.0" encoding="utf-8"?>
<person xmlns="urn:person.xsd">
  <name>John Smith</name>
  <id>123455</id>
</person>

I would like to validate the ID node in a smart way: the first 5 digits (12345) is the ID of the person, and the sixth digit (5) is a check-sum digit which is calculated by the sum of the first five digits modulo 10 (1+2+3+4+5 modulo 10 = 5).

Ideally, I would like to extend the validation support of xsd schemas to allow code such as javascript to validate a node.

For example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="urn:person.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="person">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string" />
        <xs:element name="id">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:javascript>
                var b= parseInt(input);
                var b1= parseInt(b/100000)%10;
                var b2= parseInt(b/10000)%10;
                var b3= parseInt(b/1000)%10;
                var b4= parseInt(b/100)%10;
                var b5= parseInt(b/10)%10;
                var b6= b%10;

                if (b6 == (b1+b2+b3+b4+b5)%10 )
                  return "true";
                else
                  return "false";
       开发者_高级运维       </xs:javascript>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Now this of course is not supported. I could validate this node as a string and in my source code do further validation. But I want a general solution, and my code to except any xml, and Idon't want a hard coded solution.

This is probably a common problem, but I haven't been able to locate a solution.

So where is the best place to put the extra validation information?

Alternatively is there a way to extend MSXML or .Net's XML validator to support custom validation (My program is written mainly in .Net)?

Thank you


In standard XSD, the closest you can get is to use the the pattern element. This specifies a regular expression that the content must conform to.

However, regular expressions are only good for fairly simple validation, and won't be able to do what you desire.

One option is to add the script to the annotation element of the type. This is designed for "human- and machine-targeted annotations of schema components". You could then execute this script as your second phase, thereby creating the more general solution you desire.

Since you are using MSXML, you can retrieve XSD datatypes using the SOM.

0

精彩评论

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

关注公众号