我有笔记本电脑、PC、打印机等元素。它们都应该具有相同的属性,例如价格和型号。是否可以只声明一次属性列表并将其包含在 3 个元素中?
问问题
406 次
1 回答
2
一般来说,人们不限定属性。要在没有命名空间的情况下重用它们,特别是在列表中,您可以使用attributeGroup或作为公共基本类型的一部分。
这是一个 XSD,显示了您拥有的不同可能性:
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:attributeGroup name="Priced">
<xsd:annotation>
<xsd:documentation>
Allows reuse of common attributes in components
that are not of the same kind.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="price" type="xsd:decimal"/>
<xsd:attribute name="model" type="xsd:string" use="required"/>
</xsd:attributeGroup>
<xsd:element name="Printer">
<xsd:annotation>
<xsd:documentation>
How to reference an attribute group.
Here Printer is considered in a different
type hierarchy, so it shares the Priced attributes
only.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence/>
<xsd:attributeGroup ref="Priced"/>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="Base">
<xsd:annotation>
<xsd:documentation>
If Printer, PC, and Laptop are ultimately a
"Base", and "Priced" is applicable to the "Base"
type only, then you don't need an attribute
group; reuse through base type.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence/>
<xsd:attribute name="price" type="xsd:decimal"/>
<xsd:attribute name="model" type="xsd:string" use="required"/>
</xsd:complexType>
<xsd:complexType name="AnotherBase">
<xsd:annotation>
<xsd:documentation>
Another base type; here reuse the attribute group.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence/>
<xsd:attributeGroup ref="Priced"/>
</xsd:complexType>
<xsd:element name="Laptop">
<xsd:annotation>
<xsd:documentation>
How to reuse a base type.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="Base">
<xsd:sequence/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="PC">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="Base">
<xsd:sequence/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
于 2013-04-27T12:47:20.597 回答