0

I am trying to reuse the serverType in one of the attributes but it seems to not validate or use it at all. The XSD file has no errors.

I am not sure if I need to insert the <xs:simpleType name="serverType"> in a specific place inside the file. I moved it around but no luck. Here is the snipset:

<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>

This works but it's not reusing the code:

<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>

Here is the XML file:

<?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>
Max
  • 1,289
  • 3
  • 26
  • 50
  • What is the XML you're trying to validate? Can you provide a [SSCCE](http://sscce.org/) which we can use to reproduce your issue? – Thomas Weller Mar 18 '15 at 19:21
  • I posted a XML sample - thanks – Max Mar 18 '15 at 19:30
  • Thanks. `` uses one of the values in the list. This is fine from XSD view. Do you mean that it can be changed to anything else and still validates? – Thomas Weller Mar 18 '15 at 19:33
  • if you change that to something else it does not validate against the enum list. – Max Mar 18 '15 at 19:50
  • 1
    Initially you said: "but it seems to not validate or use it at all". With your last statement I can't see that problem any more. The XSD does what it should do, doesn't it? – Thomas Weller Mar 18 '15 at 19:58
  • it worked after I moved that snipset right after this line: – Max Mar 18 '15 at 20:06
  • nevermind...it is not working ...it doesn't validate agains that enum list. arrgh – Max Mar 18 '15 at 20:37

1 Answers1

1

To re-use a xs:simpleType, you must give it a name (check) and make it global (see below):

<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>

Then the following XML will be valid:

<servers>
  <server id="76883A93-99EE-4571-B9FA-C4AE6D2A3ED1" type="IIS"/>
</servers>

And the following XML will be invalid:

<servers>
  <server id="76883A93-99EE-4571-B9FA-C4AE6D2A3ED1" type="BAD"/>
</servers>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • hmm I don't see anything different in your example. What am I missing? I compared yours with mine and they look exactly the same. – Max Mar 18 '15 at 20:41
  • yes I moved it right after the line you specified but it is not validating still. – Max Mar 18 '15 at 20:42
  • I've added two XML documents that demonstrate that the type is being found and enforced properly. I've confirmed that they work properly. I've pruned away the huge number of elements and attribute in your posted XML that are non-essential to your question. (In the future, please be sure to post a [**Minimal, Complete, and Verifiable Example (MCVE)**](http://stackoverflow.com/help/mcve) that exhibits the problem. Thanks.) – kjhughes Mar 18 '15 at 20:52
  • I will go ahead and mark as a answer but the truth is moving that snipset to " global" breaks all the other rules in my file, for instance, ipaddress stopped being validated. I am new to XSD so I will have to do my homework and understand how this thing works - thanks – Max Mar 18 '15 at 20:54
  • Sorry, with what you've posted, I cannot advise on the `ipaddress` problem. If you don't figure it out, feel free to post a new question that is complete, but minimal, exhibiting the issue. Do know, though, that the simpleType has to be global to be shared, and this alone should not cause you trouble with your other definitions. Hope this helps. Thanks. – kjhughes Mar 18 '15 at 20:59