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 8676013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:05:05+00:00 2026-06-12T20:05:05+00:00

The question in short – can I define a schema within a schema which

  • 0

The question in short – “can I define a schema within a schema which can be validated as a whole?

Explanation:

Is it possible to define a schema for the following XML. I need to define a schema for “customer”. The “customertype” child element itself is a schema. Within the customertype I should have an element called “Source” which is mandatory.

 <customer>
    <customername>acustomer</customername>
    <customertype>
      <xs:schema>
      <xs:element name="profession">
         <xs:complexType>
              <xs:sequence>
                 <xs:element name="Source" type="xs:int" />
                 <xs:element name="ProfessionName" type="xs:string" />
             </xs:sequence>
          </xs:complexType>     
         </xs:schema>
      </customertype>
  </customer>

Is it possible to Define the schema for this xml so that all the requirements are met?

  • 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-12T20:05:06+00:00Added an answer on June 12, 2026 at 8:05 pm

    As Mimo has pointed out, there’s no problem defining customertype (or another other element) as containing elements in the XSD namespace, which is what you appear to be asking about.

    But if your goal is to be able to validate customer elements (or profession elements, which is what the schema in your example declares), it’s hard to imagine a validation architecture in which that’s the best way (or even a workable way) to go about it. One reason is that validating a document instance against schema information provided by the instance being validated doesn’t produce the same confidence in the data’s cleanliness as validating it against a known schema. (Put yourself in the shoes of an adversary seeking to subvert your validation and persuade your system to accept bogus data as valid. If the adversary gets to specify what counts as a valid document instance, how useful is it to know that the document is valid?)

    What is it that prevents you from writing a schema and using it in the usual way?

    [Addition, 15 October 2012, after OP’s comment]

    If I’ve understood your comment of earlier today correctly, your requirement is to allow people other than you to specify the type of the customer element however they like, subject to the proviso that that type must contain a child element named Source, whose type will be xsd:int. You don’t specify whether you need access to the type definition they are using or not, so I’ll try to consider both the case where you do need it and the case where you don’t need it.

    Three ways to make this situation work are described below. They have in common that there is

    • a ‘main’ schema document that defines a basic version of the schema, and
    • one or more ‘auxiliary’ schema documents for use in different situations.

    In general, you may find it helpful to find a good textbook on XSD and review what it says about creating a schema from declarations in several schema documents.

    (1) One approach uses xsi:type. You define a main schema document in which the customer element has a named type; I’ll assume the type is named Customer. The Customer type accepts any element whose first child element is named Source. For example:

    <xs:element name="customer" type="Customer"/>
    <xs:complexType name="Customer">
      <xs:sequence>
        <xs:element name="Source" type="xs:int"/>
        <xs:any minOccurs="0" 
                maxOccurs="unbounded" 
                processContents="lax"/>
      </xs:sequence>
      <xs:anyAttribute processContents="lax"/>
    </xs:complexType>
    

    Those who want a more specific type for the customer element (I’ll call them the ‘users’) provide auxiliary schema documents for your target namespace in which they declare other complex types which restrict Customer. For example, they might want the customer element to contain elements called name, address, and phone number:

    <xs:complexType name="Customer-for-us">
      <xs:complexContent>
        <xs:restriction base="Customer">
          <xs:sequence>
            <xs:element name="Source" type="xs:int"/>
            <xs:element ref="name"/>
            <xs:element ref="address"/>
            <xs:element ref="phone"/>
          </xs:sequence>
        </xs:restriction>
      </xs:complexContent>
    </xs:complexType>
    

    This is a legal restriction of the Customer type, so the customer element can use it. A document instance might therefore contain an element like:

    <customer xsi:type="Customer-for-us">
      <Source>83760273</Source>
      <name>Willy Wonka</name>
      <address> ... </address>
      <phone> ... </phone>
    </customer>
    

    The document is validated against the schema constructed from their auxiliary schema document, together with the main schema document, so the definition of type Customer-for-us is enforced in the usual way.

    By using wildcards and lax validation, the Customer type ensures that users can do anything they like in their version of the type, as long as the first child is named Source and has type int.

    (2) A second approach uses a hole in the main schema document.

    You write a main schema document as before, including the declaration of the customer element as having type Customer. But the main schema document does not contain a declaration for that type. Instead, you declare the Customer type in an auxiliary schema document, which is combined with the main one at validation time in the usual way (I’d recommend you have a third schema document which serves as a driver and includes the other two, but there are many ways to make it work).

    The users who want a more specific Customer type, meanwhile, write their own declaration for the Customer type, subject to the compatibility constraints about the first child named Source and so on. The users use their own driver file, which embeds the main schema document and their version of the auxiliary schema document with their own declaration of the Customer type.

    This way, the xsi:type attribute does not need to be used.

    (3) A third approach uses the xs:redefine or (in XSD 1.1) the xs:override facility.

    You write the main schema document as described in solution (1). The users use xs:redefine or xs:override to redefine Customer as they wish. This answer is already rather long, so I do not propose to include a tutorial on the use of either redefine or override.

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

Sidebar

Related Questions

The short question: how can I (or is it possible to) make use of
Short question, is the following ok: struct X { A& x; A y; X()
Short question: Is it possible to detect window.open() in a UIWebView using the UIWebViewDelegate
Short Question: Can I specify wildcards for custom console folding? If so, what is
Short question - how do you define your view models? Here are some of
Short question: Is it possible to do a redirection, say when a user isn't
A short question: I've got a TabNavigator with multiple canvas children. How can i
Short question: How can I modify individual items in a List ? (or more
Short question: If I have class that impelemnts FactoryBean interface, how can I get
Short Question: What's the best date format to use in SQL Server? Long Explanation:

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.