Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » XML Schema Definition (XSD) » org.eclipse.xsd.XSDSchema
org.eclipse.xsd.XSDSchema [message #895762] Sun, 15 July 2012 20:10 Go to next message
Slawek L is currently offline Slawek LFriend
Messages: 21
Registered: May 2012
Junior Member
I have a xsd file:

<?xml version="1.0"?>
<xs:schema xmlns:xs="htp://www.w3.org/2001/XMLSchema">

<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

and parse it by org.eclipse.xsd.util.XSDParser to org.eclipse.xsd.XSDSchema and question is how find element to,from,heading and body inside XSDSchema? The only element that i found is note.
any advice which can help?
Re: org.eclipse.xsd.XSDSchema [message #895796 is a reply to message #895762] Mon, 16 July 2012 05:26 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
XSDElementDeclaration has getAnonymousTypeDefinition for this particular
case (a contained anonymous type), or, more generally,
getTypeDefinitition for the type of the element, regardless of whether
it's contained or referenced by name. Looking at how
XSDProtypicalSchema builds instances is likely to help you understand
how to traverse them. A generic way to walk everything is
Resource.getAllContents().


On 15/07/2012 10:11 PM, Slawek L wrote:
> I have a xsd file:
>
> <?xml version="1.0"?>
> <xs:schema xmlns:xs="htp://www.w3.org/2001/XMLSchema">
>
> <xs:element name="note">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="to" type="xs:string"/>
> <xs:element name="from" type="xs:string"/>
> <xs:element name="heading" type="xs:string"/>
> <xs:element name="body" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> </xs:schema>
>
> and parse it by org.eclipse.xsd.util.XSDParser to
> org.eclipse.xsd.XSDSchema and question is how find element
> to,from,heading and body inside XSDSchema? The only element that i
> found is note.
> any advice which can help?
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: org.eclipse.xsd.XSDSchema [message #896479 is a reply to message #895796] Wed, 18 July 2012 13:50 Go to previous messageGo to next message
Slawek L is currently offline Slawek LFriend
Messages: 21
Registered: May 2012
Junior Member
Hi, I have a simple code from XSDPrototypicalSchema and its clear :

XSDSchema xsdSchema=XSDSchema.class.newInstance();
    XSDComplexTypeDefinition usAddress = xsdFactory.createXSDComplexTypeDefinition();
    usAddress.setName("USAddress");
    
    XSDModelGroup usAddressSequence = xsdFactory.createXSDModelGroup();
    usAddressSequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
    XSDParticle usAddressParticle = xsdFactory.createXSDParticle();
    usAddressParticle.setContent(usAddressSequence);
    
    XSDElementDeclaration street = xsdFactory.createXSDElementDeclaration();
    street.setName("street");
    street.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
    XSDParticle streeParticle = xsdFactory.createXSDParticle();
    streeParticle.setContent(street);
    usAddressSequence.getContents().add(streeParticle);

    XSDElementDeclaration city = xsdFactory.createXSDElementDeclaration();
    city.setName("city");
    city.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
    XSDParticle cityParticle = xsdFactory.createXSDParticle();
    cityParticle.setContent(city);
    usAddressSequence.getContents().add(cityParticle);

    usAddress.setContent(usAddressParticle);

    XSDAttributeDeclaration country = xsdFactory.createXSDAttributeDeclaration();
    country.setName("country");
    country.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("NMTOKEN"));
    XSDAttributeUse countryAttributeUse = xsdFactory.createXSDAttributeUse();
    countryAttributeUse.setContent(country);
    countryAttributeUse.setConstraint(XSDConstraint.FIXED_LITERAL);
    countryAttributeUse.setLexicalValue("US");
    usAddress.getAttributeContents().add(countryAttributeUse);
    
    xsdSchema.getContents().add(usAddress);


input will be:

<xsd:complexType name="USAddress">
             <xsd:sequence>
                 <xsd:element name="name" type="xsd:string"/>
                 <xsd:element name="street" type="xsd:string"/>
                 <xsd:element name="city" type="xsd:string"/>
                 <xsd:element name="state" type="xsd:string"/>
                 <xsd:element name="zip" type="xsd:string"/>
             </xsd:sequence>
             <xsd:attribute fixed="US" name="country" type="xsd:NMTOKEN"/>
         </xsd:complexType>


but how from xsdSchema object get this simple structur? Almost all objects has inside Contents (EList) where can be everything(XSDComplexTypeDefinition, XSDParticial, XSDSequence ...). Could you provide more details about "generic way to walk everything", XSDSchema has a method eResource() but it gives null.
Re: org.eclipse.xsd.XSDSchema [message #896491 is a reply to message #896479] Wed, 18 July 2012 14:03 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Comments below.

On 18/07/2012 3:50 PM, Slawek L wrote:
> Hi, I have a simple code from XSDPrototypicalSchema and its clear :
>
> XSDSchema xsdSchema=XSDSchema.class.newInstance();
> XSDComplexTypeDefinition usAddress =
> xsdFactory.createXSDComplexTypeDefinition();
> usAddress.setName("USAddress");
> XSDModelGroup usAddressSequence = xsdFactory.createXSDModelGroup();
> usAddressSequence.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
> XSDParticle usAddressParticle = xsdFactory.createXSDParticle();
> usAddressParticle.setContent(usAddressSequence);
> XSDElementDeclaration street =
> xsdFactory.createXSDElementDeclaration();
> street.setName("street");
> street.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
> XSDParticle streeParticle = xsdFactory.createXSDParticle();
> streeParticle.setContent(street);
> usAddressSequence.getContents().add(streeParticle);
>
> XSDElementDeclaration city = xsdFactory.createXSDElementDeclaration();
> city.setName("city");
> city.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("string"));
> XSDParticle cityParticle = xsdFactory.createXSDParticle();
> cityParticle.setContent(city);
> usAddressSequence.getContents().add(cityParticle);
>
> usAddress.setContent(usAddressParticle);
>
> XSDAttributeDeclaration country =
> xsdFactory.createXSDAttributeDeclaration();
> country.setName("country");
> country.setTypeDefinition(xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("NMTOKEN"));
> XSDAttributeUse countryAttributeUse =
> xsdFactory.createXSDAttributeUse();
> countryAttributeUse.setContent(country);
> countryAttributeUse.setConstraint(XSDConstraint.FIXED_LITERAL);
> countryAttributeUse.setLexicalValue("US");
> usAddress.getAttributeContents().add(countryAttributeUse);
> xsdSchema.getContents().add(usAddress);
>
> input will be:
>
> <xsd:complexType name="USAddress">
> <xsd:sequence>
> <xsd:element name="name" type="xsd:string"/>
> <xsd:element name="street" type="xsd:string"/>
> <xsd:element name="city" type="xsd:string"/>
> <xsd:element name="state" type="xsd:string"/>
> <xsd:element name="zip" type="xsd:string"/>
> </xsd:sequence>
> <xsd:attribute fixed="US" name="country" type="xsd:NMTOKEN"/>
> </xsd:complexType>
>
> but how from xsdSchema object get this simple structur?
xsdSchema.getContents() contains it so you can iterate over that list.
> Almost all objects has inside Contents (EList) where can be
> everything(XSDComplexTypeDefinition, XSDParticial, XSDSequence ...).
Yes.
> Could you provide more details about "generic way to walk everything",
> XSDSchema has a method eResource() but it gives null.
EObject.eContents() generically returns all the contained EObjects.
EObject.eAllContents() is an iterator that (like
Resource.getAllContents(), will walk the entire containment tree.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: org.eclipse.xsd.XSDSchema [message #897954 is a reply to message #896491] Wed, 25 July 2012 13:51 Go to previous messageGo to next message
Slawek L is currently offline Slawek LFriend
Messages: 21
Registered: May 2012
Junior Member
How do I from XSDSchema receive org.eclipse.emf.common.util.URI? (org.eclipse.xsd.util.XSDPrototypicalSchema method printSchema(String xsdSchemaURI))
Re: org.eclipse.xsd.XSDSchema [message #898007 is a reply to message #897954] Wed, 25 July 2012 15:22 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
You can use things like an <a class="moz-txt-link-freetext" href="http://">http://</a>... URI pointing to a schema on
the web, or a <a class="moz-txt-link-freetext" href="file://">file://</a> URI pointing to something on your local file
system.  URI.createFileURI(URI.<span class="me1">createFileURI</span><span
class="br0">(&lt;java.util.File&gt;</span>.<span class="me1">getAbsolutePath</span><span
class="br0">(</span><span class="br0">)</span><span class="br0">)
is a good way to produce a URI for the file system.  If you're
wanting something in the workspace, be sure to read the EMF FAQ:<br>
</span><body>
<li class="toclevel-2"><a
href="http://wiki.eclipse.org/index.php/EMF-FAQ#How_do_I_map_between_an_EMF_Resource_and_an_Eclipse_IFile.3F"><span
class="tocnumber">2.44</span> <span class="toctext">How do
I map between an EMF Resource and an Eclipse IFile?</span></a></li>
</body>
<br>
<div class="moz-cite-prefix">On 25/07/2012 3:51 PM, Slawek L wrote:<br>
</div>
<blockquote cite="mid:juotkm$sua$1@xxxxxxxxe.org" type="cite">How
do I from XSDSchema receive org.eclipse.emf.common.util.URI?
(org.eclipse.xsd.util.XSDPrototypicalSchema method
printSchema(String xsdSchemaURI))
<br>
</blockquote>
<br>
</body>
</html>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: org.eclipse.xsd.XSDSchema [message #898442 is a reply to message #898007] Thu, 26 July 2012 11:50 Go to previous messageGo to next message
Slawek L is currently offline Slawek LFriend
Messages: 21
Registered: May 2012
Junior Member
Thanks Ed for help, now it's more understandable, but still I have some questions...
for example:

<xs:complexType name="company">
    <xs:sequence minOccurs="1" maxOccurs="1">
      <xs:element  name="name" type="xs:string"/>
      <xs:element  name="street" type="xs:string"/>
      <xs:element  name="postCode" type="xs:string"/>
      <xs:element  name="city" type="xs:string"/>
    </xs:sequence>
</xs:complexType>


It is
org.eclipse.xsd.XSDComplexTypeDefinition
object, and how do I check how many tags are inside?
best regards

[Updated on: Thu, 26 July 2012 11:51]

Report message to a moderator

Re: org.eclipse.xsd.XSDSchema [message #898451 is a reply to message #898442] Thu, 26 July 2012 12:02 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
What do you mean by "tags"?

On 26/07/2012 1:50 PM, Slawek L wrote:
> Thanks Ed for help, now it's more understandable, but still I have
> some questions...
> for example:
>
> <xs:complexType name="company">
> <xs:sequence minOccurs="1" maxOccurs="1">
> <xs:element name="name" type="xs:string"/>
> <xs:element name="street" type="xs:string"/>
> <xs:element name="postCode" type="xs:string"/>
> <xs:element name="city" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
>
> It is org.eclipse.xsd.XSDComplexTypeDefinition object, and how do I
> check how many tags are inside?
> best regards


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: org.eclipse.xsd.XSDSchema [message #898454 is a reply to message #898451] Thu, 26 July 2012 12:11 Go to previous messageGo to next message
Slawek L is currently offline Slawek LFriend
Messages: 21
Registered: May 2012
Junior Member
Sorry, I wasn't precise, in schema code we see that complexType "company" contains 4 elements: name, street, postCode, city. How do I check it in java and how do I check that city is a last element in company complexType.

[Updated on: Thu, 26 July 2012 12:13]

Report message to a moderator

Re: org.eclipse.xsd.XSDSchema [message #898462 is a reply to message #898454] Thu, 26 July 2012 12:20 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Probably you want to consider the logical content type because it will
take inheritance into account:


Open Declaration
<eclipse-open:%E2%98%82=org.eclipse.xsd/src%3Corg.eclipse.xsd%7BXSDComplexTypeDefinition.java%E2%98%83XSDComplexTypeDefinition%7EgetContentType>XSDComplexTypeContent
<eclipse-javadoc:%E2%98%82=org.eclipse.xsd/src%3Corg.eclipse.xsd%7BXSDComplexTypeDefinition.java%E2%98%83XSDComplexTypeDefinition%7EgetContentType%E2%98%82XSDComplexTypeContent>
org.eclipse.xsd.XSDComplexTypeDefinition
<eclipse-javadoc:%E2%98%82=org.eclipse.xsd/src%3Corg.eclipse.xsd%7BXSDComplexTypeDefinition.java%E2%98%83XSDComplexTypeDefinition>.getContentType()


Returns the value of the '/*Content Type*/' reference.

This represents the value of the content type
<http://www.w3.org/TR/xmlschema-1/#content_type> infoset property.
It is computed from the |content
<eclipse-javadoc:%E2%98%82=org.eclipse.xsd/src%3Corg.eclipse.xsd%7BXSDComplexTypeDefinition.java%E2%98%83XSDComplexTypeDefinition%7EgetContentType%E2%98%82%E2%98%82getContent%E2%98%82>|
and should typically not be modified directly. It will be |null|, an
|XSDSimpleTypeDefinition
<eclipse-javadoc:%E2%98%82=org.eclipse.xsd/src%3Corg.eclipse.xsd%7BXSDComplexTypeDefinition.java%E2%98%83XSDComplexTypeDefinition%7EgetContentType%E2%98%82XSDSimpleTypeDefinition>|,
or an |XSDParticle
<eclipse-javadoc:%E2%98%82=org.eclipse.xsd/src%3Corg.eclipse.xsd%7BXSDComplexTypeDefinition.java%E2%98%83XSDComplexTypeDefinition%7EgetContentType%E2%98%82XSDParticle>|.



On 26/07/2012 2:11 PM, Slawek L wrote:
> Sorry, I wasn't precise, in schema code we see that complexType
> "company" contains 4 elements: name, street, postCode, city. How do I
> check it in java?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: org.eclipse.xsd.XSDSchema [message #899577 is a reply to message #898462] Wed, 01 August 2012 13:49 Go to previous messageGo to next message
Slawek L is currently offline Slawek LFriend
Messages: 21
Registered: May 2012
Junior Member
xsd file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="htp://www.w3.org/2001/XMLSchema">
	<xsd:annotation>
		<xsd:documentation>Description .</xsd:documentation>
	</xsd:annotation>
	<xsd:element name="application">
		<xsd:complexType>
			<xsd:sequence>
				<xsd:element name="applicant">
					<xsd:complexType>
						<xsd:sequence>
							<xsd:element name="father_name">
								<xsd:simpleType>
									<xsd:restriction base="xsd:string">
										<xsd:maxLength value="50"/>
									</xsd:restriction>
								</xsd:simpleType>
							</xsd:element>
							<xsd:element name="birthday">
								<xsd:simpleType>
									<xsd:restriction base="xsd:date"/>
								</xsd:simpleType>
							</xsd:element>
						</xsd:sequence>
					</xsd:complexType>
				</xsd:element>
				<xsd:element name="date">
					<xsd:simpleType>
						<xsd:restriction base="xsd:date"/>
					</xsd:simpleType>
				</xsd:element>
			</xsd:sequence>
		</xsd:complexType>
	</xsd:element>
</xsd:schema>


Body of method
String fileName = "c:\\ParserXSDExample\\b\\4.xsd";
		XSDSchema xsdSchema = null;
		XSDParser parser = null;
		
		try {
			parser = XSDParser.class.newInstance();
			parser.parse(fileName);
			xsdSchema = parser.getSchema();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		int counter = 1;
		Document docu = xsdSchema.getDocument();
		TreeIterator<EObject> iterator = xsdSchema.eAllContents();
		while (iterator.hasNext()) {
			EObject object = (EObject) iterator.next();
			System.out.println(counter + "). element " + object.getClass());

			if (object instanceof XSDElementDeclaration) {
				XSDElementDeclaration xsdED=(XSDElementDeclaration) object;
				System.out.println(xsdED.getName());
			}
			counter++;
		}


and it produces:
application
applicant
father_name
birthday
date

And it is OK, but expected output should looks like:
application
  applicant
    father_name
    birthda
  applicant-Close
  date
application-Close

My problem is that I don't know how check that:
-element applicant is inside other element(application)(applicant has a parent)
-no more elements are inside applicant after birthda(birthday is last children)

Re: org.eclipse.xsd.XSDSchema [message #899677 is a reply to message #899577] Wed, 01 August 2012 18:41 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Comments below.

On 01/08/2012 3:50 PM, Slawek L wrote:
> xsd file:
>
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <xsd:schema xmlns:xsd="htp://www.w3.org/2001/XMLSchema">
> <xsd:annotation>
> <xsd:documentation>Description .</xsd:documentation>
> </xsd:annotation>
> <xsd:element name="application">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="applicant">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="father_name">
> <xsd:simpleType>
> <xsd:restriction base="xsd:string">
> <xsd:maxLength value="50"/>
> </xsd:restriction>
> </xsd:simpleType>
> </xsd:element>
> <xsd:element name="birthday">
> <xsd:simpleType>
> <xsd:restriction base="xsd:date"/>
> </xsd:simpleType>
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
> <xsd:element name="date">
> <xsd:simpleType>
> <xsd:restriction base="xsd:date"/>
> </xsd:simpleType>
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
> </xsd:schema>
>
>
> Body of method
> String fileName = "c:\\ParserXSDExample\\b\\4.xsd";
> XSDSchema xsdSchema = null;
> XSDParser parser = null;
>
> try {
> parser = XSDParser.class.newInstance();
> parser.parse(fileName);
There's no example that shows to do this. The examples show how to use
a resource set. Why are you doing this? It doesn't build a schema so I
don't see how it produces output like what you show.
> xsdSchema = parser.getSchema();
> } catch (InstantiationException e) {
> e.printStackTrace();
> } catch (IllegalAccessException e) {
> e.printStackTrace();
> }
>
> int counter = 1;
> Document docu = xsdSchema.getDocument();
> TreeIterator<EObject> iterator = xsdSchema.eAllContents();
> while (iterator.hasNext()) {
> EObject object = (EObject) iterator.next();
> System.out.println(counter + "). element " +
> object.getClass());
>
> if (object instanceof XSDElementDeclaration) {
> XSDElementDeclaration xsdED=(XSDElementDeclaration)
> object;
> System.out.println(xsdED.getName());
> }
> counter++;
> }
>
> and it produces:
>
> application
> applicant
> father_name
> birthday
> date
>
> And it is OK, but expected output should looks like:
>
> application
> applicant
> father_name
> birthda
> applicant-Close
> date
> application-Close
>
> My problem is that I don't know how check that:
> -element applicant is inside other element(application)(applicant has
> a parent)
> -no more elements are inside applicant after birthda(birthday is last
> children)
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:schema error
Next Topic:Adding XSD to Catalog which includes and imports XSD
Goto Forum:
  


Current Time: Thu Mar 28 20:41:57 GMT 2024

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

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

Back to the top