Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在XML模式复杂类型中,限制如何影响允许的属性集?_Xml_Xsd - Fatal编程技术网

在XML模式复杂类型中,限制如何影响允许的属性集?

在XML模式复杂类型中,限制如何影响允许的属性集?,xml,xsd,Xml,Xsd,因此,通过XML模式限制,您可以派生一个新的复杂类型,它具有父类型元素的子集。例如,如果我有此基本类型: <complexType name="baseType"> <complexContent> <restriction base="anyType"> <attribute name="number" type="decimal"/> <attribute name="quantity" type="p

因此,通过XML模式限制,您可以派生一个新的复杂类型,它具有父类型元素的子集。例如,如果我有此基本类型:

<complexType name="baseType">
  <complexContent>
    <restriction base="anyType">
      <attribute name="number" type="decimal"/>
      <attribute name="quantity" type="positiveInteger"/>
      <sequence>
        <element name="first" minOccurs="0" maxOccurs="1" type="string"/>
        <element name="second" minOccurs="0" maxOccurs="1" type="string"/>
      </sequence>
    </restriction>
  </complexContent>
</complexType>

此处允许哪些属性?

在限制中,您必须包括在基类型中声明的集合中希望在新类型中允许的所有元素和属性。限制中的元素声明将覆盖以前的声明

您的模式几乎是正确的。在
complexType
元素中,
序列
必须出现在
属性
声明之前。如果您有此架构:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        elementFormDefault="qualified" targetNamespace="tns" xmlns:tns="tns">

    <complexType name="baseType">
        <complexContent>
            <restriction base="anyType">
                <sequence>
                    <element name="first" minOccurs="0" maxOccurs="1" type="string"/>
                    <element name="second" minOccurs="0" maxOccurs="1" type="string"/>
                </sequence>
                <attribute name="number" type="decimal"/>
                <attribute name="quantity" type="positiveInteger"/>
            </restriction>
        </complexContent>
    </complexType>

    <complexType name="newType">
        <complexContent>
            <restriction base="tns:baseType"> <!-- restriction of baseType -->
                <sequence>
                    <element name="first" type="string"/>
                </sequence>
                <!-- attribute declarations are not necessary, since they allow the same types -->
            </restriction>
        </complexContent>
    </complexType>

    <element name="root">
        <complexType>
            <choice maxOccurs="unbounded">
                <element name="base" type="tns:baseType" minOccurs="0"/>
                <element name="new"  type="tns:newType" minOccurs="0"/>
            </choice>
        </complexType>
    </element>

</schema>

它将验证前两个
元素,但由于注释中解释的原因,这两个
元素的验证将失败:

<base number="123.5" quantity="3"> <!-- will validate -->
    <first></first>
    <second></second>
</base>

<base> <!-- will validate: min occurs for <first> and <second> is zero OK --> 
</base>

<new number="123.5"> <!-- will fail validation: min occurs of <first> is one -->
</new>

<new number="123.5" quantity="3"> <!-- will validate: quantity attribute is inherited -->
    <first></first>
    <second></second> <!-- will fail validation: no <second> element allowed -->
</new>


派生类型中的元素和属性声明必须相等或更严格:派生类型中不能有:

<element name="first" minOccurs="0" maxOccurs="unbounded" type="string"/>


因为在基类型中没有显式的
maxOccurs
,所以该值为1,
unbounded
不会成为限制。此外,如果您将属性声明为
decimal
,例如,在基类型中,您可以再次将其声明为
integer
,以对其进行更多限制,但您不能将原来是
integer
的属性声明为
decimal
,因为这不是一个限制。

属性是否仍然继承?其中写道:“但是,属性声明不需要在派生类型定义中重复;在本例中,RestrictedPurchaseOrderType将从PurchaseOrderType继承orderDate属性声明。”我理解这意味着如果没有列出属性,属性仍然会被继承?还是不是这样?你是对的。答案中有一个输入错误,因为
newType
实际上不是从
baseType
派生的,而是从
anyType
派生的。我会解决它。在这种情况下,属性声明是不必要的,除非您希望限制属性类型(例如:make
number
需要一个
整数
而不是任何
十进制
),请注意,我需要为所有引用的类型添加前缀。这是必要的,因为默认名称空间是XML模式名称空间。遵循为XML模式名称空间使用前缀的约定(通常是
xs
xsd
)并使用目标名称空间作为默认名称空间通常是更好的做法。
<base number="123.5" quantity="3"> <!-- will validate -->
    <first></first>
    <second></second>
</base>

<base> <!-- will validate: min occurs for <first> and <second> is zero OK --> 
</base>

<new number="123.5"> <!-- will fail validation: min occurs of <first> is one -->
</new>

<new number="123.5" quantity="3"> <!-- will validate: quantity attribute is inherited -->
    <first></first>
    <second></second> <!-- will fail validation: no <second> element allowed -->
</new>
<element name="first" minOccurs="0" maxOccurs="unbounded" type="string"/>