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 #67394] Mon, 10 April 2006 06:42 Go to next message
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
Re: Find out the root element [message #67415 is a reply to message #67394] Mon, 10 April 2006 12:01 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Gilad,

Any global element can be the root element of an instance, so I don't
know how you define a "root" element to be more restrictive than that.
Can you elaborate on what you mean by the term root element.


Gilad wrote:

> 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
>
>
Re: Find out the root element [message #67434 is a reply to message #67415] Mon, 10 April 2006 13:07 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gilado74.gmail.com

i meant, for my needs i need to distinguish between elements that contains
sub elements and elements that referenced by other elements in the schema.

E.G.
both "purchaseOrder" and "comment" are in root level but the "comment"
element is referenced from the "PurchaseOrderType" complexType thus it
can't be root element of XML (for my application).

<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:complexType>
Re: Find out the root element [message #67455 is a reply to message #67434] Mon, 10 April 2006 13:47 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Gilad,

Elements which contain sub elements can also be referenced by other
elements, so this characterization doesn't help make things much
clearer. You can analyze the type of an element to understand if it's
complex or simple and you can use XSDUtil.UsageCrossReferencer to find
uses of all global elements to decide if any particular type of use
makes the element not a root.


Gilad wrote:

>
> i meant, for my needs i need to distinguish between elements that
> contains sub elements and elements that referenced by other elements
> in the schema.
>
> E.G. both "purchaseOrder" and "comment" are in root level but the
> "comment" element is referenced from the "PurchaseOrderType"
> complexType thus it can't be root element of XML (for my application).
> <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:complexType>
>
Re: Find out the root element [message #67477 is a reply to message #67415] Mon, 10 April 2006 14:11 Go to previous message
Eclipse UserFriend
Originally posted by: patrick.mcgovern.unisys.cm

"Ed Merks" <merks@ca.ibm.com> wrote in message
news:e1dheu$174$1@utils.eclipse.org...
> Gilad,
>
> Any global element can be the root element of an instance, so I don't know
> how you define a "root" element to be more restrictive than that. Can you
> elaborate on what you mean by the term root element.

Does he mean an element that doesnt have a parent?

>

>
> Gilad wrote:
>
>> 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
>>
>>
Re: Find out the root element [message #597872 is a reply to message #67394] Mon, 10 April 2006 12:01 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Gilad,

Any global element can be the root element of an instance, so I don't
know how you define a "root" element to be more restrictive than that.
Can you elaborate on what you mean by the term root element.


Gilad wrote:

> 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
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Find out the root element [message #597879 is a reply to message #67415] Mon, 10 April 2006 13:07 Go to previous message
Eclipse UserFriend
Originally posted by: gilado74.gmail.com

i meant, for my needs i need to distinguish between elements that contains
sub elements and elements that referenced by other elements in the schema.

E.G.
both "purchaseOrder" and "comment" are in root level but the "comment"
element is referenced from the "PurchaseOrderType" complexType thus it
can't be root element of XML (for my application).

<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:complexType>
Re: Find out the root element [message #597882 is a reply to message #67434] Mon, 10 April 2006 13:47 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Gilad,

Elements which contain sub elements can also be referenced by other
elements, so this characterization doesn't help make things much
clearer. You can analyze the type of an element to understand if it's
complex or simple and you can use XSDUtil.UsageCrossReferencer to find
uses of all global elements to decide if any particular type of use
makes the element not a root.


Gilad wrote:

>
> i meant, for my needs i need to distinguish between elements that
> contains sub elements and elements that referenced by other elements
> in the schema.
>
> E.G. both "purchaseOrder" and "comment" are in root level but the
> "comment" element is referenced from the "PurchaseOrderType"
> complexType thus it can't be root element of XML (for my application).
> <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:complexType>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Find out the root element [message #597890 is a reply to message #67415] Mon, 10 April 2006 14:11 Go to previous message
Patrick McGovern is currently offline Patrick McGovernFriend
Messages: 5
Registered: July 2009
Junior Member
"Ed Merks" <merks@ca.ibm.com> wrote in message
news:e1dheu$174$1@utils.eclipse.org...
> Gilad,
>
> Any global element can be the root element of an instance, so I don't know
> how you define a "root" element to be more restrictive than that. Can you
> elaborate on what you mean by the term root element.

Does he mean an element that doesnt have a parent?

>

>
> Gilad wrote:
>
>> 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:Find out the root element
Next Topic:Embedded elements
Goto Forum:
  


Current Time: Tue Apr 16 15:23:43 GMT 2024

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

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

Back to the top