XmlSchemaValidationFlags

XmlSchemaValidationFlags

本文介绍了XS:关键,为什么验证传球的时候键值不是重要的参考会员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很感兴趣,在我的XSD定义键约束。 我的理解,使用 XS:键应限制使用的值的引用列表中的成员的值。

I am interested in defining a key constraint in my Xsd. It is my understanding that using xs:key should constrain the value used to a member of a referenced list of values.

假设我们使用的示例XSD,

Assuming we are using the sample Xsd,

<xs:schema
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="namespace1"
        xmlns:r="namespace1"
        elementFormDefault="qualified">

  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>

        <xs:element name="A" type="r:A" maxOccurs="unbounded">
          <xs:keyref name="dummy" refer="r:pNumKey">
            <xs:selector xpath="part"/>
            <xs:field xpath="@ref-number"/>
          </xs:keyref>
        </xs:element>

        <xs:element name="B" type="r:B"/>

      </xs:sequence>
    </xs:complexType>

    <xs:key name="pNumKey">
      <xs:selector xpath="r:B/r:part"/>
      <xs:field xpath="@key-number"/>
    </xs:key>

  </xs:element>

  <xs:complexType name="A">
    <xs:sequence>
      <xs:element name="part" maxOccurs="unbounded">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute name="ref-number" type="xs:integer"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="B">
    <xs:sequence>
      <xs:element name="part" maxOccurs="unbounded">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute name="key-number" type="xs:integer"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

示例XML,

sample Xml,

<root xmlns="namespace1">
  <A>
    <!--
      if the ref-number is not equal to one of the key-number,
      the validation will give error
    -->
    <part ref-number="1"/>
  </A>
  <A>
    <!--
      if the ref-number is not equal to one of the key-number,
      the validation will give error
    -->
    <part ref-number="2"/>
  </A>
  <B>
    <part key-number="1"/>
    <part key-number="2"/>
    <part key-number="3"/>
  </B>
</root>

和,比方说,一些简单的验证

and, say, some simple validation

[TestMethod]
public void Test_Schema()
{
    string schemaFileName = @"sampleSchema.xsd";
    string xmlFileName = @"sampleXml.xml";
    XmlReaderSettings settings = new XmlReaderSettings
    {
        ValidationType = ValidationType.Schema,
        ValidationFlags =
            XmlSchemaValidationFlags.ProcessInlineSchema |
            XmlSchemaValidationFlags.ProcessSchemaLocation |
            XmlSchemaValidationFlags.ReportValidationWarnings,
    };
    settings.Schemas.Add (schema);
    settings.ValidationEventHandler +=
        (o, e) => { throw new Exception("CRASH"); };

    XmlSchema schema =
        XmlSchema.Read (
        File.OpenText (schemaFileName),
        (o, e) => { throw new Exception ("BOOM"); });

    XmlReader reader = XmlReader.Create (xmlFileName, settings);
    while (reader.Read ()) { }
}

怎么回事,验证当我用坏值 A /一部分[@参考号码]

<root xmlns="namespace1">
  <A>
    <!-- doesn't go CRASH BOOM bang! why not? :( -->
    <part ref-number="5"/>
  </A>
  <B>
    <part key-number="1"/>
    <part key-number="2"/>
    <part key-number="3"/>
  </B>
</root>

时的任何一种或所有XSD,XML,或验证上述不正确的?还是我误解了 XS预期目的:键

推荐答案

像往常一样,经过一晚的休息,重新审视,发现不超过 2的错误少在这项工作中。

As usual, after a good night's rest and a fresh look, spotted no fewer than 2 errors in this exercise.

  1. 在第一个错误,验证身份的限制是一个明确的过程,通过 XmlSchemaValidationFlags.ProcessIdentityConstraints XmlReaderSettings.ValidationFlag 设置和
  2. 第二个错误, MSDN示例包含在架构中的错误,&LT; XS:选择的XPath =部分/&GT; 改为&LT; XS:选择的XPath =R:部分/&GT;
  1. First error, validating identity constraints is an explicit process, induced via XmlSchemaValidationFlags.ProcessIdentityConstraints set by XmlReaderSettings.ValidationFlag, and
  2. second error, msdn sample contains an error in schema, <xs:selector xpath="part"/> should read <xs:selector xpath="r:part"/>.

完整的工作示例如下,

<xs:schema
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="namespace1"
        xmlns:r="namespace1"
        elementFormDefault="qualified">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="A" type="r:A" maxOccurs="unbounded">
          <xs:keyref name="dummy" refer="r:pNumKey">
            <!-- without 'r:' below, key was not recognized, boo-urns msdn! -->
            <xs:selector xpath="r:part"/>
            <xs:field xpath="@ref-number"/>
          </xs:keyref>
        </xs:element>
        <xs:element name="B" type="r:B"/>
      </xs:sequence>
    </xs:complexType>
    <xs:key name="pNumKey">
      <xs:selector xpath="r:B/r:part"/>
      <xs:field xpath="@key-number"/>
    </xs:key>
  </xs:element>

  <xs:complexType name="A">
    <xs:sequence>
      <xs:element name="part" maxOccurs="unbounded">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute name="ref-number" type="xs:integer"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="B">
    <xs:sequence>
      <xs:element name="part" maxOccurs="unbounded">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute name="key-number" type="xs:integer"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

XML示例

Xml sample

<root xmlns="namespace1">
  <A>
    <!-- goes CRASH BOOM bang! failure for the win! -->
    <part ref-number="5"/>
  </A>
  <B>
    <part key-number="1"/>
    <part key-number="2"/>
    <part key-number="3"/>
  </B>
</root>

简单的验证

[TestMethod]
public void Test_Schema()
{
    string schemaFileName = @"sampleSchema.xsd";
    string xmlFileName = @"sampleXml.xml";

    XmlSchema schema =
        XmlSchema.Read(
        File.OpenText(schemaFileName),
        (o, e) => { throw new Exception("BOOM"); });

    XmlReaderSettings settings = new XmlReaderSettings
    {
        ValidationType = ValidationType.Schema,
        ValidationFlags =
            XmlSchemaValidationFlags.ProcessInlineSchema |
            XmlSchemaValidationFlags.ProcessSchemaLocation |
            XmlSchemaValidationFlags.ReportValidationWarnings |

            // d'oh! explicit flag for processing identity constraints!
            XmlSchemaValidationFlags.ProcessIdentityConstraints,
    };
    settings.Schemas.Add(schema);
    settings.ValidationEventHandler +=
        (o, e) => { throw new Exception("CRASH"); };

    XmlReader reader = XmlReader.Create(xmlFileName, settings);
    while (reader.Read()) { }
}

这篇关于XS:关键,为什么验证传球的时候键值不是重要的参考会员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:59