How to utilize complexType without wrapper element in XSD?

Roland

How do I write an XSD for this XML-1 (simplified examples):

<?xml version="1.0" encoding="utf-8"?>
<user>
  <username>Albert Einstein</username>
  <!-- no wrapper element for address -->
  <street>Main Street</street>
  <city>Ghost Town</city>
  <state>Up State</state>
</user>

instead of the more logical XML-2:

<?xml version="1.0" encoding="utf-8"?>
<user>
  <username>Albert Einstein</username>
  <!-- with wrapper element for address -->
  <address>
    <street>Main Street</street>
    <city>Ghost Town</city>
    <state>Up State</state>
  </address>
</user>

where username stands for all fields that are not part of the database table for addresses, which is a table that I cannot just change to include more fields,

with some kind of record type for the address fields like this XSD:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="user" type="user" />
  <!-- =========================================== -->
  <xsd:complexType name="user">
    <xsd:sequence>
      <!-- username stands for all non-address fields -->
      <xsd:element name="username" type="xsd:string" />
      <!-- wrapper element for the address fields -->
      <xsd:element name="address" type="address_Type" />
    </xsd:sequence>
  </xsd:complexType>
  <!-- =========================================== -->
  <xsd:complexType name="address_Type">
    <!-- the columns of the database table for addresses -->
    <xsd:sequence>
      <xsd:element name="street" type="xsd:string" />
      <xsd:element name="city" type="xsd:string" />
      <xsd:element name="state" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

Using the tools in Visual Studio I verified that XML-2 validates with the XSD, but XML-1 does not. How do I improve the "problem line" in the XSD? Apparently, you cannot write in the XSD an element without name:

<xsd:element type="address_type" />

I need to include the complexType for the address fields without the enclosing

<address> 

tag.

Reason for this question is that this XSD will be useful to create a C# class for the database table address using xsd.exe in the visual studio sdk.

I will have to process many different format XML's, with most of them having address data within the address wrapper element. It is easy to expand the XSD to validate those XML's. The problem is with XML with address data without wrapper element, that seem not to be validatable with XSD with the complexType Address_Type. Unless someone smarter than myself sees a solution.

Because of the relation between the complexType and the structure of the database table, I cannot just add the username field to the complexType. Username really stands for all fields that are NOT in the address table.

I am open for all suggestions. Even after doing quite some XSD stuff I still feel like a beginner and I am not even sure if using xsd:sequence is needed at all.

kjhughes

Update: New Q/A that addresses @Roland's refined requirements has been created here:

Original, outdated answer and updates follow...


If you do not want a wrapper address element, you can simply not define one, and place its content model directly within the content model of user:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="user" type="user" />
  <xsd:complexType name="user">
    <xsd:sequence>
      <xsd:element name="username" type="xsd:string" />
      <xsd:element name="street" type="xsd:string" />
      <xsd:element name="city" type="xsd:string" />
      <xsd:element name="state" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

Per your comment, you don't want to do the above; you want to reuse the elements in address_Type.

There is no direct way to say what you want, but you could have the user complex type extend the address_Type complex type. This would allow you to add the username element to those elements already appearing in address_type (although it would read rather unnaturally to extend an address in such a way).

To reuse parts of content models more naturally, consider using xs:group. For examples, see

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to utilize complexType without wrapper element in XSD?

From Dev

XSD : ComplexType Element With the Same Name

From Dev

XSD - <xs:sequence> without <xs:complexType>?

From Dev

How to limit defined complexType in XSD Schema file

From Dev

Can I give the same name to complexType and element - XSD Standards

From Dev

Can I give the same name to complexType and element - XSD Standards

From Dev

Is an empty complexType valid in an XSD?

From Dev

Why is xsd:complexType not an attribute?

From Dev

Restrict complexType with attributes in XSD?

From Dev

parsing xsd complexType recursively

From Dev

How can I remove wrapper (parent element) without removing the child?

From Dev

A complex type without an element in XSD

From Dev

A complex type without an element in XSD

From Dev

How does JQuery utilize $ without a function by this name?

From Dev

xsd:complexType with children, attributes, and restrictions

From Dev

How to declare a complexType has only one child element?

From Dev

How to declare a complexType has only one child element?

From Dev

Requiring one element without attributes XSD

From Dev

Change WSDL xsd:complexType name with Apache CXF

From Dev

xsd restrict children to children of another complexType

From Dev

xsd:complexType after xsd:attribute declarations yields error

From Dev

Unique constraint on a complexType instead of an element

From Dev

Wrap function parameters complexType in an element

From Dev

XSD: how to reference types from external XSD without importing elements?

From Dev

How to keep xsd keyref confined to a specific element?

From Dev

How to define a simple element and attribute in XSD

From Dev

How to access parent element in XSD assertion XPath?

From Dev

How to restrict combination of element values in XSD?

From Dev

How to access parent element in XSD assertion XPath?

Related Related

  1. 1

    How to utilize complexType without wrapper element in XSD?

  2. 2

    XSD : ComplexType Element With the Same Name

  3. 3

    XSD - <xs:sequence> without <xs:complexType>?

  4. 4

    How to limit defined complexType in XSD Schema file

  5. 5

    Can I give the same name to complexType and element - XSD Standards

  6. 6

    Can I give the same name to complexType and element - XSD Standards

  7. 7

    Is an empty complexType valid in an XSD?

  8. 8

    Why is xsd:complexType not an attribute?

  9. 9

    Restrict complexType with attributes in XSD?

  10. 10

    parsing xsd complexType recursively

  11. 11

    How can I remove wrapper (parent element) without removing the child?

  12. 12

    A complex type without an element in XSD

  13. 13

    A complex type without an element in XSD

  14. 14

    How does JQuery utilize $ without a function by this name?

  15. 15

    xsd:complexType with children, attributes, and restrictions

  16. 16

    How to declare a complexType has only one child element?

  17. 17

    How to declare a complexType has only one child element?

  18. 18

    Requiring one element without attributes XSD

  19. 19

    Change WSDL xsd:complexType name with Apache CXF

  20. 20

    xsd restrict children to children of another complexType

  21. 21

    xsd:complexType after xsd:attribute declarations yields error

  22. 22

    Unique constraint on a complexType instead of an element

  23. 23

    Wrap function parameters complexType in an element

  24. 24

    XSD: how to reference types from external XSD without importing elements?

  25. 25

    How to keep xsd keyref confined to a specific element?

  26. 26

    How to define a simple element and attribute in XSD

  27. 27

    How to access parent element in XSD assertion XPath?

  28. 28

    How to restrict combination of element values in XSD?

  29. 29

    How to access parent element in XSD assertion XPath?

HotTag

Archive