aspose file tools*
The moose likes XML and Related Technologies and the fly likes Attribute declaration in XML Schema Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Attribute declaration in XML Schema" Watch "Attribute declaration in XML Schema" New topic
Author

Attribute declaration in XML Schema

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
How can we declare attributes for child elements.

<element name="name">
<complexType>
<sequence>
<element name="firstName" type="string"/>
<element name="middleName" type="string"/>
</sequence>
</complexType>
</element>

In the above schema declaration I would like to add an attribute called "nickName" to "firstName" child element of "name" element. Also the firstName should have only text characters within it. NO element is allowed.

How to declare this requirement in schema. I would also like to know whether this is possible. Bacause I tried with complexType declaration like

<element name="firstName" type="string">
<complexType>
<attribute name="nickName" type="String" use="option"/>
</complexType>
<element>

I ended up getting error

"Element 'firstName' cannot have both a type attribute and a simpleType/complexType type child"

After removing the "type" attribute I still got error

"No character data is allowed by content model"

By making use of "mixed" attribute set to "true" in "complexType" I can rectify this error. But that would allow child element declarations within "firstName" element. But I only want character data to be present within it.

I tried with "simpleType".

<element name="firstName" type="string">
<simpleType>
<attribute name="nickName" type="String" use="option"/>
</simpleType>
<element>

But I ended up get error

'attribute' is not allowed for the content of a simpleType. Only list, union and restriction are allowed.

I appreciate your help


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Nicolas Stern
Ranch Hand

Joined: Apr 26, 2002
Posts: 57
Hello

the answer is stated in the question: you can't declare an attribute in a simpleType. You can use a complexType.



Hth - cheers
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Hello Nicolas Stern

Thanks for your reply.

I did what you said already and indeed it worked fine. But my question is different.

By adding "mixed" attribute we may allow child element declarations to be part of that element. But what I want is only text characters. NO element declaration should be part of that element.

How to declare that this element(firstName) can contain ONLY Character Data."
[ March 15, 2006: Message edited by: Vishnu Prakash ]
Nicolas Stern
Ranch Hand

Joined: Apr 26, 2002
Posts: 57
Hello

I'm not sure I understand how your validation should work:

If you define firstName as follow(complex type with the attribute declaration and mixed="true") then your element firstName allows no child element:



This will pass validation:




While this won't:



Isn't it what you need ?

Hth - cheers
Zevino Troy
Greenhorn

Joined: Jul 27, 2010
Posts: 1
I think it was more like described here :
XSD at W3Schools

And so the code should be :
 
 
subject: Attribute declaration in XML Schema