Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » 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: gilado74.gmail.com

Hi all,

I want to find out whether a given XSD schema contains more then one root
element.
currently, i did not find any "simple" utility in the API that will do the
job.
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)
{
return
(XSDElementDeclaration)schema.getElementDeclarations().itera tor().next();
}

// insert all suspicious root elements into hash table
for (Iterator i =
schema.getElementDeclarations().iterator();i.hasNext();)
{
elementDeclaration=(XSDElementDeclaration)i.next();

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
{
elementDeclaration=(XSDElementDeclaration)i.next();

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

if(rootElementHash.isEmpty())
throw new RuntimeException("no root element in the schema");

if(rootElementHash.size()==1)
{
return
(XSDElementDeclaration)rootElementHash.values().iterator().n ext();
}
throw new RuntimeException("more then one root element in the
schema");
}



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="http://www.w3.org/2001/XMLSchema">

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

<xs:complexType name="PurchaseOrderType">
<xs:sequence>
<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:sequence>
<xs:attribute name="orderDate" type="xs:date"/>
</xs:complexType>
<xs:complexType name="USAddress">
<xs:sequence>
<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:sequence>
<xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/>
</xs:complexType>
<xs:complexType name="Items">
<xs:sequence>
<xs:element name="item" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="productName" type="xs:string"/>
<xs:element name="quantity">
<xs:simpleType>
<xs:restriction base="xs:positiveInteger">
<xs:maxExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="USPrice" type="xs:decimal"/>
<xs:element ref="comment" minOccurs="0"/>
<xs:element name="shipDate" type="xs:date" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="partNum" type="SKU" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<!-- 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}"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>



what is wrong with my code ?

Thanks,

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


Current Time: Fri Apr 19 20:47:58 GMT 2024

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

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

Back to the top