3

I want to define an element with some restricted text and an attribute

<some_element my_attr="data">some restricted text</some_element> 

How can I create an element like this?

I tried:

<complexType> <simpleContent> <restriction base="string"> <pattern value="AAA"></pattern> <attribute name="newattr" type="string"></attribute> </restriction> </simpleContent> </complexType> 

But getting error msg:

Complex Type Definition Representation Error for type '#AnonType_childparent'. When is used, the base type must be a complexType whose content type is simple, or, only if restriction is specified, a complex type with mixed content and emptiable particle, or, only if extension is specified, a simple type. 'string' satisfies none of these conditions.

And then tried something like this

<complexType> <complexContent> <extension base="string"> <attribute name="attr" type="string"></attribute> </extension> </complexContent> </complexType> 

This time error was (This time I didn't add any restriction; this was for test purpose only):

Complex Type Definition Representation Error for type '#AnonType_childparent'. When is used, the base type must be a complexType. 'string' is a simpleType.

I am not getting clearly what the errors are implying? Is there some better explanation of error text available?

1 Answer 1

5

Your question is more or less a duplicate of this question (just answered it).

Think in these terms:

  • if your element has attributes and/or nested elements, it must be a complex type.
  • if your element can have text, but no markup (nested elements) then it must be of simpleContent
  • simpleContent can only be extended from a simple type (if you need to add an attribute). If you need additional constraints on the simple type, you need to specifically create a new simple type, restricting the base type, then extend that in a simple content.

This post on SO shows you an end-to-end example, of the above explanation. For your particular case:

<?xml version="1.0" encoding="utf-8" ?> <!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> <xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:simpleType name="tSomeRestrictedText"> <xsd:restriction base="xsd:string"> <xsd:pattern value="AAA"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="some_element"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="tSomeRestrictedText"> <xsd:attribute name="my_attr" type="xsd:string"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> </xsd:schema> 
Sign up to request clarification or add additional context in comments.

4 Comments

the problem I am facing is, I know how to add text content with attribute to an element, but I cannot do it for restricted content.
@MasterChief, not sure I understand; have a look at the updated response; does it work for you? If not, please clarify.
I have marked this as answer, as it does the job. But can you clarify, why we need to split in 2 parts. Why can't we extend a restricted type directly(using nesting)?
@MasterChief, your goal is to add an attribute (you're creating a new complex type/simple content from scratch). To add, you therefore extend. the extension particle under simpleContent only supports addition of attributes, nothing more. Restriction particles, such as the facets you might be interested in, are only part of the restriction particle.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.