如何编写xsd以满足以下XML要求 [英] how to write xsd for following XML requirement

查看:63
本文介绍了如何编写xsd以满足以下XML要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="UTF-8"?>
 <Metadata>
  <Field Name="Claim Number">100</Field>
  <Field Name="Audit Year">2010</Field>
  <Field Name="Auditor Name">ABC PQR</Field>
</Metadata>







in the above xml if I delete <Field Name="Audit Year">2010</Field> this line then it must say "Audit Year" is not present in the given XML.

below is the present XSD but it's not a proper way.







<<pre lang="xml">?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="temp" >
<xs:restriction base="xs:string">
  <xs:enumeration value="Audit Year" />
</xs:restriction>
<xs:element name="Metadata">
<xs:complexType>
  <xs:sequence>
    <xs:element maxOccurs="unbounded" name="Field">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
           <xs:attribute name="Name" type="temp" use="required"/>
           <!--<xs:attribute name="Name" type="xs:string" use="optional" />-->
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType></xs:element></xs:schema></pre>

推荐答案

在XSD中,您可以使某些元素成为可选元素或必需元素。这可以通过使用属性 minOccurs 多样性来控制,可以理解为出现次数的约束。 c $ c>和 maxOccurs 。这里的用法在一个简单的示例中解释: http:// stackoverflow.com/questions/9243772/how-to-make-an-element-in-xml-schema-optional [ ^ ]。



另请参阅: http://en.wikipedia.org/wiki/Multiplicity_%28software%29 [< a href =http://en.wikipedia.org/wiki/Multiplicity_%28software%29target =_ blanktitle =New Window> ^ ]。

< br $> b $ b



另一件事是声明多态类型元数据(具有多态组成序列)。

参见,例如:

http://ojitha.blogspot.com/2012/01/learn-xml-schema-by-examp le-2.html [ ^ ]。



或者,不要使用序列。使用一组不同类型的固定组件,每个组件具有不同的多重性。



换句话说,您的架构不适合上层的要求。你需要重新设计它。



-SA
In XSD, you can make some element optional or mandatory. This can be controlled by explicitly defining the multiplicity, which can be understood as a constraint over the number of occurrences, using the attributes minOccurs and maxOccurs. Here the usage is explained on a simple sample: http://stackoverflow.com/questions/9243772/how-to-make-an-element-in-xml-schema-optional[^].

See also: http://en.wikipedia.org/wiki/Multiplicity_%28software%29[^].



Another thing to do is to declare polymorphic type Metadata (with polymorphic composition sequence).
See, for example:
http://ojitha.blogspot.com/2012/01/learn-xml-schema-by-example-2.html[^].

Alternatively, don't use sequence. Use a set of fixed components of different type with different multiplicity on each.

In other words, your schema is not adequate to the requirements on upper level. You need to redesign it.

—SA


我找到了2个链接:

http://stackoverflow.com/questions/8925706 / xml-schema-how-to-restrict-attribute-by-enumeration [ ^ ] - 你之前可能见过的这个,因为你使用了类似的方法



http://stackoverflow.com/questions/7690949/element-mandatory -attribute-declaration-in-xsd-schema [ ^ ] - 这个需要你注意。第二个答案包含5个例子,第二个答案可能就是你要找的东西。



注意:2 of 3 字段字段是整数基数,但一个是字符串基数。这可能是麻烦的原因!







我试试这个:

I found 2 links:
http://stackoverflow.com/questions/8925706/xml-schema-how-to-restrict-attribute-by-enumeration[^] - this one you have probably seen before, because you used similar approach
and
http://stackoverflow.com/questions/7690949/element-mandatory-attribute-declaration-in-xsd-schema[^] - this one needs your attention. Second answer contains 5 examples, where second one is probably that what you're looking for.

Note: 2 of 3 Field fields are integer base, but one is string base. It might be the reason of trouble!



I'd try this one:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >

  <xs:element name="Metadata">
    <xs:complexType>
      <xs:sequence minOccurs="1" maxOccurs ="unbounded">
        <xs:element name="Field">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:integer">
                <xs:attribute name="Name" use="required">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:enumeration value="Audit Year"/>
                      <xs:enumeration value="Claim Number"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:attribute>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>


</xs:schema>





注意:我没有足够的时间来测试它。



Note: I do not had enough time to test it.


[我认为没有任何办法在XSD 1.0中执行此操作。在XSD 1.1中,与大多数事情一样,它可以通过断言来完成。]


这篇关于如何编写xsd以满足以下XML要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆