Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » Find out the root element
Find out the root element [message #597864] Mon, 10 April 2006 06:42
Eclipse UserFriend
Originally posted by:

Hi all,

I want to find out whether a given XSD schema contains more then one root
currently, i did not find any "simple" utility in the API that will do the
I wrote code that will handle it, but it's not handle properly all
possible cases.

my questions are:
1) There is any "simple" utility in the API that return the list of the
root elements ( not .getElementDeclarations()) or somthing similar ?

2) in case the first question is no. Why the following code:

private XSDElementDeclaration findRootElement(XSDSchema schema)
XSDElementDeclaration elementDeclaration;
Hashtable rootElementHash = new Hashtable();
Object o;
String name;
String qName;

// in case there is only one element in the root level, should be
the root element
if(schema.getElementDeclarations().size() == 1)
(XSDElementDeclaration)schema.getElementDeclarations().itera tor().next();

// insert all suspicious root elements into hash table
for (Iterator i =

rootElementHash.put(elementDeclaration.getName(),elementDecl aration);

//the last item that left in the hash table is the root element
for (Iterator i =
schema.getElementDeclarations().iterator();i.hasNext();) // for each
element on the root level

for(TreeIterator j =
elementDeclaration.eAllContents();j.hasNext();) // for each element in
the sub tree under
// the the element in the root level
o =;
if(o instanceof XSDElementDeclaration )
XSDElementDeclaration elemDec =
qName = elemDec.getQName();
name = elemDec.getName();
if (name != null) // the element is not
reference thus no need to remove the object
if(qName != null &&
rootElementHash.containsKey(qName) ) // in case the element from the
sub tree exist
// in the hash table, remove the item from the table

throw new RuntimeException("no root element in the schema");

(XSDElementDeclaration)rootElementHash.values().iterator().n ext();
throw new RuntimeException("more then one root element in the

return wrong answear ("more then one root element in the schema") for the
given schema: (I have already tested successfully the code with different
scheam with reference elements)

<xs:schema xmlns:xs="">

<xs:element name="purchaseOrder" type="PurchaseOrderType"/>
<xs:element name="comment" type="xs:string"/>

<xs:complexType name="PurchaseOrderType">
<xs:element name="shipTo" type="USAddress"/>
<xs:element name="billTo" type="USAddress"/>
<xs:element ref="comment" minOccurs="0"/>
<xs:element name="items" type="Items"/>
<xs:attribute name="orderDate" type="xs:date"/>
<xs:complexType name="USAddress">
<xs:element name="name" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:decimal"/>
<xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/>
<xs:complexType name="Items">
<xs:element name="item" minOccurs="0" maxOccurs="unbounded">
<xs:element name="productName" type="xs:string"/>
<xs:element name="quantity">
<xs:restriction base="xs:positiveInteger">
<xs:maxExclusive value="100"/>
<xs:element name="USPrice" type="xs:decimal"/>
<xs:element ref="comment" minOccurs="0"/>
<xs:element name="shipDate" type="xs:date" minOccurs="0"/>
<xs:attribute name="partNum" type="SKU" use="required"/>
<!-- Stock Keeping Unit, a code for identifying products -->
<xs:simpleType name="SKU">
<xs:restriction base="xs:string">
<xs:pattern value="\d{3}-[A-Z]{2}"/>

what is wrong with my code ?


Previous Topic:extension base
Next Topic:Find out the root element
Goto Forum:

Current Time: Wed Oct 26 17:26:51 GMT 2016

Powered by FUDForum. Page generated in 0.01783 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software