使用.NET验证XML身份约束的问题?

时间:2016-05-24 22:22:33

标签: .net xml validation

我有以下用于测试验证行为的Schema(使用XMLDocument以及与模式匹配的简单xml文件):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="testSchema.xsd" targetNamespace="testSchema.xsd">

  <xs:element name="multiEle" type="typeOne">
    <xs:key name="keyOne">
      <xs:selector xpath="./eleOne" />
      <xs:field xpath="@boolTwo" />
    </xs:key>
  </xs:element>

  <xs:complexType name ="typeOne">
    <xs:sequence>
      <xs:element name="eleOne" type="xs:string"/>

      <xs:sequence>

        <xs:element name="eleTwo" type="xs:decimal" />

        <xs:element name="eleThree">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:boolean">
                <xs:attribute name="boolTwo" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>      

      </xs:sequence>      

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

            <xs:element name="eleOne">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="eleFive" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>

            <xs:element name="eleSix" />    

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

      <xs:choice>
        <xs:element name="eleOne" type="xs:string"/>
        <xs:element name="eleTwo" type="xs:decimal" />
      </xs:choice>

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

我希望这个模式甚至不能编译,更不用说验证了,因为身份约束指向不存在这种属性的元素上的属性。此外,XML文档中没有指定任何属性,即:

    <?xml version="1.0" encoding="utf-8"?>
<multiEle xmlns="testSchema.xsd">
  <eleOne>eleOne1</eleOne>
  <eleTwo>2</eleTwo>
  <eleThree boolTwo="anySimpleType">true</eleThree>
  <eleFour>
    <eleOne>
      <eleFive></eleFive>
    </eleOne>
    <eleSix>anyType</eleSix>
  </eleFour>
  <eleOne>abc</eleOne>
</multiEle>

但是,我发现编译的模式和XMLdocument在不抛出任何异常的情况下进行了验证(为处理程序指定了null)。有什么东西我不见了吗?

1 个答案:

答案 0 :(得分:2)

您的I write an example as per your requriment 1-we have a json array { "status": 200, "status_message": "Success", "response": [ { "quizNumber" : "1", "image" : "", "question" : "Which car manufacturer was the first to win 100 F1 races?", "option1" : "Ferrari", "option2" : "Nissan", "option3" : "Ford", "option4" : "", "answer" : "Ferrari." }, { "quizNumber" : "2", "image" : "", "question" : "In the professional era which woman has won the most titles at Wimbledon [singles, doubles and mixed] ?", "option1" : "Venus", "option2" : "Hingis", "option3" : "Martina Navratilova", "option4" : "Waynetta", "answer" : "Martina Navratilova" }, { "quizNumber" : "3", "image" : "", "question" : "How many times have Liverpool been relegated from the top flight of English football?", "option1" : "Four", "option2" : "Three", "option3" : "Two", "option4" : "Five", "answer" : "Three" }]} 2-Make a pojo class class Mypojo { String op1,op2,op3,op4,ans; Mypojo(String s1,String s2,String s3,String s4,String ss){ op1 = s1; ....... ans = ss; } //add getter / setter method here } 3- Now in activity take an array list int i = 0; ArrayList<Mypojo> mypojo = new AarrayList(); where you parse json make pojo class object like Mypojo pojo = new Mypojo(parameters) and pass all parameters then mypojo.add(pojo); In this way all json data will be added in pojo type array list. 4-Now in Next button code will be int arraysize = mypojo.size(); if(i<arraysize){ i++; get all values from pojo arraylist by using i as a index position like String op1 = mypojo.get(i).getOp1(); and change your UI. } This is for example if you will do all steps correctly your problem will be solved. 未选择任何元素:它正在查找无命名空间中的xs:selector元素,但您的elemOne元素位于命名空间elemOne中。空集不能包含两个具有相同键值的元素,因此您的实例有效。

如果使用Saxon 9.6进行验证,它会在模式编译时发出警告:

testSchema.xsd

(Saxon 9.7不会发出此警告 - 我正在调查原因。)

使用始终选择空序列的XPath表达式时,XSD规范不会出错。这是因为指定它非常复杂 - 它需要XQuery Formal Semantics规范的所有机制来定义静态类型规则。 Saxon通过其模式感知的XPath处理来实现它,它进行了这种分析(根据自己的规则,而不是任何W3C规则)。