我如何使用断言(启动XSD)

发布于 2025-01-21 00:35:06 字数 3136 浏览 0 评论 0原文

我想更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2025-01-28 00:35:06

这是不可能的。 XSD不支持条件约束。我建议的是摆脱类型属性并将其值用作元素的名称。

<?xml version="1.0"?>
<homeAppliances>
    
    <stereo>
        <description>My stereo</description>
        <brand>Sony</brand>
        <type>Hi-fi</type>
    </stereo>
    
    <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>
    </juicer>
</homeAppliances>

在XSD中,您可以通过XS:选择

<xs:element name="homeAppliances">
        <xs:complexType>
            <xs:sequence>
                <xs:choice maxOccurs="unbounded">
                    <xs:element name="stereo" type="stereoAppliance"/>
                    <xs:element name="juice" type="juiceAppliance"/>
                </xs:choice>                
            </xs:sequence>
        </xs:complexType>
    </xs:element>

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.

<?xml version="1.0"?>
<homeAppliances>
    
    <stereo>
        <description>My stereo</description>
        <brand>Sony</brand>
        <type>Hi-fi</type>
    </stereo>
    
    <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>
    </juicer>
</homeAppliances>

In the XSD you can describe it via xs:choice:

<xs:element name="homeAppliances">
        <xs:complexType>
            <xs:sequence>
                <xs:choice maxOccurs="unbounded">
                    <xs:element name="stereo" type="stereoAppliance"/>
                    <xs:element name="juice" type="juiceAppliance"/>
                </xs:choice>                
            </xs:sequence>
        </xs:complexType>
    </xs:element>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文