Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8761043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:11:45+00:00 2026-06-13T15:11:45+00:00

I am trying to implement an XML Schema which will enforce the following the

  • 0

I am trying to implement an XML Schema which will enforce the following the XML ;

<databases>
    <database>
        <name>"Test A"</name>
        <host>"192.168.0.100"</host>
        <default>yes</default>
    </database>
    <database>
        <name>"Test B"</name>
        <host>"192.168.0.200"</host>
        <default>no</default>        
    </database>
    <database>
        <name>"Test C"</name>
        <host>"localhost"</host>
        <default>no</default>        
    </database>
</databases>

I am able to implement the XML Schema myself except for one crucial issue; and that is that a maximum of only one database should be marked as the default. This means that zero databases could be marked as the default and this should also be considered valid.

As an example, the following XML should be considered invalid by the XML Schema, since more than one database is marked as default.

<databases>
    <database>
        <name>"Test A"</name>
        <host>"192.168.0.100"</host>
        <default>yes</default>
    </database>
    <database>
        <name>"Test B"</name>
        <host>"192.168.0.200"</host>
        <default>no</default>        
    </database>
    <database>
        <name>"Test C"</name>
        <host>"localhost"</host>
        <default>yes</default>        
    </database>

Whereas the following XML should be considered valid by the XML Schema since no (zero) databases are marked as default ;

<databases>
    <database>
        <name>"Test A"</name>
        <host>"192.168.0.100"</host>
        <default>no</default>
    </database>
    <database>
        <name>"Test B"</name>
        <host>"192.168.0.200"</host>
        <default>no</default>        
    </database>
    <database>
        <name>"Test C"</name>
        <host>"localhost"</host>
        <default>no</default>        
    </database>

Does anyone know if it is possible to enforce such a constraint with XML Schemas? I feel as though it should be, but I’m not sure how to go about implementing it.

Any assistance with respect to this matter would be immensely appreciated.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:11:46+00:00Added an answer on June 13, 2026 at 3:11 pm

    The easiest way to check that only one database is marked as the default is probably to structure your XML differently: record the databases as above, but drop the default element, and add an attribute or child element to the databases element which identifies the default database. Your XML becomes:

    <databases default="Test A">
        <database>
            <name>Test A</name>
            <host>"192.168.0.100"</host>
       </database>
        <database>
            <name>Test B</name>
            <host>"192.168.0.200"</host>
       </database>
        <database>
            <name>Test C</name>
            <host>"localhost"</host>
       </database>
    </databases>
    

    (I have dropped the quotation marks in the name elements, on the theory that they are not actually part of the database name. If they are part of the name, then the default attribute will need to be something like default='"Test A"', including the quotes.)

    You will need to ensure that the database/name element is unique; do this with an xs:key construct in the declaration of the databases element. You will also need to ensure that the optional default attribute points at a database name; do this with xs:keyref. The declaration of databases might look something like this:

    <xs:element name="databases" type="databases">
      <xs:key name="dbname">
        <xs:selector xpath="database"/>
        <xs:field xpath="name"/>
      </xs:key>
      <xs:keyref refer="dbname" name="defaultdb">
        <xs:selector xpath="."/>
        <xs:field xpath="@default"/>
      </xs:keyref>
    </xs:element>
    

    A second approach makes a slightly devious use of xs:unique, but again requires refactoring your XML. Instead of making the third child of database be default with values yes or no, make the third child be either an element named default-database or an element named non-default-database (change the names to suit your preferences, of course). Define these in such a way as to ensure that every instance of default-database has the same simple-type value.

    Then specify that no two occurrences of default-database may have the same string value, using xs:unique:

    <xs:unique name="dbname">
      <xs:selector xpath="database"/>
      <xs:field xpath="default-database"/>
    </xs:unique>
    

    Since every instance of default-database has the same value, this uniqueness constraint ensures that there can only ever be one such element. The schema as a whole might look something like this. First the bookkeeping:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      elementFormDefault="qualified">
    

    Now the element declaration for databases:

      <xs:element name="databases" type="databases">
        <xs:unique name="dbname">
          <xs:selector xpath="database"/>
          <xs:field xpath="default-database"/>
        </xs:unique>
      </xs:element>
    

    And its complex type:

      <xs:complexType name="databases">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="database"/>
        </xs:sequence>
        <xs:attribute name="default" type="xs:string" use="optional"/>
      </xs:complexType>
    

    Now some more bookkeeping: declaration for database, just to keep us honest.

      <xs:element name="database" type="database"/>
    
      <xs:complexType name="database">
        <xs:sequence>
          <xs:element name="name" type="xs:string"/>
          <xs:element name="host" type="xs:string"/>
          <xs:choice>       
            <xs:element name="default-database" type="empty"/>
            <xs:element name="non-default-database" type="empty"/>
          </xs:choice>
        </xs:sequence>
      </xs:complexType>
    

    And a final bit of bookkeeping: a type that ensures that every instance of default-database has the same value. (There are other ways to achieve this, of course; this is just the one that came to mind first.)

      <xs:simpleType name="empty">
        <xs:restriction base="xs:string">
          <xs:enumeration value=""/>
        </xs:restriction>
      </xs:simpleType> 
    
    </xs:schema>
    

    If you are absolutely married to your existing design, then I don’t know how to do what you want to do in XSD 1.0; if you can use XSD 1.1, of course, you can add an assertion on databases that says

    <xs:assert test="count(database[default = 'yes']) = 1"/>
    

    But as the alternative solutions above illustrate, that design and the use of assertions is not your only option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a very simple XML schema constraint. The idref attribute on
I am trying to implement a protocol which I will use for my application
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
I'm currently configuring my schema.xml file and trying to figure out what's the best
I was trying to add the XML schema to an existing EJB project. JAXB
I'm trying to implement a list using the ListView, which contains rows built with
I'm trying to implement search functionality in my app, which is very basic at
I am trying to implement a short converter using TinyXML that will take an
I'm trying to implement a custom ListView which has separators in it. This is
I am trying to implement a layout, which contains a list of ViewPagers. Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.