This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attribute declaration in XML Schema

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nicolas Stern
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it was more like described here :
XSD at W3Schools

And so the code should be :
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic