English 中文(简体)
在我XML Schema 中, 我如何需要特定的自定义数据类型?
原标题:How do I require specific instances of a custom datatype in my XML schema?
  • 时间:2012-05-24 23:06:25
  •  标签:
  • xsd

假设我有一个定制的数据类型 在我的计划:

  <xs:element name="Fruit">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Name"/>
        <xs:element ref="Supplier"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Name" type="xs:NCName"/>
  <xs:element name="Supplier" type="xs:string"/>

在别的地方,我想要求 具体的水果实例, 所以我有类似以下的 XML 文件:

<fruits>
  <common>
    <!-- the common section should always include Fruits with these Names -->
    <Fruit>
      <Name>apple</Name>
      <Supplier>Shady Orchard</Supplier>
    </Fruit>
    <Fruit>
      <Name>orange</Name>
      <Supplier>Florida Orange Co.</Supplier>
    </Fruit>
    <Fruit>
      <Name>banana</Name>
      <Supplier>Bananaland</Supplier>
    </Fruit>
  </common>
  <exotic>
    <Fruit>
      <Name>kiwano</Name>
      <Supplier>Fancy Fruits</Supplier>
    </Fruit>
    <Fruit>
      <Name>rambutan</Name>
      <Supplier>Indonesia Fruit Co.</Supplier>
    </Fruit>
    <!-- the list goes on... -->
  </exotic>
</fruits>

我知道我可以这样定义我档案中的异国情调部分:

  <xs:element name="exotic">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="Fruit"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

但我如何定义 common 部分, 即总是需要 < code> Fruit 和名称 pple oangle banana 的 < code < fruit ?

最佳回答

工作不是那么困难。 我非常肯定有一个更优雅的解决方案, 但这样会奏效。 唯一的事情是, 我们并不真的需要具体的例子, 而是创建特殊类型来与这些值匹配。 我非常肯定有办法可以不设置类型限制, 固定值, 但我无法记住属性, 我不知道它是否与三个值的替代值相配合 。

这里 xsd 是一个 xsd

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

<xs:element name="fruits">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="fr:common" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="fr:exotic" minOccurs="1" maxOccurs="1" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="fruit">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="fr:name" />
            <xs:element ref="fr:supplier" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="name" type="xs:NCName" />
<xs:element name="supplier" type="xs:string" />

<xs:element name="exotic">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="fr:fruit" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="common">
    <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="fruit">
                <xs:complexType>
                    <xs:sequence minOccurs="1" maxOccurs="1">
                        <xs:element name="name" type="fr:CommonFruitType" />
                        <xs:element ref="fr:supplier" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:simpleType name="CommonFruitType">
    <xs:restriction base="xs:NCName">
        <xs:enumeration value="apple" />
        <xs:enumeration value="orange" />
        <xs:enumeration value="banana" />
    </xs:restriction>
</xs:simpleType>

在此举一个无法验证的示例 。

<?xml version="1.0" encoding="UTF-8"?>
<fruits xmlns="http://www.example.org/FruitSchema" xmlns:fr="http://www.example.org/FruitSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/FruitSchema FruitSchema.xsd">
    <common>
        <!-- the common section should always include fruits with these names -->
        <fruit>
            <name>apple</name>
            <supplier>Shady Orchard</supplier>
        </fruit>
        <fruit>
            <name>apple</name>
            <supplier>Shady Orchard</supplier>
        </fruit>
        <fruit>
            <name>orange</name>
            <supplier>Florida Orange Co.</supplier>
        </fruit>
        <fruit>
            <name>banana</name>
            <supplier>Bananaland</supplier>
        </fruit>
        <fruit>
            <name>kiwano</name>
            <supplier>Fancy fruits</supplier>
        </fruit>
    </common>
    <exotic>
        <fruit>
            <name>kiwano</name>
            <supplier>Fancy fruits</supplier>
        </fruit>
        <fruit>
            <name>rambutan</name>
            <supplier>Indonesia fruit Co.</supplier>
        </fruit>
        <!-- the list goes on... -->
    </exotic>
</fruits>

它只验证common 中的水果,即苹果、橙子或香蕉,不论供应商是什么。

问题回答

暂无回答




相关问题
How to validate DataTable with XML Schema?

Does anyone know how to validate a DataTable against a particular DataTable XSD (Schema)? Which would be the best performance-optimized way of doing this? For example: <?xml version="1.0" ...

Deprecated XML validation code problem C#

I m trying to figure out how to correct his deprecated xml schema validation code. public static bool ValidateXml(string xmlFilename, string schemaFilename) { ⁞ //Forward stream reading ...

Getting an object representation of an .XSD file in Java

What is the easiest way to obtain a statically typed representation of an XML schema (.XSD) in Java? More specifically I want to be able to programatically traverse all defined simpleType:s and ...

xmlserializer validation

I m using XmlSerializer to deserialize Xml achives. But I found the class xsd.exe generated only offers capability to read the xml, but no validation. For example, if one node is missing in a document,...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签