10

I just want to know for what and when:

<xsd:simpleContent>
...
</xsd:simpleContent>

is used.

4 Answers 4

13

<xsd:simpleContent> is used when you have an element that can contain structural markup (=complex type) and the element is not allowed to contain child elements. In other words the elements content type allows only attributes and text content. Example: <foo bar="baz">foobar</foo> is an element defined with <xsd:complexType> and <xsd:simpleContent>.

It is true that using <xsd:simpleContent> involves creating a type either by restriction or by extension, but actually all complex types are implicitly either restrictions or extensions. Extension or restriction is just not necessary explicitly written in code because there is an abbreviated syntax that allows leaving them out.

7

If you want an element whose value is a date, and which takes attributes, like this:

<event type="birthday">2011-07-17</event>

then you need a complex type with simple content (CT-SC). It's defined by taking the content type - xs:date - and extending it with an attribute definition for the "type" attribute.

1
  • This is helpful. Could you add the schema as well? Commented Aug 8, 2022 at 16:12
2

As Jordan has said it allows to extend complexType, for instance:

  <xsd:complexType name="SizeType">
      <xsd:simpleContent>
        <xsd:extension base="xsd:integer">
          <xsd:attribute name="system" type="xsd:token"/>
        </xsd:extension>
      </xsd:simpleContent>
  </xsd:complexType>

I suggest to see these examples, they have been very useful to me:

http://www.datypic.com/books/defxmlschema/examples.html

4
  • Thx folks for the replies, but i still dont get it. When i have a complex type, i can do all this without the simpleContent?
    – Gobliins
    Commented Jul 12, 2011 at 8:58
  • I think it depends on the type of complexType that you want. See this example, there is the xml that you want and the correspondent xsd:datypic.com/books/defxmlschema/chapter13.html As you can see there isn't any simpleContent here.
    – Shilaghae
    Commented Jul 12, 2011 at 10:38
  • i figured out that simpleContent is used when extension or restriction base is used.
    – Gobliins
    Commented Jul 12, 2011 at 10:48
  • Yes! I'm sorry I noticed that I didn't write the result of above example. <size system="US-DRESS">10</size>
    – Shilaghae
    Commented Jul 12, 2011 at 11:00
2

Basically it allows you to extend a complexType element. If you had a "decimal" complexType, you could extend it with simpleContent to be a "currency" type by adding in a currency sign like $ or €, and a code such as USD or EUR. 4.75 as a decimal would become something like $4.75 USD with those extensions.

Microsoft's article is good for a basic understanding: http://msdn.microsoft.com/en-us/library/ms256106.aspx

1
  • Thx folks for the replies, but i still dont get it. When i have a complex type, i can do all this without the simpleContent?
    – Gobliins
    Commented Jul 12, 2011 at 8:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.