我如何使用断言(启动XSD)
我想更新 homeAppliances.xsd,以便该元素具有依赖于 kind 属性值(以该值为条件)的内容(类型)。
我还想添加更多具有不同类型家用电器的电器。
请注意,每个元素都有一个 kind 属性,该属性指定该电器是立体声电器还是榨汁机电器。
我的目标是输出应该只显示那些具有“kind”属性的设备
这是我的 xml
<?xml version="1.0"?>
<homeAppliances>
<appliance kind="stereo">
<description>My stereo</description>
<brand>Sony</brand>
<type>Hi-fi</type>
</appliance>
<appliance kind="juicer">
<description>Theres just no substitute for a properly squeezed orange in the morning.
So delicate and refreshing. The finest hotels use mechanical juicers of this type for their
most discriminating guests. This is the largest selling juicer of its kind. Its a beautiful
little all-metal piece in baked enamel and polished chrome; it even won the Frankfurt Fair Award
for its design. Uses no electricity and produces no non-recyclable waste as do frozen juices.</description>
<warranty>lifetime warranty</warranty>
<name>OJ Home Juicer</name>
<image>images\mighty_oj.gif</image>
<cost>41.95</cost>
<retailer>http://www.thewhitewhale.com/oj.htm</retailer>
</appliance>
这是我的 xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="appliance">
<xs:sequence>
<xs:element name="description" type="xs:string" />
<xs:element name="warranty" type="xs:string" minOccurs="0" />
</xs:sequence>
<xs:attribute name="kind" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="stereoAppliance">
<xs:complexContent>
<xs:extension base="appliance">
<xs:sequence>
<xs:element name="brand" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="juiceAppliance">
<xs:complexContent>
<xs:extension base="appliance">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="image" type="xs:string" />
<xs:element name="weight" type="xs:unsignedByte" minOccurs="0" />
<xs:element name="cost" type="xs:decimal" />
<xs:element name="retailer" type="xs:anyURI" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="homeAppliances">
<xs:complexType>
<xs:sequence>
<xs:element name="appliance" type="appliance" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I want to update homeAppliances.xsd so that the element has a content (type) that depends on (is conditional to) the value of the kind attribute.
I also want to add more appliances with a different kind of home appliances
Notice that each element has a kind attribute, which specifies whether the appliance is a stereo appliance or a juicer appliance.
My goal is that the output should only show those appliances that have a "kind" attribute
this is my xml
<?xml version="1.0"?>
<homeAppliances>
<appliance kind="stereo">
<description>My stereo</description>
<brand>Sony</brand>
<type>Hi-fi</type>
</appliance>
<appliance kind="juicer">
<description>Theres just no substitute for a properly squeezed orange in the morning.
So delicate and refreshing. The finest hotels use mechanical juicers of this type for their
most discriminating guests. This is the largest selling juicer of its kind. Its a beautiful
little all-metal piece in baked enamel and polished chrome; it even won the Frankfurt Fair Award
for its design. Uses no electricity and produces no non-recyclable waste as do frozen juices.</description>
<warranty>lifetime warranty</warranty>
<name>OJ Home Juicer</name>
<image>images\mighty_oj.gif</image>
<cost>41.95</cost>
<retailer>http://www.thewhitewhale.com/oj.htm</retailer>
</appliance>
and this is my xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="appliance">
<xs:sequence>
<xs:element name="description" type="xs:string" />
<xs:element name="warranty" type="xs:string" minOccurs="0" />
</xs:sequence>
<xs:attribute name="kind" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="stereoAppliance">
<xs:complexContent>
<xs:extension base="appliance">
<xs:sequence>
<xs:element name="brand" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="juiceAppliance">
<xs:complexContent>
<xs:extension base="appliance">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="image" type="xs:string" />
<xs:element name="weight" type="xs:unsignedByte" minOccurs="0" />
<xs:element name="cost" type="xs:decimal" />
<xs:element name="retailer" type="xs:anyURI" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="homeAppliances">
<xs:complexType>
<xs:sequence>
<xs:element name="appliance" type="appliance" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不可能的。 XSD不支持条件约束。我建议的是摆脱类型属性并将其值用作元素的名称。
在XSD中,您可以通过
XS:选择
:It's impossible. XSD doesn't support conditional constraints. What I suggest is to get rid of the type attribute and use its value as a name of elements.
In the XSD you can describe it via
xs:choice
: