Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » How to get elements from included schema?
How to get elements from included schema? [message #61412] Fri, 10 June 2005 05:21 Go to next message
Eclipse User
Originally posted by: ksrki.uns.ns.ac.yu

Hi,

I have two schemas (A and B) and schema A includes contents from the
schema B via <xsd:include> property of XML Schema rec. It (schema A)
also references some of the elements of the schema B. When I traverse
schema A I'm not able to get any info about the type definition of the
referenced elements, ie. element.getTypeDefinition() returns null.

Is there any cure to this situation?

Thanks for help,
Srdjan Komazec
Re: How to get elements from included schema? [message #61436 is a reply to message #61412] Fri, 10 June 2005 06:42 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

Srdjan,

Maybe you need to do
element.getResolvedElementDeclaration().getTypeDefinition() because you
have traversed to an element ref?


Srdjan Komazec wrote:

> Hi,
>
> I have two schemas (A and B) and schema A includes contents from the
> schema B via <xsd:include> property of XML Schema rec. It (schema A)
> also references some of the elements of the schema B. When I traverse
> schema A I'm not able to get any info about the type definition of the
> referenced elements, ie. element.getTypeDefinition() returns null.
>
> Is there any cure to this situation?
>
> Thanks for help,
> Srdjan Komazec
>
Re: How to get elements from included schema? [message #61457 is a reply to message #61436] Fri, 10 June 2005 08:47 Go to previous messageGo to next message
Eclipse User
Originally posted by: ksrki.uns.ns.ac.yu

Thanks on fast replay.

Unfortunatelly, that is not an issue because I have called
getResolvedElementDeclaration() for all XSDElementDeclaration instances
before any other method of the class to be sure that I have concrete
element (this method returns the same instance if it is not element ref).

I'm wondering if there is any special treatment for those elements that
came from another schema (included by <xsd:include> or imported by
<xsd:import>)?

Thanks in adavnced!


Ed Merks wrote:
> Srdjan,
>
> Maybe you need to do
> element.getResolvedElementDeclaration().getTypeDefinition() because you
> have traversed to an element ref?
>
>
> Srdjan Komazec wrote:
>
>> Hi,
>>
>> I have two schemas (A and B) and schema A includes contents from the
>> schema B via <xsd:include> property of XML Schema rec. It (schema A)
>> also references some of the elements of the schema B. When I traverse
>> schema A I'm not able to get any info about the type definition of the
>> referenced elements, ie. element.getTypeDefinition() returns null.
>>
>> Is there any cure to this situation?
>>
>> Thanks for help,
>> Srdjan Komazec
>>
Re: How to get elements from included schema? [message #61480 is a reply to message #61457] Fri, 10 June 2005 09:31 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

Srdjan,

As far as I know, the only way the type could be null is if the element
reference is an unresolved one, i.e., if element.getContainer() ==
null. Have you validated the schema (XSDMainTest in
org.eclipse.xsd.test shows how and the test directory there has scripts
you can use for command line validation) or tried opening the schema in
the Sample XML Schema Editor to check for problems? It sounds likely
that your imports or includes aren't resolving correctly.

My answers can be much more concrete if you show exactly what you are
doing (in terms of code) and what instances you are working with...


Srdjan Komazec wrote:

>
> Thanks on fast replay.
>
> Unfortunatelly, that is not an issue because I have called
> getResolvedElementDeclaration() for all XSDElementDeclaration
> instances before any other method of the class to be sure that I have
> concrete element (this method returns the same instance if it is not
> element ref).
>
> I'm wondering if there is any special treatment for those elements
> that came from another schema (included by <xsd:include> or imported
> by <xsd:import>)?
>
> Thanks in adavnced!
>
>
> Ed Merks wrote:
>
>> Srdjan,
>>
>> Maybe you need to do
>> element.getResolvedElementDeclaration().getTypeDefinition() because
>> you have traversed to an element ref?
>>
>>
>> Srdjan Komazec wrote:
>>
>>> Hi,
>>>
>>> I have two schemas (A and B) and schema A includes contents from the
>>> schema B via <xsd:include> property of XML Schema rec. It (schema A)
>>> also references some of the elements of the schema B. When I
>>> traverse schema A I'm not able to get any info about the type
>>> definition of the referenced elements, ie.
>>> element.getTypeDefinition() returns null.
>>>
>>> Is there any cure to this situation?
>>>
>>> Thanks for help,
>>> Srdjan Komazec
>>>
Re: How to get elements from included schema? [message #61503 is a reply to message #61480] Fri, 10 June 2005 10:13 Go to previous messageGo to next message
Eclipse User
Originally posted by: ksrki.uns.ns.ac.yu

This is a multi-part message in MIME format.
--------------050703010505030806000701
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Ed, thanks for fast replay again.

Both schema instances were created with XMLSpy. XML Schema editor
doesn't make any problem either.

I'm sending these two schema files (person.xsd and class.xsd). I'm also
sending code (TestInclude2.java) that is designed to show my problem
(and XSDSearch class which is utility class).

Hope to hear something soon.
Best regards,
Srdjan Komazec


Ed Merks wrote:
> Srdjan,
>
> As far as I know, the only way the type could be null is if the element
> reference is an unresolved one, i.e., if element.getContainer() ==
> null. Have you validated the schema (XSDMainTest in
> org.eclipse.xsd.test shows how and the test directory there has scripts
> you can use for command line validation) or tried opening the schema in
> the Sample XML Schema Editor to check for problems? It sounds likely
> that your imports or includes aren't resolving correctly.
> My answers can be much more concrete if you show exactly what you are
> doing (in terms of code) and what instances you are working with...
>
>
> Srdjan Komazec wrote:
>
>>
>> Thanks on fast replay.
>>
>> Unfortunatelly, that is not an issue because I have called
>> getResolvedElementDeclaration() for all XSDElementDeclaration
>> instances before any other method of the class to be sure that I have
>> concrete element (this method returns the same instance if it is not
>> element ref).
>>
>> I'm wondering if there is any special treatment for those elements
>> that came from another schema (included by <xsd:include> or imported
>> by <xsd:import>)?
>>
>> Thanks in adavnced!
>>
>>
>> Ed Merks wrote:
>>
>>> Srdjan,
>>>
>>> Maybe you need to do
>>> element.getResolvedElementDeclaration().getTypeDefinition() because
>>> you have traversed to an element ref?
>>>
>>>
>>> Srdjan Komazec wrote:
>>>
>>>> Hi,
>>>>
>>>> I have two schemas (A and B) and schema A includes contents from the
>>>> schema B via <xsd:include> property of XML Schema rec. It (schema A)
>>>> also references some of the elements of the schema B. When I
>>>> traverse schema A I'm not able to get any info about the type
>>>> definition of the referenced elements, ie.
>>>> element.getTypeDefinition() returns null.
>>>>
>>>> Is there any cure to this situation?
>>>>
>>>> Thanks for help,
>>>> Srdjan Komazec
>>>>


--------------050703010505030806000701
Content-Type: text/xml;
name="class.xsd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="class.xsd"

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:prob="http://ccd.ns.ac.yu/probaInclude" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ccd.ns.ac.yu/probaInclude" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xsd:include schemaLocation="person.xsd"/>
<xsd:element name="class">
<xsd:annotation>
<xsd:documentation>Comment describing your root element</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence maxOccurs="unbounded">
<xsd:element ref="prob:Person"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

--------------050703010505030806000701
Content-Type: text/xml;
name="person.xsd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="person.xsd"

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:prob="http://ccd.ns.ac.yu/probaInclude" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ccd.ns.ac.yu/probaInclude" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xsd:element name="Person">
<xsd:annotation>
<xsd:documentation>Comment describing your root element</xsd:documentation>
</xsd:annotation>

<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="Age" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>

--------------050703010505030806000701
Content-Type: text/x-java;
name="TestInclude2.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="TestInclude2.java"

package com.ccd.xsd.test;

import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;

import com.ccd.xsd.util.resolve.search.XSDSearch;

/**
* Class:TestInclude
*
* Date:Jun 8, 2005 11:00:05 AM
*
* Description:
*
* @author Srdjan Komazec FTN - Novi Sad <a href="mailto:ksrki@uns.ns.ac.yu">ksrki@uns.ns.ac.yu</a>
* @version 1.0
*/
public class TestInclude2 {

public TestInclude2(){

String schemaURL = "src/com/ccd/xsd/test/class.xsd";
XSDSchema schema = null;
Object component = null;
XSDElementDeclaration rootElement = null, childElement = null;
XSDComplexTypeDefinition complexTypeDefinition = null;
XSDComplexTypeContent complexTypeContent = null;
XSDParticleContent particleContent = null;


//Loading the schema
try {

schema = loadSchemaUsingResourceSet(schemaURL);

} catch (Exception e) {
e.printStackTrace();
}

//Browsing the schema content and searching for Element Declarations
Iterator iter = schema.getContents().iterator();

while (iter.hasNext()){

component = iter.next();

if (component instanceof XSDElementDeclaration){

//Found root element decalation
rootElement = (XSDElementDeclaration)component;
break;
}

}

//Taking type definition for class element
complexTypeDefinition = (XSDComplexTypeDefinition)rootElement.getTypeDefinition();

//Content of this complex type definition
complexTypeContent = complexTypeDefinition.getContent();

//Because I know that class sequence of person element refrences comprise class element I have created an
//iterator to what this sequence consists of (Person elemenet references)
iter = ((XSDModelGroup)((XSDParticle)complexTypeContent).getContent ()).getContents().iterator();

while(iter.hasNext()){

particleContent = ((XSDParticle)iter.next()).getContent();

if (particleContent instanceof XSDElementDeclaration){

//Here is our Person element reference (from included schema)
childElement = (XSDElementDeclaration)particleContent;

//The reference needs to be resolved
childElement = XSDSearch.getElementByReference(childElement);
}
}

//THERE IS NO TYPE DEFINITION FOR THIS ELEMENT? HOW TO GET ONE?
if (childElement.getTypeDefinition() == null)
System.out.println("No type definition for Person element which is included from Person Schema!");
}

public static void main(String[] args) {

new TestInclude2();

}

private XSDSchema loadSchemaUsingResourceSet(String schemaURL) throws Exception{

XSDSchema returnSchema = null;

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd", new XSDResourceFactoryImpl());

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION, Boolean.TRUE);

XSDResourceImpl xsdSchemaResource = (XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL), true);
xsdSchemaResource.load(null);

for (Iterator resources = resourceSet.getResources().iterator(); resources.hasNext(); /* no-op */){

Resource resource = (Resource)resources.next();

if (resource instanceof XSDResourceImpl){
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;

returnSchema = xsdResource.getSchema();
return returnSchema;
}
}

System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ") did not contain any schemas!");
return null;
}
}

--------------050703010505030806000701
Content-Type: text/x-java;
name="XSDSearch.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="XSDSearch.java"

/*
* Created on May 9, 2005
*/
package com.ccd.xsd.util.resolve.search;

import java.util.Iterator;

import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;

/**
* @author srdjan
*
* Klasa XSDSearch sadrzi metode koje pomazu u pronalazenju odredjenih segmenata XML Schema dokumenata
* (elemenata, atributa, definicija tipova i sl).
*/
public class XSDSearch {

/*
* Finding root element. If root element isn't finded the null is returned
*/
public static XSDElementDeclaration rootElementDeclarationFinder(XSDSchema schema, String localName){

Object tempXSDComponent = null;

Iterator rootElementsOfTheSchema = schema.getContents().iterator();

while(rootElementsOfTheSchema.hasNext()){

tempXSDComponent = rootElementsOfTheSchema.next();

//Ukoliko je u pitanju deklaracija elementa
if (tempXSDComponent instanceof XSDElementDeclaration)
//Ukoliko se ime posmatranog elementa poklapa sa imenom koje se trazi
if (((XSDElementDeclaration)tempXSDComponent).getName().equals( localName))
break;
}

return (XSDElementDeclaration)tempXSDComponent;
}

public static XSDElementDeclaration getElementByReference(XSDElementDeclaration elementDeclaration) {

XSDElementDeclaration targetElementDeclaration = elementDeclaration;

//Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
if (elementDeclaration.isElementDeclarationReference())
targetElementDeclaration = elementDeclaration.getResolvedElementDeclaration();

return targetElementDeclaration;
}

public static XSDAttributeDeclaration getAttributeByReference(XSDAttributeDeclaration attributeDeclaration) {

XSDAttributeDeclaration targetAttributeDeclaration = attributeDeclaration;

//Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
if (attributeDeclaration.isAttributeDeclarationReference())
targetAttributeDeclaration = attributeDeclaration.getResolvedAttributeDeclaration();

return targetAttributeDeclaration;
}

}

--------------050703010505030806000701--
Re: How to get elements from included schema? [message #61548 is a reply to message #61503] Fri, 10 June 2005 11:55 Go to previous messageGo to next message
Eclipse User
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------040709040906060805050600
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Sridjan,

I haven't run the test code, but I'm quite sure the problem is that you
are loading the root schema with a relative URI rather than an absolute
URI and relative schemaLocations can only be resolved against an
absolute URI. In the XSDMainTest you'll see code like this which
ensures that if a file path is passed that it is converted to a file:
URI with an absolute path:

URI uri;
File file = new File(xsdFile);
if (file.isFile())
{
uri = URI.createFileURI(file.getCanonicalFile().toString());
}
else
{
uri = URI.createURI(xsdFile);
}

I think you need to do the same.

The bottom line is that if the URI you use to load the schema is not
absolute, i.e., doesn't start with <scheme>:, relative includes and
imports won't resolve and won't work...


Srdjan Komazec wrote:

> Ed, thanks for fast replay again.
>
> Both schema instances were created with XMLSpy. XML Schema editor
> doesn't make any problem either.
>
> I'm sending these two schema files (person.xsd and class.xsd). I'm
> also sending code (TestInclude2.java) that is designed to show my
> problem (and XSDSearch class which is utility class).
>
> Hope to hear something soon.
> Best regards,
> Srdjan Komazec
>
>
> Ed Merks wrote:
>
>> Srdjan,
>>
>> As far as I know, the only way the type could be null is if the
>> element reference is an unresolved one, i.e., if
>> element.getContainer() == null. Have you validated the schema
>> (XSDMainTest in org.eclipse.xsd.test shows how and the test directory
>> there has scripts you can use for command line validation) or tried
>> opening the schema in the Sample XML Schema Editor to check for
>> problems? It sounds likely that your imports or includes aren't
>> resolving correctly.
>> My answers can be much more concrete if you show exactly what you are
>> doing (in terms of code) and what instances you are working with...
>>
>>
>> Srdjan Komazec wrote:
>>
>>>
>>> Thanks on fast replay.
>>>
>>> Unfortunatelly, that is not an issue because I have called
>>> getResolvedElementDeclaration() for all XSDElementDeclaration
>>> instances before any other method of the class to be sure that I
>>> have concrete element (this method returns the same instance if it
>>> is not element ref).
>>>
>>> I'm wondering if there is any special treatment for those
>>> elements that came from another schema (included by <xsd:include> or
>>> imported by <xsd:import>)?
>>>
>>> Thanks in adavnced!
>>>
>>>
>>> Ed Merks wrote:
>>>
>>>> Srdjan,
>>>>
>>>> Maybe you need to do
>>>> element.getResolvedElementDeclaration().getTypeDefinition() because
>>>> you have traversed to an element ref?
>>>>
>>>>
>>>> Srdjan Komazec wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I have two schemas (A and B) and schema A includes contents from
>>>>> the schema B via <xsd:include> property of XML Schema rec. It
>>>>> (schema A) also references some of the elements of the schema B.
>>>>> When I traverse schema A I'm not able to get any info about the
>>>>> type definition of the referenced elements, ie.
>>>>> element.getTypeDefinition() returns null.
>>>>>
>>>>> Is there any cure to this situation?
>>>>>
>>>>> Thanks for help,
>>>>> Srdjan Komazec
>>>>>
>
> ------------------------------------------------------------ ------------
>
><?xml version="1.0" encoding="UTF-8"?>
><xsd:schema xmlns:prob="http://ccd.ns.ac.yu/probaInclude" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ccd.ns.ac.yu/probaInclude" elementFormDefault="qualified" attributeFormDefault="unqualified">
>
> <xsd:include schemaLocation="person.xsd"/>
> <xsd:element name="class">
> <xsd:annotation>
> <xsd:documentation>Comment describing your root element</xsd:documentation>
> </xsd:annotation>
> <xsd:complexType>
> <xsd:sequence maxOccurs="unbounded">
> <xsd:element ref="prob:Person"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
></xsd:schema>
>
>
> ------------------------------------------------------------ ------------
>
><?xml version="1.0" encoding="UTF-8"?>
><xsd:schema xmlns:prob="http://ccd.ns.ac.yu/probaInclude" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ccd.ns.ac.yu/probaInclude" elementFormDefault="qualified" attributeFormDefault="unqualified">
>
> <xsd:element name="Person">
> <xsd:annotation>
> <xsd:documentation>Comment describing your root element</xsd:documentation>
> </xsd:annotation>
>
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="Name" type="xsd:string"/>
> <xsd:element name="Age" type="xsd:integer"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
>
></xsd:schema>
>
>
> ------------------------------------------------------------ ------------
>
>package com.ccd.xsd.test;
>
>import java.util.Iterator;
>
>import org.eclipse.emf.common.util.URI;
>import org.eclipse.emf.ecore.resource.Resource;
>import org.eclipse.emf.ecore.resource.ResourceSet;
>import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
>import org.eclipse.xsd.XSDComplexTypeContent;
>import org.eclipse.xsd.XSDComplexTypeDefinition;
>import org.eclipse.xsd.XSDElementDeclaration;
>import org.eclipse.xsd.XSDModelGroup;
>import org.eclipse.xsd.XSDParticle;
>import org.eclipse.xsd.XSDParticleContent;
>import org.eclipse.xsd.XSDSchema;
>import org.eclipse.xsd.util.XSDResourceFactoryImpl;
>import org.eclipse.xsd.util.XSDResourceImpl;
>
>import com.ccd.xsd.util.resolve.search.XSDSearch;
>
>/**
> * Class:TestInclude
> *
> * Date:Jun 8, 2005 11:00:05 AM
> *
> * Description:
> *
> * @author Srdjan Komazec FTN - Novi Sad <a href="mailto:ksrki@uns.ns.ac.yu">ksrki@uns.ns.ac.yu</a>
> * @version 1.0
> */
>public class TestInclude2 {
>
> public TestInclude2(){
>
> String schemaURL = "src/com/ccd/xsd/test/class.xsd";
> XSDSchema schema = null;
> Object component = null;
> XSDElementDeclaration rootElement = null, childElement = null;
> XSDComplexTypeDefinition complexTypeDefinition = null;
> XSDComplexTypeContent complexTypeContent = null;
> XSDParticleContent particleContent = null;
>
>
> //Loading the schema
> try {
>
> schema = loadSchemaUsingResourceSet(schemaURL);
>
> } catch (Exception e) {
> e.printStackTrace();
> }
>
> //Browsing the schema content and searching for Element Declarations
> Iterator iter = schema.getContents().iterator();
>
> while (iter.hasNext()){
>
> component = iter.next();
>
> if (component instanceof XSDElementDeclaration){
>
> //Found root element decalation
> rootElement = (XSDElementDeclaration)component;
> break;
> }
>
> }
>
> //Taking type definition for class element
> complexTypeDefinition = (XSDComplexTypeDefinition)rootElement.getTypeDefinition();
>
> //Content of this complex type definition
> complexTypeContent = complexTypeDefinition.getContent();
>
> //Because I know that class sequence of person element refrences comprise class element I have created an
> //iterator to what this sequence consists of (Person elemenet references)
> iter = ((XSDModelGroup)((XSDParticle)complexTypeContent).getContent ()).getContents().iterator();
>
> while(iter.hasNext()){
>
> particleContent = ((XSDParticle)iter.next()).getContent();
>
> if (particleContent instanceof XSDElementDeclaration){
>
> //Here is our Person element reference (from included schema)
> childElement = (XSDElementDeclaration)particleContent;
>
> //The reference needs to be resolved
> childElement = XSDSearch.getElementByReference(childElement);
> }
> }
>
> //THERE IS NO TYPE DEFINITION FOR THIS ELEMENT? HOW TO GET ONE?
> if (childElement.getTypeDefinition() == null)
> System.out.println("No type definition for Person element which is included from Person Schema!");
> }
>
> public static void main(String[] args) {
>
> new TestInclude2();
>
> }
>
> private XSDSchema loadSchemaUsingResourceSet(String schemaURL) throws Exception{
>
> XSDSchema returnSchema = null;
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd", new XSDResourceFactoryImpl());
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION, Boolean.TRUE);
>
> XSDResourceImpl xsdSchemaResource = (XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL), true);
> xsdSchemaResource.load(null);
>
> for (Iterator resources = resourceSet.getResources().iterator(); resources.hasNext(); /* no-op */){
>
> Resource resource = (Resource)resources.next();
>
> if (resource instanceof XSDResourceImpl){
> XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
>
> returnSchema = xsdResource.getSchema();
> return returnSchema;
> }
> }
>
> System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ") did not contain any schemas!");
> return null;
> }
>}
>
>
> ------------------------------------------------------------ ------------
>
>/*
> * Created on May 9, 2005
> */
>package com.ccd.xsd.util.resolve.search;
>
>import java.util.Iterator;
>
>import org.eclipse.xsd.XSDAttributeDeclaration;
>import org.eclipse.xsd.XSDElementDeclaration;
>import org.eclipse.xsd.XSDSchema;
>
>/**
> * @author srdjan
> *
> * Klasa XSDSearch sadrzi metode koje pomazu u pronalazenju odredjenih segmenata XML Schema dokumenata
> * (elemenata, atributa, definicija tipova i sl).
> */
>public class XSDSearch {
>
> /*
> * Finding root element. If root element isn't finded the null is returned
> */
> public static XSDElementDeclaration rootElementDeclarationFinder(XSDSchema schema, String localName){
>
> Object tempXSDComponent = null;
>
> Iterator rootElementsOfTheSchema = schema.getContents().iterator();
>
> while(rootElementsOfTheSchema.hasNext()){
>
> tempXSDComponent = rootElementsOfTheSchema.next();
>
> //Ukoliko je u pitanju deklaracija elementa
> if (tempXSDComponent instanceof XSDElementDeclaration)
> //Ukoliko se ime posmatranog elementa poklapa sa imenom koje se trazi
> if (((XSDElementDeclaration)tempXSDComponent).getName().equals( localName))
> break;
> }
>
> return (XSDElementDeclaration)tempXSDComponent;
> }
>
> public static XSDElementDeclaration getElementByReference(XSDElementDeclaration elementDeclaration) {
>
> XSDElementDeclaration targetElementDeclaration = elementDeclaration;
>
> //Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
> if (elementDeclaration.isElementDeclarationReference())
> targetElementDeclaration = elementDeclaration.getResolvedElementDeclaration();
>
> return targetElementDeclaration;
> }
>
> public static XSDAttributeDeclaration getAttributeByReference(XSDAttributeDeclaration attributeDeclaration) {
>
> XSDAttributeDeclaration targetAttributeDeclaration = attributeDeclaration;
>
> //Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
> if (attributeDeclaration.isAttributeDeclarationReference())
> targetAttributeDeclaration = attributeDeclaration.getResolvedAttributeDeclaration();
>
> return targetAttributeDeclaration;
> }
>
>}
>
>


--------------040709040906060805050600
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Sridjan,<br>
<br>
I haven't run the test code, but I'm quite sure the problem is that you
are loading the root schema with a relative URI rather than an absolute
URI and relative schemaLocations can only be resolved against an
absolute URI.  In the XSDMainTest you'll see code like this which
ensures that if a file path is passed that it is converted to a file:
URI with an absolute path:<br>
<blockquote>    URI uri;<br>
    File file = new File(xsdFile);<br>
    if (file.isFile())<br>
    {<br>
      uri = URI.createFileURI(file.getCanonicalFile().toString());<br>
    }<br>
    else<br>
    {<br>
      uri = URI.createURI(xsdFile);<br>
    }<br>
</blockquote>
I think you need to do the same.  <br>
<br>
The bottom line is that if the URI you use to load the schema is not
absolute, i.e., doesn't start with &lt;scheme&gt;:, relative includes
and imports won't resolve and won't work...<br>
<br>
<br>
Srdjan Komazec wrote:
<blockquote cite="midd8c75k$7c2$1@news.eclipse.org" type="cite">Ed,
thanks for fast replay again.
<br>
<br>
Both schema instances were created with XMLSpy. XML Schema editor
doesn't make any problem either.
<br>
<br>
I'm sending these two schema files (person.xsd and class.xsd). I'm also
sending code (TestInclude2.java) that is designed to show my problem
(and XSDSearch class which is utility class).
<br>
<br>
Hope to hear something soon.
<br>
Best regards,
<br>
Srdjan Komazec
<br>
<br>
<br>
Ed Merks wrote:
<br>
<blockquote type="cite">Srdjan,
<br>
<br>
As far as I know, the only way the type could be null is if the element
reference is an unresolved one, i.e., if element.getContainer() ==
null.  Have you validated the schema (XSDMainTest in
org.eclipse.xsd.test shows how and the test directory there has scripts
you can use for command line validation) or tried opening the schema in
the Sample XML Schema Editor to check for problems?  It sounds likely
that your imports or includes aren't resolving correctly.
<br>
My answers can be much more concrete if you show exactly what you are
doing (in terms of code) and what instances you are working with...
<br>
<br>
<br>
Srdjan Komazec wrote:
<br>
<br>
<blockquote type="cite"><br>
    Thanks on fast replay.
<br>
<br>
    Unfortunatelly, that is not an issue because I have called
getResolvedElementDeclaration() for all XSDElementDeclaration instances
before any other method of the class to be sure that I have concrete
element (this method returns the same instance if it is not element
ref).
<br>
<br>
    I'm wondering if there is any special treatment for those elements
that came from another schema (included by &lt;xsd:include&gt; or
imported by &lt;xsd:import&gt;)?
<br>
<br>
    Thanks in adavnced!
<br>
<br>
<br>
Ed Merks wrote:
<br>
<br>
<blockquote type="cite">Srdjan,
<br>
<br>
Maybe you need to do
element.getResolvedElementDeclaration().getTypeDefinition() because you
have traversed to an element ref?
<br>
<br>
<br>
Srdjan Komazec wrote:
<br>
<br>
<blockquote type="cite">Hi,
<br>
<br>
I have two schemas (A and B) and schema A includes contents from the
schema B via &lt;xsd:include&gt; property of XML Schema rec. It (schema
A) also references some of the elements of the schema B. When I
traverse schema A I'm not able to get any info about the type
definition of the referenced elements, ie. element.getTypeDefinition()
returns null.
<br>
<br>
Is there any cure to this situation?
<br>
<br>
Thanks for help,
<br>
Srdjan Komazec
<br>
<br>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<br>
<pre wrap="">
<hr size="4" width="90%">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsd:schema xmlns:prob=<a class="moz-txt-link-rfc2396E" href="http://ccd.ns.ac.yu/probaInclude">"http://ccd.ns.ac.yu/probaInclude"</a> xmlns:xsd=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema">"http://www.w3.org/2001/XMLSchema"</a> targetNamespace=<a class="moz-txt-link-rfc2396E" href="http://ccd.ns.ac.yu/probaInclude">"http://ccd.ns.ac.yu/probaInclude"</a> elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;

&lt;xsd:include schemaLocation="person.xsd"/&gt;
&lt;xsd:element name="class"&gt;
&lt;xsd:annotation&gt;
&lt;xsd:documentation&gt;Comment describing your root element&lt;/xsd:documentation&gt;
&lt;/xsd:annotation&gt;
&lt;xsd:complexType&gt;
&lt;xsd:sequence maxOccurs="unbounded"&gt;
&lt;xsd:element ref="prob:Person"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:element&gt;
&lt;/xsd:schema&gt;
</pre>
<pre wrap="">
<hr size="4" width="90%">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsd:schema xmlns:prob=<a class="moz-txt-link-rfc2396E" href="http://ccd.ns.ac.yu/probaInclude">"http://ccd.ns.ac.yu/probaInclude"</a> xmlns:xsd=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema">"http://www.w3.org/2001/XMLSchema"</a> targetNamespace=<a class="moz-txt-link-rfc2396E" href="http://ccd.ns.ac.yu/probaInclude">"http://ccd.ns.ac.yu/probaInclude"</a> elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;

&lt;xsd:element name="Person"&gt;
&lt;xsd:annotation&gt;
&lt;xsd:documentation&gt;Comment describing your root element&lt;/xsd:documentation&gt;
&lt;/xsd:annotation&gt;

&lt;xsd:complexType&gt;
&lt;xsd:sequence&gt;
&lt;xsd:element name="Name" type="xsd:string"/&gt;
&lt;xsd:element name="Age" type="xsd:integer"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:element&gt;

&lt;/xsd:schema&gt;
</pre>
<pre wrap="">
<hr size="4" width="90%">
package com.ccd.xsd.test;

import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;

import com.ccd.xsd.util.resolve.search.XSDSearch;

/**
* Class:TestInclude
*
* Date:Jun 8, 2005 11:00:05 AM
*
* Description:
*
* @author Srdjan Komazec FTN - Novi Sad &lt;a href=<a class="moz-txt-link-rfc2396E" href="mailto:ksrki@uns.ns.ac.yu">"mailto:ksrki@uns.ns.ac.yu"</a>&gt;<a class="moz-txt-link-abbreviated" href="mailto:ksrki@uns.ns.ac.yu">ksrki@uns.ns.ac.yu</a>&lt;/a&gt;
* @version 1.0
*/
public class TestInclude2 {

public TestInclude2(){

String schemaURL = "src/com/ccd/xsd/test/class.xsd";
XSDSchema schema = null;
Object component = null;
XSDElementDeclaration rootElement = null, childElement = null;
XSDComplexTypeDefinition complexTypeDefinition = null;
XSDComplexTypeContent complexTypeContent = null;
XSDParticleContent particleContent = null;


//Loading the schema
try {

schema = loadSchemaUsingResourceSet(schemaURL);

} catch (Exception e) {
e.printStackTrace();
}

//Browsing the schema content and searching for Element Declarations
Iterator iter = schema.getContents().iterator();

while (iter.hasNext()){

component = iter.next();

if (component instanceof XSDElementDeclaration){

//Found root element decalation
rootElement = (XSDElementDeclaration)component;
break;
}

}

//Taking type definition for class element
complexTypeDefinition = (XSDComplexTypeDefinition)rootElement.getTypeDefinition();

//Content of this complex type definition
complexTypeContent = complexTypeDefinition.getContent();

//Because I know that class sequence of person element refrences comprise class element I have created an
//iterator to what this sequence consists of (Person elemenet references)
iter = ((XSDModelGroup)((XSDParticle)complexTypeContent).getContent ()).getContents().iterator();

while(iter.hasNext()){

particleContent = ((XSDParticle)iter.next()).getContent();

if (particleContent instanceof XSDElementDeclaration){

//Here is our Person element reference (from included schema)
childElement = (XSDElementDeclaration)particleContent;

//The reference needs to be resolved
childElement = XSDSearch.getElementByReference(childElement);
}
}

//THERE IS NO TYPE DEFINITION FOR THIS ELEMENT? HOW TO GET ONE?
if (childElement.getTypeDefinition() == null)
System.out.println("No type definition for Person element which is included from Person Schema!");
}

public static void main(String[] args) {

new TestInclude2();

}

private XSDSchema loadSchemaUsingResourceSet(String schemaURL) throws Exception{

XSDSchema returnSchema = null;

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd", new XSDResourceFactoryImpl());

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION, Boolean.TRUE);

XSDResourceImpl xsdSchemaResource = (XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL), true);
xsdSchemaResource.load(null);

for (Iterator resources = resourceSet.getResources().iterator(); resources.hasNext(); /* no-op */){

Resource resource = (Resource)resources.next();

if (resource instanceof XSDResourceImpl){
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;

returnSchema = xsdResource.getSchema();
return returnSchema;
}
}

System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ") did not contain any schemas!");
return null;
}
}
</pre>
<pre wrap="">
<hr size="4" width="90%">
/*
* Created on May 9, 2005
*/
package com.ccd.xsd.util.resolve.search;

import java.util.Iterator;

import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;

/**
* @author srdjan
*
* Klasa XSDSearch sadrzi metode koje pomazu u pronalazenju odredjenih segmenata XML Schema dokumenata
* (elemenata, atributa, definicija tipova i sl).
*/
public class XSDSearch {

/*
* Finding root element. If root element isn't finded the null is returned
*/
public static XSDElementDeclaration rootElementDeclarationFinder(XSDSchema schema, String localName){

Object tempXSDComponent = null;

Iterator rootElementsOfTheSchema = schema.getContents().iterator();

while(rootElementsOfTheSchema.hasNext()){

tempXSDComponent = rootElementsOfTheSchema.next();

//Ukoliko je u pitanju deklaracija elementa
if (tempXSDComponent instanceof XSDElementDeclaration)
//Ukoliko se ime posmatranog elementa poklapa sa imenom koje se trazi
if (((XSDElementDeclaration)tempXSDComponent).getName().equals( localName))
break;
}

return (XSDElementDeclaration)tempXSDComponent;
}

public static XSDElementDeclaration getElementByReference(XSDElementDeclaration elementDeclaration) {

XSDElementDeclaration targetElementDeclaration = elementDeclaration;

//Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
if (elementDeclaration.isElementDeclarationReference())
targetElementDeclaration = elementDeclaration.getResolvedElementDeclaration();

return targetElementDeclaration;
}

public static XSDAttributeDeclaration getAttributeByReference(XSDAttributeDeclaration attributeDeclaration) {

XSDAttributeDeclaration targetAttributeDeclaration = attributeDeclaration;

//Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
if (attributeDeclaration.isAttributeDeclarationReference())
targetAttributeDeclaration = attributeDeclaration.getResolvedAttributeDeclaration();

return targetAttributeDeclaration;
}

}
</pre>
</blockquote>
<br>
</body>
</html>

--------------040709040906060805050600--
Re: How to get elements from included schema? [message #61565 is a reply to message #61548] Sun, 12 June 2005 08:40 Go to previous message
Eclipse User
Originally posted by: ksrki.uns.ns.ac.yu

That was the solution of this problem.

Thanks for the help.

Best regards,
Srdan K
Re: How to get elements from included schema? [message #595596 is a reply to message #61412] Fri, 10 June 2005 06:42 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26013
Registered: July 2009
Senior Member
Srdjan,

Maybe you need to do
element.getResolvedElementDeclaration().getTypeDefinition() because you
have traversed to an element ref?


Srdjan Komazec wrote:

> Hi,
>
> I have two schemas (A and B) and schema A includes contents from the
> schema B via <xsd:include> property of XML Schema rec. It (schema A)
> also references some of the elements of the schema B. When I traverse
> schema A I'm not able to get any info about the type definition of the
> referenced elements, ie. element.getTypeDefinition() returns null.
>
> Is there any cure to this situation?
>
> Thanks for help,
> Srdjan Komazec
>
Re: How to get elements from included schema? [message #595609 is a reply to message #61436] Fri, 10 June 2005 08:47 Go to previous message
Eclipse User
Originally posted by: ksrki.uns.ns.ac.yu

Thanks on fast replay.

Unfortunatelly, that is not an issue because I have called
getResolvedElementDeclaration() for all XSDElementDeclaration instances
before any other method of the class to be sure that I have concrete
element (this method returns the same instance if it is not element ref).

I'm wondering if there is any special treatment for those elements that
came from another schema (included by <xsd:include> or imported by
<xsd:import>)?

Thanks in adavnced!


Ed Merks wrote:
> Srdjan,
>
> Maybe you need to do
> element.getResolvedElementDeclaration().getTypeDefinition() because you
> have traversed to an element ref?
>
>
> Srdjan Komazec wrote:
>
>> Hi,
>>
>> I have two schemas (A and B) and schema A includes contents from the
>> schema B via <xsd:include> property of XML Schema rec. It (schema A)
>> also references some of the elements of the schema B. When I traverse
>> schema A I'm not able to get any info about the type definition of the
>> referenced elements, ie. element.getTypeDefinition() returns null.
>>
>> Is there any cure to this situation?
>>
>> Thanks for help,
>> Srdjan Komazec
>>
Re: How to get elements from included schema? [message #595614 is a reply to message #61457] Fri, 10 June 2005 09:31 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26013
Registered: July 2009
Senior Member
Srdjan,

As far as I know, the only way the type could be null is if the element
reference is an unresolved one, i.e., if element.getContainer() ==
null. Have you validated the schema (XSDMainTest in
org.eclipse.xsd.test shows how and the test directory there has scripts
you can use for command line validation) or tried opening the schema in
the Sample XML Schema Editor to check for problems? It sounds likely
that your imports or includes aren't resolving correctly.

My answers can be much more concrete if you show exactly what you are
doing (in terms of code) and what instances you are working with...


Srdjan Komazec wrote:

>
> Thanks on fast replay.
>
> Unfortunatelly, that is not an issue because I have called
> getResolvedElementDeclaration() for all XSDElementDeclaration
> instances before any other method of the class to be sure that I have
> concrete element (this method returns the same instance if it is not
> element ref).
>
> I'm wondering if there is any special treatment for those elements
> that came from another schema (included by <xsd:include> or imported
> by <xsd:import>)?
>
> Thanks in adavnced!
>
>
> Ed Merks wrote:
>
>> Srdjan,
>>
>> Maybe you need to do
>> element.getResolvedElementDeclaration().getTypeDefinition() because
>> you have traversed to an element ref?
>>
>>
>> Srdjan Komazec wrote:
>>
>>> Hi,
>>>
>>> I have two schemas (A and B) and schema A includes contents from the
>>> schema B via <xsd:include> property of XML Schema rec. It (schema A)
>>> also references some of the elements of the schema B. When I
>>> traverse schema A I'm not able to get any info about the type
>>> definition of the referenced elements, ie.
>>> element.getTypeDefinition() returns null.
>>>
>>> Is there any cure to this situation?
>>>
>>> Thanks for help,
>>> Srdjan Komazec
>>>
Re: How to get elements from included schema? [message #595623 is a reply to message #61480] Fri, 10 June 2005 10:13 Go to previous message
Eclipse User
Originally posted by: ksrki.uns.ns.ac.yu

This is a multi-part message in MIME format.
--------------050703010505030806000701
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Ed, thanks for fast replay again.

Both schema instances were created with XMLSpy. XML Schema editor
doesn't make any problem either.

I'm sending these two schema files (person.xsd and class.xsd). I'm also
sending code (TestInclude2.java) that is designed to show my problem
(and XSDSearch class which is utility class).

Hope to hear something soon.
Best regards,
Srdjan Komazec


Ed Merks wrote:
> Srdjan,
>
> As far as I know, the only way the type could be null is if the element
> reference is an unresolved one, i.e., if element.getContainer() ==
> null. Have you validated the schema (XSDMainTest in
> org.eclipse.xsd.test shows how and the test directory there has scripts
> you can use for command line validation) or tried opening the schema in
> the Sample XML Schema Editor to check for problems? It sounds likely
> that your imports or includes aren't resolving correctly.
> My answers can be much more concrete if you show exactly what you are
> doing (in terms of code) and what instances you are working with...
>
>
> Srdjan Komazec wrote:
>
>>
>> Thanks on fast replay.
>>
>> Unfortunatelly, that is not an issue because I have called
>> getResolvedElementDeclaration() for all XSDElementDeclaration
>> instances before any other method of the class to be sure that I have
>> concrete element (this method returns the same instance if it is not
>> element ref).
>>
>> I'm wondering if there is any special treatment for those elements
>> that came from another schema (included by <xsd:include> or imported
>> by <xsd:import>)?
>>
>> Thanks in adavnced!
>>
>>
>> Ed Merks wrote:
>>
>>> Srdjan,
>>>
>>> Maybe you need to do
>>> element.getResolvedElementDeclaration().getTypeDefinition() because
>>> you have traversed to an element ref?
>>>
>>>
>>> Srdjan Komazec wrote:
>>>
>>>> Hi,
>>>>
>>>> I have two schemas (A and B) and schema A includes contents from the
>>>> schema B via <xsd:include> property of XML Schema rec. It (schema A)
>>>> also references some of the elements of the schema B. When I
>>>> traverse schema A I'm not able to get any info about the type
>>>> definition of the referenced elements, ie.
>>>> element.getTypeDefinition() returns null.
>>>>
>>>> Is there any cure to this situation?
>>>>
>>>> Thanks for help,
>>>> Srdjan Komazec
>>>>


--------------050703010505030806000701
Content-Type: text/xml;
name="class.xsd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="class.xsd"

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:prob="http://ccd.ns.ac.yu/probaInclude" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ccd.ns.ac.yu/probaInclude" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xsd:include schemaLocation="person.xsd"/>
<xsd:element name="class">
<xsd:annotation>
<xsd:documentation>Comment describing your root element</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence maxOccurs="unbounded">
<xsd:element ref="prob:Person"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

--------------050703010505030806000701
Content-Type: text/xml;
name="person.xsd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="person.xsd"

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:prob="http://ccd.ns.ac.yu/probaInclude" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ccd.ns.ac.yu/probaInclude" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xsd:element name="Person">
<xsd:annotation>
<xsd:documentation>Comment describing your root element</xsd:documentation>
</xsd:annotation>

<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="Age" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>

--------------050703010505030806000701
Content-Type: text/x-java;
name="TestInclude2.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="TestInclude2.java"

package com.ccd.xsd.test;

import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;

import com.ccd.xsd.util.resolve.search.XSDSearch;

/**
* Class:TestInclude
*
* Date:Jun 8, 2005 11:00:05 AM
*
* Description:
*
* @author Srdjan Komazec FTN - Novi Sad <a href="mailto:ksrki@uns.ns.ac.yu">ksrki@uns.ns.ac.yu</a>
* @version 1.0
*/
public class TestInclude2 {

public TestInclude2(){

String schemaURL = "src/com/ccd/xsd/test/class.xsd";
XSDSchema schema = null;
Object component = null;
XSDElementDeclaration rootElement = null, childElement = null;
XSDComplexTypeDefinition complexTypeDefinition = null;
XSDComplexTypeContent complexTypeContent = null;
XSDParticleContent particleContent = null;


//Loading the schema
try {

schema = loadSchemaUsingResourceSet(schemaURL);

} catch (Exception e) {
e.printStackTrace();
}

//Browsing the schema content and searching for Element Declarations
Iterator iter = schema.getContents().iterator();

while (iter.hasNext()){

component = iter.next();

if (component instanceof XSDElementDeclaration){

//Found root element decalation
rootElement = (XSDElementDeclaration)component;
break;
}

}

//Taking type definition for class element
complexTypeDefinition = (XSDComplexTypeDefinition)rootElement.getTypeDefinition();

//Content of this complex type definition
complexTypeContent = complexTypeDefinition.getContent();

//Because I know that class sequence of person element refrences comprise class element I have created an
//iterator to what this sequence consists of (Person elemenet references)
iter = ((XSDModelGroup)((XSDParticle)complexTypeContent).getContent ()).getContents().iterator();

while(iter.hasNext()){

particleContent = ((XSDParticle)iter.next()).getContent();

if (particleContent instanceof XSDElementDeclaration){

//Here is our Person element reference (from included schema)
childElement = (XSDElementDeclaration)particleContent;

//The reference needs to be resolved
childElement = XSDSearch.getElementByReference(childElement);
}
}

//THERE IS NO TYPE DEFINITION FOR THIS ELEMENT? HOW TO GET ONE?
if (childElement.getTypeDefinition() == null)
System.out.println("No type definition for Person element which is included from Person Schema!");
}

public static void main(String[] args) {

new TestInclude2();

}

private XSDSchema loadSchemaUsingResourceSet(String schemaURL) throws Exception{

XSDSchema returnSchema = null;

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd", new XSDResourceFactoryImpl());

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION, Boolean.TRUE);

XSDResourceImpl xsdSchemaResource = (XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL), true);
xsdSchemaResource.load(null);

for (Iterator resources = resourceSet.getResources().iterator(); resources.hasNext(); /* no-op */){

Resource resource = (Resource)resources.next();

if (resource instanceof XSDResourceImpl){
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;

returnSchema = xsdResource.getSchema();
return returnSchema;
}
}

System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ") did not contain any schemas!");
return null;
}
}

--------------050703010505030806000701
Content-Type: text/x-java;
name="XSDSearch.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="XSDSearch.java"

/*
* Created on May 9, 2005
*/
package com.ccd.xsd.util.resolve.search;

import java.util.Iterator;

import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;

/**
* @author srdjan
*
* Klasa XSDSearch sadrzi metode koje pomazu u pronalazenju odredjenih segmenata XML Schema dokumenata
* (elemenata, atributa, definicija tipova i sl).
*/
public class XSDSearch {

/*
* Finding root element. If root element isn't finded the null is returned
*/
public static XSDElementDeclaration rootElementDeclarationFinder(XSDSchema schema, String localName){

Object tempXSDComponent = null;

Iterator rootElementsOfTheSchema = schema.getContents().iterator();

while(rootElementsOfTheSchema.hasNext()){

tempXSDComponent = rootElementsOfTheSchema.next();

//Ukoliko je u pitanju deklaracija elementa
if (tempXSDComponent instanceof XSDElementDeclaration)
//Ukoliko se ime posmatranog elementa poklapa sa imenom koje se trazi
if (((XSDElementDeclaration)tempXSDComponent).getName().equals( localName))
break;
}

return (XSDElementDeclaration)tempXSDComponent;
}

public static XSDElementDeclaration getElementByReference(XSDElementDeclaration elementDeclaration) {

XSDElementDeclaration targetElementDeclaration = elementDeclaration;

//Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
if (elementDeclaration.isElementDeclarationReference())
targetElementDeclaration = elementDeclaration.getResolvedElementDeclaration();

return targetElementDeclaration;
}

public static XSDAttributeDeclaration getAttributeByReference(XSDAttributeDeclaration attributeDeclaration) {

XSDAttributeDeclaration targetAttributeDeclaration = attributeDeclaration;

//Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
if (attributeDeclaration.isAttributeDeclarationReference())
targetAttributeDeclaration = attributeDeclaration.getResolvedAttributeDeclaration();

return targetAttributeDeclaration;
}

}

--------------050703010505030806000701--
Re: How to get elements from included schema? [message #595633 is a reply to message #61503] Fri, 10 June 2005 11:55 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26013
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------040709040906060805050600
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Sridjan,

I haven't run the test code, but I'm quite sure the problem is that you
are loading the root schema with a relative URI rather than an absolute
URI and relative schemaLocations can only be resolved against an
absolute URI. In the XSDMainTest you'll see code like this which
ensures that if a file path is passed that it is converted to a file:
URI with an absolute path:

URI uri;
File file = new File(xsdFile);
if (file.isFile())
{
uri = URI.createFileURI(file.getCanonicalFile().toString());
}
else
{
uri = URI.createURI(xsdFile);
}

I think you need to do the same.

The bottom line is that if the URI you use to load the schema is not
absolute, i.e., doesn't start with <scheme>:, relative includes and
imports won't resolve and won't work...


Srdjan Komazec wrote:

> Ed, thanks for fast replay again.
>
> Both schema instances were created with XMLSpy. XML Schema editor
> doesn't make any problem either.
>
> I'm sending these two schema files (person.xsd and class.xsd). I'm
> also sending code (TestInclude2.java) that is designed to show my
> problem (and XSDSearch class which is utility class).
>
> Hope to hear something soon.
> Best regards,
> Srdjan Komazec
>
>
> Ed Merks wrote:
>
>> Srdjan,
>>
>> As far as I know, the only way the type could be null is if the
>> element reference is an unresolved one, i.e., if
>> element.getContainer() == null. Have you validated the schema
>> (XSDMainTest in org.eclipse.xsd.test shows how and the test directory
>> there has scripts you can use for command line validation) or tried
>> opening the schema in the Sample XML Schema Editor to check for
>> problems? It sounds likely that your imports or includes aren't
>> resolving correctly.
>> My answers can be much more concrete if you show exactly what you are
>> doing (in terms of code) and what instances you are working with...
>>
>>
>> Srdjan Komazec wrote:
>>
>>>
>>> Thanks on fast replay.
>>>
>>> Unfortunatelly, that is not an issue because I have called
>>> getResolvedElementDeclaration() for all XSDElementDeclaration
>>> instances before any other method of the class to be sure that I
>>> have concrete element (this method returns the same instance if it
>>> is not element ref).
>>>
>>> I'm wondering if there is any special treatment for those
>>> elements that came from another schema (included by <xsd:include> or
>>> imported by <xsd:import>)?
>>>
>>> Thanks in adavnced!
>>>
>>>
>>> Ed Merks wrote:
>>>
>>>> Srdjan,
>>>>
>>>> Maybe you need to do
>>>> element.getResolvedElementDeclaration().getTypeDefinition() because
>>>> you have traversed to an element ref?
>>>>
>>>>
>>>> Srdjan Komazec wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I have two schemas (A and B) and schema A includes contents from
>>>>> the schema B via <xsd:include> property of XML Schema rec. It
>>>>> (schema A) also references some of the elements of the schema B.
>>>>> When I traverse schema A I'm not able to get any info about the
>>>>> type definition of the referenced elements, ie.
>>>>> element.getTypeDefinition() returns null.
>>>>>
>>>>> Is there any cure to this situation?
>>>>>
>>>>> Thanks for help,
>>>>> Srdjan Komazec
>>>>>
>
> ------------------------------------------------------------ ------------
>
><?xml version="1.0" encoding="UTF-8"?>
><xsd:schema xmlns:prob="http://ccd.ns.ac.yu/probaInclude" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ccd.ns.ac.yu/probaInclude" elementFormDefault="qualified" attributeFormDefault="unqualified">
>
> <xsd:include schemaLocation="person.xsd"/>
> <xsd:element name="class">
> <xsd:annotation>
> <xsd:documentation>Comment describing your root element</xsd:documentation>
> </xsd:annotation>
> <xsd:complexType>
> <xsd:sequence maxOccurs="unbounded">
> <xsd:element ref="prob:Person"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
></xsd:schema>
>
>
> ------------------------------------------------------------ ------------
>
><?xml version="1.0" encoding="UTF-8"?>
><xsd:schema xmlns:prob="http://ccd.ns.ac.yu/probaInclude" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ccd.ns.ac.yu/probaInclude" elementFormDefault="qualified" attributeFormDefault="unqualified">
>
> <xsd:element name="Person">
> <xsd:annotation>
> <xsd:documentation>Comment describing your root element</xsd:documentation>
> </xsd:annotation>
>
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="Name" type="xsd:string"/>
> <xsd:element name="Age" type="xsd:integer"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
>
></xsd:schema>
>
>
> ------------------------------------------------------------ ------------
>
>package com.ccd.xsd.test;
>
>import java.util.Iterator;
>
>import org.eclipse.emf.common.util.URI;
>import org.eclipse.emf.ecore.resource.Resource;
>import org.eclipse.emf.ecore.resource.ResourceSet;
>import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
>import org.eclipse.xsd.XSDComplexTypeContent;
>import org.eclipse.xsd.XSDComplexTypeDefinition;
>import org.eclipse.xsd.XSDElementDeclaration;
>import org.eclipse.xsd.XSDModelGroup;
>import org.eclipse.xsd.XSDParticle;
>import org.eclipse.xsd.XSDParticleContent;
>import org.eclipse.xsd.XSDSchema;
>import org.eclipse.xsd.util.XSDResourceFactoryImpl;
>import org.eclipse.xsd.util.XSDResourceImpl;
>
>import com.ccd.xsd.util.resolve.search.XSDSearch;
>
>/**
> * Class:TestInclude
> *
> * Date:Jun 8, 2005 11:00:05 AM
> *
> * Description:
> *
> * @author Srdjan Komazec FTN - Novi Sad <a href="mailto:ksrki@uns.ns.ac.yu">ksrki@uns.ns.ac.yu</a>
> * @version 1.0
> */
>public class TestInclude2 {
>
> public TestInclude2(){
>
> String schemaURL = "src/com/ccd/xsd/test/class.xsd";
> XSDSchema schema = null;
> Object component = null;
> XSDElementDeclaration rootElement = null, childElement = null;
> XSDComplexTypeDefinition complexTypeDefinition = null;
> XSDComplexTypeContent complexTypeContent = null;
> XSDParticleContent particleContent = null;
>
>
> //Loading the schema
> try {
>
> schema = loadSchemaUsingResourceSet(schemaURL);
>
> } catch (Exception e) {
> e.printStackTrace();
> }
>
> //Browsing the schema content and searching for Element Declarations
> Iterator iter = schema.getContents().iterator();
>
> while (iter.hasNext()){
>
> component = iter.next();
>
> if (component instanceof XSDElementDeclaration){
>
> //Found root element decalation
> rootElement = (XSDElementDeclaration)component;
> break;
> }
>
> }
>
> //Taking type definition for class element
> complexTypeDefinition = (XSDComplexTypeDefinition)rootElement.getTypeDefinition();
>
> //Content of this complex type definition
> complexTypeContent = complexTypeDefinition.getContent();
>
> //Because I know that class sequence of person element refrences comprise class element I have created an
> //iterator to what this sequence consists of (Person elemenet references)
> iter = ((XSDModelGroup)((XSDParticle)complexTypeContent).getContent ()).getContents().iterator();
>
> while(iter.hasNext()){
>
> particleContent = ((XSDParticle)iter.next()).getContent();
>
> if (particleContent instanceof XSDElementDeclaration){
>
> //Here is our Person element reference (from included schema)
> childElement = (XSDElementDeclaration)particleContent;
>
> //The reference needs to be resolved
> childElement = XSDSearch.getElementByReference(childElement);
> }
> }
>
> //THERE IS NO TYPE DEFINITION FOR THIS ELEMENT? HOW TO GET ONE?
> if (childElement.getTypeDefinition() == null)
> System.out.println("No type definition for Person element which is included from Person Schema!");
> }
>
> public static void main(String[] args) {
>
> new TestInclude2();
>
> }
>
> private XSDSchema loadSchemaUsingResourceSet(String schemaURL) throws Exception{
>
> XSDSchema returnSchema = null;
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd", new XSDResourceFactoryImpl());
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION, Boolean.TRUE);
>
> XSDResourceImpl xsdSchemaResource = (XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL), true);
> xsdSchemaResource.load(null);
>
> for (Iterator resources = resourceSet.getResources().iterator(); resources.hasNext(); /* no-op */){
>
> Resource resource = (Resource)resources.next();
>
> if (resource instanceof XSDResourceImpl){
> XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
>
> returnSchema = xsdResource.getSchema();
> return returnSchema;
> }
> }
>
> System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ") did not contain any schemas!");
> return null;
> }
>}
>
>
> ------------------------------------------------------------ ------------
>
>/*
> * Created on May 9, 2005
> */
>package com.ccd.xsd.util.resolve.search;
>
>import java.util.Iterator;
>
>import org.eclipse.xsd.XSDAttributeDeclaration;
>import org.eclipse.xsd.XSDElementDeclaration;
>import org.eclipse.xsd.XSDSchema;
>
>/**
> * @author srdjan
> *
> * Klasa XSDSearch sadrzi metode koje pomazu u pronalazenju odredjenih segmenata XML Schema dokumenata
> * (elemenata, atributa, definicija tipova i sl).
> */
>public class XSDSearch {
>
> /*
> * Finding root element. If root element isn't finded the null is returned
> */
> public static XSDElementDeclaration rootElementDeclarationFinder(XSDSchema schema, String localName){
>
> Object tempXSDComponent = null;
>
> Iterator rootElementsOfTheSchema = schema.getContents().iterator();
>
> while(rootElementsOfTheSchema.hasNext()){
>
> tempXSDComponent = rootElementsOfTheSchema.next();
>
> //Ukoliko je u pitanju deklaracija elementa
> if (tempXSDComponent instanceof XSDElementDeclaration)
> //Ukoliko se ime posmatranog elementa poklapa sa imenom koje se trazi
> if (((XSDElementDeclaration)tempXSDComponent).getName().equals( localName))
> break;
> }
>
> return (XSDElementDeclaration)tempXSDComponent;
> }
>
> public static XSDElementDeclaration getElementByReference(XSDElementDeclaration elementDeclaration) {
>
> XSDElementDeclaration targetElementDeclaration = elementDeclaration;
>
> //Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
> if (elementDeclaration.isElementDeclarationReference())
> targetElementDeclaration = elementDeclaration.getResolvedElementDeclaration();
>
> return targetElementDeclaration;
> }
>
> public static XSDAttributeDeclaration getAttributeByReference(XSDAttributeDeclaration attributeDeclaration) {
>
> XSDAttributeDeclaration targetAttributeDeclaration = attributeDeclaration;
>
> //Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
> if (attributeDeclaration.isAttributeDeclarationReference())
> targetAttributeDeclaration = attributeDeclaration.getResolvedAttributeDeclaration();
>
> return targetAttributeDeclaration;
> }
>
>}
>
>


--------------040709040906060805050600
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Sridjan,<br>
<br>
I haven't run the test code, but I'm quite sure the problem is that you
are loading the root schema with a relative URI rather than an absolute
URI and relative schemaLocations can only be resolved against an
absolute URI.  In the XSDMainTest you'll see code like this which
ensures that if a file path is passed that it is converted to a file:
URI with an absolute path:<br>
<blockquote>    URI uri;<br>
    File file = new File(xsdFile);<br>
    if (file.isFile())<br>
    {<br>
      uri = URI.createFileURI(file.getCanonicalFile().toString());<br>
    }<br>
    else<br>
    {<br>
      uri = URI.createURI(xsdFile);<br>
    }<br>
</blockquote>
I think you need to do the same.  <br>
<br>
The bottom line is that if the URI you use to load the schema is not
absolute, i.e., doesn't start with &lt;scheme&gt;:, relative includes
and imports won't resolve and won't work...<br>
<br>
<br>
Srdjan Komazec wrote:
<blockquote cite="midd8c75k$7c2$1@news.eclipse.org" type="cite">Ed,
thanks for fast replay again.
<br>
<br>
Both schema instances were created with XMLSpy. XML Schema editor
doesn't make any problem either.
<br>
<br>
I'm sending these two schema files (person.xsd and class.xsd). I'm also
sending code (TestInclude2.java) that is designed to show my problem
(and XSDSearch class which is utility class).
<br>
<br>
Hope to hear something soon.
<br>
Best regards,
<br>
Srdjan Komazec
<br>
<br>
<br>
Ed Merks wrote:
<br>
<blockquote type="cite">Srdjan,
<br>
<br>
As far as I know, the only way the type could be null is if the element
reference is an unresolved one, i.e., if element.getContainer() ==
null.  Have you validated the schema (XSDMainTest in
org.eclipse.xsd.test shows how and the test directory there has scripts
you can use for command line validation) or tried opening the schema in
the Sample XML Schema Editor to check for problems?  It sounds likely
that your imports or includes aren't resolving correctly.
<br>
My answers can be much more concrete if you show exactly what you are
doing (in terms of code) and what instances you are working with...
<br>
<br>
<br>
Srdjan Komazec wrote:
<br>
<br>
<blockquote type="cite"><br>
    Thanks on fast replay.
<br>
<br>
    Unfortunatelly, that is not an issue because I have called
getResolvedElementDeclaration() for all XSDElementDeclaration instances
before any other method of the class to be sure that I have concrete
element (this method returns the same instance if it is not element
ref).
<br>
<br>
    I'm wondering if there is any special treatment for those elements
that came from another schema (included by &lt;xsd:include&gt; or
imported by &lt;xsd:import&gt;)?
<br>
<br>
    Thanks in adavnced!
<br>
<br>
<br>
Ed Merks wrote:
<br>
<br>
<blockquote type="cite">Srdjan,
<br>
<br>
Maybe you need to do
element.getResolvedElementDeclaration().getTypeDefinition() because you
have traversed to an element ref?
<br>
<br>
<br>
Srdjan Komazec wrote:
<br>
<br>
<blockquote type="cite">Hi,
<br>
<br>
I have two schemas (A and B) and schema A includes contents from the
schema B via &lt;xsd:include&gt; property of XML Schema rec. It (schema
A) also references some of the elements of the schema B. When I
traverse schema A I'm not able to get any info about the type
definition of the referenced elements, ie. element.getTypeDefinition()
returns null.
<br>
<br>
Is there any cure to this situation?
<br>
<br>
Thanks for help,
<br>
Srdjan Komazec
<br>
<br>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<br>
<pre wrap="">
<hr size="4" width="90%">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsd:schema xmlns:prob=<a class="moz-txt-link-rfc2396E" href="http://ccd.ns.ac.yu/probaInclude">"http://ccd.ns.ac.yu/probaInclude"</a> xmlns:xsd=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema">"http://www.w3.org/2001/XMLSchema"</a> targetNamespace=<a class="moz-txt-link-rfc2396E" href="http://ccd.ns.ac.yu/probaInclude">"http://ccd.ns.ac.yu/probaInclude"</a> elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;

&lt;xsd:include schemaLocation="person.xsd"/&gt;
&lt;xsd:element name="class"&gt;
&lt;xsd:annotation&gt;
&lt;xsd:documentation&gt;Comment describing your root element&lt;/xsd:documentation&gt;
&lt;/xsd:annotation&gt;
&lt;xsd:complexType&gt;
&lt;xsd:sequence maxOccurs="unbounded"&gt;
&lt;xsd:element ref="prob:Person"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:element&gt;
&lt;/xsd:schema&gt;
</pre>
<pre wrap="">
<hr size="4" width="90%">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsd:schema xmlns:prob=<a class="moz-txt-link-rfc2396E" href="http://ccd.ns.ac.yu/probaInclude">"http://ccd.ns.ac.yu/probaInclude"</a> xmlns:xsd=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema">"http://www.w3.org/2001/XMLSchema"</a> targetNamespace=<a class="moz-txt-link-rfc2396E" href="http://ccd.ns.ac.yu/probaInclude">"http://ccd.ns.ac.yu/probaInclude"</a> elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;

&lt;xsd:element name="Person"&gt;
&lt;xsd:annotation&gt;
&lt;xsd:documentation&gt;Comment describing your root element&lt;/xsd:documentation&gt;
&lt;/xsd:annotation&gt;

&lt;xsd:complexType&gt;
&lt;xsd:sequence&gt;
&lt;xsd:element name="Name" type="xsd:string"/&gt;
&lt;xsd:element name="Age" type="xsd:integer"/&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;/xsd:element&gt;

&lt;/xsd:schema&gt;
</pre>
<pre wrap="">
<hr size="4" width="90%">
package com.ccd.xsd.test;

import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xsd.XSDComplexTypeContent;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceFactoryImpl;
import org.eclipse.xsd.util.XSDResourceImpl;

import com.ccd.xsd.util.resolve.search.XSDSearch;

/**
* Class:TestInclude
*
* Date:Jun 8, 2005 11:00:05 AM
*
* Description:
*
* @author Srdjan Komazec FTN - Novi Sad &lt;a href=<a class="moz-txt-link-rfc2396E" href="mailto:ksrki@uns.ns.ac.yu">"mailto:ksrki@uns.ns.ac.yu"</a>&gt;<a class="moz-txt-link-abbreviated" href="mailto:ksrki@uns.ns.ac.yu">ksrki@uns.ns.ac.yu</a>&lt;/a&gt;
* @version 1.0
*/
public class TestInclude2 {

public TestInclude2(){

String schemaURL = "src/com/ccd/xsd/test/class.xsd";
XSDSchema schema = null;
Object component = null;
XSDElementDeclaration rootElement = null, childElement = null;
XSDComplexTypeDefinition complexTypeDefinition = null;
XSDComplexTypeContent complexTypeContent = null;
XSDParticleContent particleContent = null;


//Loading the schema
try {

schema = loadSchemaUsingResourceSet(schemaURL);

} catch (Exception e) {
e.printStackTrace();
}

//Browsing the schema content and searching for Element Declarations
Iterator iter = schema.getContents().iterator();

while (iter.hasNext()){

component = iter.next();

if (component instanceof XSDElementDeclaration){

//Found root element decalation
rootElement = (XSDElementDeclaration)component;
break;
}

}

//Taking type definition for class element
complexTypeDefinition = (XSDComplexTypeDefinition)rootElement.getTypeDefinition();

//Content of this complex type definition
complexTypeContent = complexTypeDefinition.getContent();

//Because I know that class sequence of person element refrences comprise class element I have created an
//iterator to what this sequence consists of (Person elemenet references)
iter = ((XSDModelGroup)((XSDParticle)complexTypeContent).getContent ()).getContents().iterator();

while(iter.hasNext()){

particleContent = ((XSDParticle)iter.next()).getContent();

if (particleContent instanceof XSDElementDeclaration){

//Here is our Person element reference (from included schema)
childElement = (XSDElementDeclaration)particleContent;

//The reference needs to be resolved
childElement = XSDSearch.getElementByReference(childElement);
}
}

//THERE IS NO TYPE DEFINITION FOR THIS ELEMENT? HOW TO GET ONE?
if (childElement.getTypeDefinition() == null)
System.out.println("No type definition for Person element which is included from Person Schema!");
}

public static void main(String[] args) {

new TestInclude2();

}

private XSDSchema loadSchemaUsingResourceSet(String schemaURL) throws Exception{

XSDSchema returnSchema = null;

Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap( ).put( "xsd", new XSDResourceFactoryImpl());

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getLoadOptions().put(XSDResourceImpl.XSD_TRACK_L OCATION, Boolean.TRUE);

XSDResourceImpl xsdSchemaResource = (XSDResourceImpl)resourceSet.getResource(URI.createURI(schem aURL), true);
xsdSchemaResource.load(null);

for (Iterator resources = resourceSet.getResources().iterator(); resources.hasNext(); /* no-op */){

Resource resource = (Resource)resources.next();

if (resource instanceof XSDResourceImpl){
XSDResourceImpl xsdResource = (XSDResourceImpl)resource;

returnSchema = xsdResource.getSchema();
return returnSchema;
}
}

System.err.println("loadSchemaUsingResourceSet(" + schemaURL + ") did not contain any schemas!");
return null;
}
}
</pre>
<pre wrap="">
<hr size="4" width="90%">
/*
* Created on May 9, 2005
*/
package com.ccd.xsd.util.resolve.search;

import java.util.Iterator;

import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;

/**
* @author srdjan
*
* Klasa XSDSearch sadrzi metode koje pomazu u pronalazenju odredjenih segmenata XML Schema dokumenata
* (elemenata, atributa, definicija tipova i sl).
*/
public class XSDSearch {

/*
* Finding root element. If root element isn't finded the null is returned
*/
public static XSDElementDeclaration rootElementDeclarationFinder(XSDSchema schema, String localName){

Object tempXSDComponent = null;

Iterator rootElementsOfTheSchema = schema.getContents().iterator();

while(rootElementsOfTheSchema.hasNext()){

tempXSDComponent = rootElementsOfTheSchema.next();

//Ukoliko je u pitanju deklaracija elementa
if (tempXSDComponent instanceof XSDElementDeclaration)
//Ukoliko se ime posmatranog elementa poklapa sa imenom koje se trazi
if (((XSDElementDeclaration)tempXSDComponent).getName().equals( localName))
break;
}

return (XSDElementDeclaration)tempXSDComponent;
}

public static XSDElementDeclaration getElementByReference(XSDElementDeclaration elementDeclaration) {

XSDElementDeclaration targetElementDeclaration = elementDeclaration;

//Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
if (elementDeclaration.isElementDeclarationReference())
targetElementDeclaration = elementDeclaration.getResolvedElementDeclaration();

return targetElementDeclaration;
}

public static XSDAttributeDeclaration getAttributeByReference(XSDAttributeDeclaration attributeDeclaration) {

XSDAttributeDeclaration targetAttributeDeclaration = attributeDeclaration;

//Ako je u pitanju referenca tada treba da vratimo deklaraciju elementa na koju ona referise
if (attributeDeclaration.isAttributeDeclarationReference())
targetAttributeDeclaration = attributeDeclaration.getResolvedAttributeDeclaration();

return targetAttributeDeclaration;
}

}
</pre>
</blockquote>
<br>
</body>
</html>

--------------040709040906060805050600--
Re: How to get elements from included schema? [message #595647 is a reply to message #61548] Sun, 12 June 2005 08:40 Go to previous message
Eclipse User
Originally posted by: ksrki.uns.ns.ac.yu

That was the solution of this problem.

Thanks for the help.

Best regards,
Srdan K
Previous Topic:How to get elements from included schema?
Next Topic:Best way to extend XSD editor
Goto Forum:
  


Current Time: Fri Aug 29 08:19:48 EDT 2014

Powered by FUDForum. Page generated in 0.02915 seconds