首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在许多属性中重用simpleType

在许多属性中重用simpleType
EN

Stack Overflow用户
提问于 2015-03-18 19:08:23
回答 1查看 182关注 0票数 0

我试图在其中一个属性中重用serverType,但它似乎根本没有验证或使用它。XSD文件没有错误。

我不确定是否需要在文件中的特定位置插入<xs:simpleType name="serverType">。我把它搬来搬去,但没有运气。下面是片段集:

代码语言:javascript
复制
<xs:element minOccurs="0" maxOccurs="unbounded" name="servers">
  <xs:complexType>
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="server">

    <xs:simpleType name="serverType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="IIS"/>
            <xs:enumeration value="Exchange"/>
            <xs:enumeration value="Sharepoint"/>
        </xs:restriction>
    </xs:simpleType>    
        <xs:complexType>
          <xs:attribute name="id">
              <xs:simpleType>
                 <xs:restriction base="xs:string">
                     <xs:pattern value="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})|(\{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\})"/>
                 </xs:restriction>
              </xs:simpleType>
          </xs:attribute>   
          <xs:attribute name="type" type="serverType" />    
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:sequence>

这是可行的,但它并没有重用代码:

代码语言:javascript
复制
<xs:attribute name="type">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="IIS"/>
            <xs:enumeration value="Sharepoint"/>
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

以下是XML文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup type="Contoso.ConfigurationSectionGroup, Contoso" name="atc">
      <section name="siteManager" type="Contoso.SiteManagerConfigurationSection, Contoso.Dashboard" />
    </sectionGroup>
  </configSections>
  <atc>
    <siteManager>
      <sites version="1.0.0">
        <site id="007F10AB-E6E2-4F47-989E-3F946B454CBE" name="SITE001 (Central)">
          <servers>
            <server id="76883A93-99EE-4571-B9FA-C4AE6D2A3ED1" name="SERVER001" type="IIS" fqdn="SERVER001.CONTOSO.COM" ipAddress="10.10.10.10" />
          </servers>
        </site>
      </sites>
    </siteManager>
  </atc>
</configuration>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-18 20:14:47

要重用xs:simpleType,您必须给它一个名称(检查),并使它成为全局的(参见下面):

代码语言:javascript
复制
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:simpleType name="serverType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="IIS"/>
      <xs:enumeration value="Exchange"/>
      <xs:enumeration value="Sharepoint"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="servers">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="server">
          <xs:complexType>
            <xs:attribute name="id">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:pattern value="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})|(\{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\})"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>   
            <xs:attribute name="type" type="serverType" />    
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

然后以下XML将是有效的:

代码语言:javascript
复制
<servers>
  <server id="76883A93-99EE-4571-B9FA-C4AE6D2A3ED1" type="IIS"/>
</servers>

以下XML将无效:

代码语言:javascript
复制
<servers>
  <server id="76883A93-99EE-4571-B9FA-C4AE6D2A3ED1" type="BAD"/>
</servers>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29130683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档