Home » Archived » XML Schema Definition (XSD) » Traversing a schema
Traversing a schema [message #50608] |
Thu, 19 August 2004 00:10 ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: gj.puredge.com
I'm sure this is a total newbie question but I'm still oretty new to the
Schema Infoset Model and am having a little trouble wrapping my brain around
it so I'm taking it one step at a time and hopefully don't annoy people too
much with these simple questions. I'm afraid this is a little long so
please bear with me. I promise to write a "XML Schema Infoset Model for
Idiots" article when this is all done to avoid this in the future :)
I mentioned in another post that I process all the element declarations in a
schema like this:
EList elements = schema.getSchema().getElementDeclarations()
for (Iterator iter = elements.iterator(); iter.hasNext(); )
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
// do something with the element
}
So if I come to something like this:
<xsd:element name="Start">
<xsd:complexType>
<xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The object "element" will contain the XSDElementDeclaration for the schema
element "Start". I can even see the name of this element (Start) using
element.getName();
Here's where things get a little hairy for guys like me who are just
starting out with the schema model. Could someone please let me know when I
go off the rails?
At this point I've got my "element" object which contains the schema element
named "Start". Now I know that a schema element can be either a simple or
complex type. If it is a simple type I don't need to go much further for
now. If it is a complex type I need to do some further processing to get
the contents.
I check if the element is a complexType:
XSDParticle particle = type.getComplexType();
If I get back null that means the element is a simple type. In this example
I get back a particle which tells me I have a complex type.
So I obtain the contents of the complex type:
XSDParticleContent content = particle.getContent();
To me this means I'm sitting right about here in the schema:
--> <xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
That's where I get lost. I know that I should be able to see the
<xsd:choice> element as some item in "content" but I do not know where to
find it.
Could anyone give me a few pointers?
Thanks
Gary
|
|
|
Re: Traversing a schema [message #50667 is a reply to message #50608] |
Thu, 19 August 2004 10:46 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: merks.ca.ibm.com
This is a multi-part message in MIME format.
--------------090009000505070705070706
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Gary,
Because XSD represents both the concrete syntax of XML Schema and the
abstract component model defined in the XML Schema spec, you can analyze
the particle structure either according to its syntax or according to
it's component structure. From a concrete point of view, a particle
has content of type XSDParticleContent, which can either be a model
group, a wildcard, an element declaration, or a model group definition.
If it's an element declaration or model group definition, you have to be
sure to get the resolved element declaration / resolved model group
definition to walk from the object representing the "ref" to the actual
object where it is declared/defined (and given the model group
definition, you need to walk to it's model group). From an abstract
point of view, a particle has a term, which is either a model group, a
wildcard or an element declaration, i.e., all the above walking has
already been done. This structure is recursive on model group which has
concrete contents and abstract particles, both of which represent more
particles. Something like this is probably what you want to do
public void visit(XSDParticle xsdParticle)
{
XSDTerm xsdTerm = xsdParticle.getTerm();
if (xsdTerm instanceof XSDModelGroup)
{
XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
for (Iterator i = xsdModelGroup.getParticles().iterator();
i.hasNext(); )
{
visit((XSDParticle)i.next());
}
}
else if (xsdTerm instanceof XSDWildcard)
{
XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
}
else
{
XSDElementDeclaration xsdElementDeclaration =
(XSDElementDeclaration)xsdTerm;
}
}
Gary J wrote:
>I'm sure this is a total newbie question but I'm still oretty new to the
>Schema Infoset Model and am having a little trouble wrapping my brain around
>it so I'm taking it one step at a time and hopefully don't annoy people too
>much with these simple questions. I'm afraid this is a little long so
>please bear with me. I promise to write a "XML Schema Infoset Model for
>Idiots" article when this is all done to avoid this in the future :)
>
>I mentioned in another post that I process all the element declarations in a
>schema like this:
>
>EList elements = schema.getSchema().getElementDeclarations()
>for (Iterator iter = elements.iterator(); iter.hasNext(); )
> {
> XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
> // do something with the element
> }
>
>So if I come to something like this:
>
> <xsd:element name="Start">
> <xsd:complexType>
> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
> </xsd:complexType>
> </xsd:element>
>
>The object "element" will contain the XSDElementDeclaration for the schema
>element "Start". I can even see the name of this element (Start) using
>element.getName();
>
>Here's where things get a little hairy for guys like me who are just
>starting out with the schema model. Could someone please let me know when I
>go off the rails?
>
>At this point I've got my "element" object which contains the schema element
>named "Start". Now I know that a schema element can be either a simple or
>complex type. If it is a simple type I don't need to go much further for
>now. If it is a complex type I need to do some further processing to get
>the contents.
>
>I check if the element is a complexType:
>
>XSDParticle particle = type.getComplexType();
>
>If I get back null that means the element is a simple type. In this example
>I get back a particle which tells me I have a complex type.
>
>So I obtain the contents of the complex type:
>
>XSDParticleContent content = particle.getContent();
>
>To me this means I'm sitting right about here in the schema:
>
>--> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
>
>That's where I get lost. I know that I should be able to see the
><xsd:choice> element as some item in "content" but I do not know where to
>find it.
>
>Could anyone give me a few pointers?
>
>Thanks
>
>Gary
>
>
>
>
--------------090009000505070705070706
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Gary,<br>
<br>
Because XSD represents both the concrete syntax of XML Schema and the
abstract component model defined in the XML Schema spec, you can
analyze the particle structure either according to its syntax or
according to it's component structure. From a concrete point of
view, a particle has content of type XSDParticleContent, which can
either be a model group, a wildcard, an element declaration, or a model
group definition. If it's an element declaration or model group
definition, you have to be sure to get the resolved element declaration
/ resolved model group definition to walk from the object representing
the "ref" to the actual object where it is declared/defined (and given
the model group definition, you need to walk to it's model group).
From an abstract point of view, a particle has a term, which is either
a model group, a wildcard or an element declaration, i.e., all the
above walking has already been done. This structure is recursive on
model group which has concrete contents and abstract particles, both of
which represent more particles. Something like this is probably what
you want to do<br>
<blockquote><small>public void visit(XSDParticle xsdParticle)<br>
{<br>
XSDTerm xsdTerm = xsdParticle.getTerm();<br>
if (xsdTerm instanceof XSDModelGroup)<br>
{<br>
XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;<br>
for (Iterator i = xsdModelGroup.getParticles().iterator();
i.hasNext(); )<br>
{<br>
visit((XSDParticle)i.next());<br>
}<br>
}<br>
else if (xsdTerm instanceof XSDWildcard)<br>
{<br>
XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;<br>
}<br>
else<br>
{<br>
XSDElementDeclaration xsdElementDeclaration =
(XSDElementDeclaration)xsdTerm;<br>
}<br>
}</small><br>
</blockquote>
Gary J wrote:<br>
<blockquote cite="midcg0r14$qu2$1@eclipse.org" type="cite">
<pre wrap="">I'm sure this is a total newbie question but I'm still oretty new to the
Schema Infoset Model and am having a little trouble wrapping my brain around
it so I'm taking it one step at a time and hopefully don't annoy people too
much with these simple questions. I'm afraid this is a little long so
please bear with me. I promise to write a "XML Schema Infoset Model for
Idiots" article when this is all done to avoid this in the future :)
I mentioned in another post that I process all the element declarations in a
schema like this:
EList elements = schema.getSchema().getElementDeclarations()
for (Iterator iter = elements.iterator(); iter.hasNext(); )
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
// do something with the element
}
So if I come to something like this:
<xsd:element name="Start">
<xsd:complexType>
<xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The object "element" will contain the XSDElementDeclaration for the schema
element "Start". I can even see the name of this element (Start) using
element.getName();
Here's where things get a little hairy for guys like me who are just
starting out with the schema model. Could someone please let me know when I
go off the rails?
At this point I've got my "element" object which contains the schema element
named "Start". Now I know that a schema element can be either a simple or
complex type. If it is a simple type I don't need to go much further for
now. If it is a complex type I need to do some further processing to get
the contents.
I check if the element is a complexType:
XSDParticle particle = type.getComplexType();
If I get back null that means the element is a simple type. In this example
I get back a particle which tells me I have a complex type.
So I obtain the contents of the complex type:
XSDParticleContent content = particle.getContent();
To me this means I'm sitting right about here in the schema:
--> <xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
That's where I get lost. I know that I should be able to see the
<xsd:choice> element as some item in "content" but I do not know where to
find it.
Could anyone give me a few pointers?
Thanks
Gary
</pre>
</blockquote>
<br>
</body>
</html>
--------------090009000505070705070706--
|
|
|
Re: Traversing a schema [message #50749 is a reply to message #50667] |
Thu, 19 August 2004 19:09 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: gj.puredge.com
I think I answered part of my own question. I was only checking Element
declarations, not types. I'm now checking both and get back a larger list
for "comments".
Is there any documentation about what I should be seeing in these lists?
Here's my code:
EList elements = schema.getSchema().getElementDeclarations();
EList types = schema.getSchema().getTypeDefinitions();
for (Iterator iter = elements.iterator(); iter.hasNext();)
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
System.out.println(" Element: " + element.getName());
ArrayList elist = (ArrayList)UsageCrossReferencer.find(element,
elements);
// list contents of element references
for (Iterator i = elist.iterator(); i.hasNext();)
{
Object obj = i.next();
System.out.println(" Element ref: " + obj.getClass());
}
ArrayList tlist = (ArrayList)UsageCrossReferencer.find(element, types);
// list contents of type references
for (Iterator i = tlist.iterator(); i.hasNext();)
{
Object obj = i.next();
System.out.println(" Type ref: " + obj.getClass());
}
}
Here's what I get:
For the "comment" element I get:
Element: comment
Element ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Element ref: class org.eclipse.emf.ecore.util.EObjectEList
Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
For the "purchaseOrder" element I get:
Element: purchaseOrder
Element ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Element ref: class org.eclipse.emf.ecore.util.EObjectEList
How do I decode all this?
Thanks
Gary
"Ed Merks" <merks@ca.ibm.com> wrote in message
news:cg20aa$hot$1@eclipse.org...
Gary,
Because XSD represents both the concrete syntax of XML Schema and the
abstract component model defined in the XML Schema spec, you can analyze the
particle structure either according to its syntax or according to it's
component structure. From a concrete point of view, a particle has
content of type XSDParticleContent, which can either be a model group, a
wildcard, an element declaration, or a model group definition. If it's an
element declaration or model group definition, you have to be sure to get
the resolved element declaration / resolved model group definition to walk
from the object representing the "ref" to the actual object where it is
declared/defined (and given the model group definition, you need to walk to
it's model group). From an abstract point of view, a particle has a term,
which is either a model group, a wildcard or an element declaration, i.e.,
all the above walking has already been done. This structure is recursive on
model group which has concrete contents and abstract particles, both of
which represent more particles. Something like this is probably what you
want to do
public void visit(XSDParticle xsdParticle)
{
XSDTerm xsdTerm = xsdParticle.getTerm();
if (xsdTerm instanceof XSDModelGroup)
{
XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
for (Iterator i = xsdModelGroup.getParticles().iterator();
i.hasNext(); )
{
visit((XSDParticle)i.next());
}
}
else if (xsdTerm instanceof XSDWildcard)
{
XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
}
else
{
XSDElementDeclaration xsdElementDeclaration =
(XSDElementDeclaration)xsdTerm;
}
}
Gary J wrote:
I'm sure this is a total newbie question but I'm still oretty new to the
Schema Infoset Model and am having a little trouble wrapping my brain around
it so I'm taking it one step at a time and hopefully don't annoy people too
much with these simple questions. I'm afraid this is a little long so
please bear with me. I promise to write a "XML Schema Infoset Model for
Idiots" article when this is all done to avoid this in the future :)
I mentioned in another post that I process all the element declarations in a
schema like this:
EList elements = schema.getSchema().getElementDeclarations()
for (Iterator iter = elements.iterator(); iter.hasNext(); )
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
// do something with the element
}
So if I come to something like this:
<xsd:element name="Start">
<xsd:complexType>
<xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The object "element" will contain the XSDElementDeclaration for the schema
element "Start". I can even see the name of this element (Start) using
element.getName();
Here's where things get a little hairy for guys like me who are just
starting out with the schema model. Could someone please let me know when I
go off the rails?
At this point I've got my "element" object which contains the schema element
named "Start". Now I know that a schema element can be either a simple or
complex type. If it is a simple type I don't need to go much further for
now. If it is a complex type I need to do some further processing to get
the contents.
I check if the element is a complexType:
XSDParticle particle = type.getComplexType();
If I get back null that means the element is a simple type. In this example
I get back a particle which tells me I have a complex type.
So I obtain the contents of the complex type:
XSDParticleContent content = particle.getContent();
To me this means I'm sitting right about here in the schema:
--> <xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
That's where I get lost. I know that I should be able to see the
<xsd:choice> element as some item in "content" but I do not know where to
find it.
Could anyone give me a few pointers?
Thanks
Gary
|
|
|
Re: Traversing a schema [message #50798 is a reply to message #50749] |
Thu, 19 August 2004 19:17 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: gj.puredge.com
Sorry, this was posted to the wrong thread.
"Gary J" <gj@puredge.com> wrote in message news:cg2tol$7ih$1@eclipse.org...
> I think I answered part of my own question. I was only checking Element
> declarations, not types. I'm now checking both and get back a larger list
> for "comments".
>
> Is there any documentation about what I should be seeing in these lists?
>
> Here's my code:
>
> EList elements = schema.getSchema().getElementDeclarations();
> EList types = schema.getSchema().getTypeDefinitions();
>
> for (Iterator iter = elements.iterator(); iter.hasNext();)
> {
> XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
> System.out.println(" Element: " + element.getName());
>
> ArrayList elist = (ArrayList)UsageCrossReferencer.find(element,
> elements);
>
> // list contents of element references
> for (Iterator i = e
list.iterator(); i.hasNext();)
> {
> Object obj = i.next();
> System.out.println(" Element ref: " + obj.getClass());
> }
>
> ArrayList tlist = (ArrayList)UsageCrossReferencer.find(element, types);
>
> // list contents of type references
> for (Iterator i = tlist.iterator(); i.hasNext();)
> {
> Object obj = i.next();
> System.out.println(" Type ref: " + obj.getClass());
> }
>
> }
>
> Here's what I get:
>
> For the "comment" element I get:
> Element: comment
> Element ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Element ref: class org.eclipse.emf.ecore.util.EObjectEList
>
> Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
>
> For the "purchaseOrder" element I get:
> Element: purchaseOrder
> Element ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Element ref: class org.eclipse.emf.ecore.util.EObjectEList
>
> How do I decode all this?
>
> Thanks
>
> Gary
>
>
>
>
> "Ed Merks" <merks@ca.ibm.com> wrote in message
> news:cg20aa$hot$1@eclipse.org...
> Gary,
>
> Because XSD represents both the concrete syntax of XML Schema and the
> abstract component model defined in the XML Schema spec, you can analyze
the
> particle structure either according to its syntax or according to it's
> component structure. From a concrete point of view, a particle has
> content of type XSDParticleContent, which can either be a model group, a
> wildcard, an element declaration, or a model group definition. If it's an
> element declaration or model group definition, you have to be sure to get
> the resolved element declaration / resolved model group definition to walk
> from the object representing the "ref" to the actual object where it is
> declared/defined (and given the model group definition, you need to walk
to
> it's model group). From an abstract point of view, a particle has a
term,
> which is either a model group, a wildcard or an element declaration, i.e.,
> all the above walking has already been done. This structure is recursive
on
> model group which has concrete contents and abstract particles, both of
> which represent more particles. Something like this is probably what you
> want to do
>
> public void visit(XSDParticle xsdParticle)
> {
> XSDTerm xsdTerm = xsdParticle.getTerm();
> if (xsdTerm instanceof XSDModelGroup)
> {
> XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
> for (Iterator i = xsdModelGroup.getParticles().iterator();
> i.hasNext(); )
> {
> visit((XSDParticle)i.next());
> }
> }
> else if (xsdTerm instanceof XSDWildcard)
> {
> XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
> }
> else
> {
> XSDElementDeclaration xsdElementDeclaration =
> (XSDElementDeclaration)xsdTerm;
> }
> }
>
> Gary J wrote:
>
> I'm sure this is a total newbie question but I'm still oretty new to the
> Schema Infoset Model and am having a little trouble wrapping my brain
around
> it so I'm taking it one step at a time and hopefully don't annoy people
too
> much with these simple questions. I'm afraid this is a little long so
> please bear with me. I promise to write a "XML Schema Infoset Model for
> Idiots" article when this is all done to avoid this in the future :)
>
> I mentioned in another post that I process all the element declarations in
a
> schema like this:
>
> EList elements = schema.getSchema().getElementDeclarations()
> for (Iterator iter = elements.iterator(); iter.hasNext(); )
> {
> XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
> // do something with the element
> }
>
> So if I come to something like this:
>
> <xsd:element name="Start">
> <xsd:complexType>
> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
> </xsd:complexType>
> </xsd:element>
>
> The object "element" will contain the XSDElementDeclaration for the schema
> element "Start". I can even see the name of this element (Start) using
> element.getName();
>
> Here's where things get a little hairy for guys like me who are just
> starting out with the schema model. Could someone please let me know when
I
> go off the rails?
>
> At this point I've got my "element" object which contains the schema
element
> named "Start". Now I know that a schema element can be either a simple or
> complex type. If it is a simple type I don't need to go much further for
> now. If it is a complex type I need to do some further processing to get
> the contents.
>
> I check if the element is a complexType:
>
> XSDParticle particle = type.getComplexType();
>
> If I get back null that means the element is a simple type. In this
example
> I get back a particle which tells me I have a complex type.
>
> So I obtain the contents of the complex type:
>
> XSDParticleContent content = particle.getContent();
>
> To me this means I'm sitting right about here in the schema:
>
> --> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
>
> That's where I get lost. I know that I should be able to see the
> <xsd:choice> element as some item in "content" but I do not know where to
> find it.
>
> Could anyone give me a few pointers?
>
> Thanks
>
> Gary
>
>
>
>
>
|
|
|
Re: Traversing a schema [message #51048 is a reply to message #50667] |
Fri, 20 August 2004 17:09 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: gj.puredge.com
Hi,
I'm just getting into this now. When I walk the schema I'm starting with
the Element Declarations and determining the type of each element with
element.getTypeDefinition() from which I will get the Particle if the type
is complex. I can then get the Term and process as you suggested.
I noticed that there are some situations when getTypeDefinition() returns
null. One case is when the element is a reference, ie: ref= . If I don't
get back a type, how can I traverse the elements if the referenced item is a
complex element?
Are there other situations when getTypeDefinition() returns a null?
I'm slowly chewing my way through Part 1 but its not easy going :)
Thanks
"Ed Merks" <merks@ca.ibm.com> wrote in message
news:cg20aa$hot$1@eclipse.org...
Gary,
Because XSD represents both the concrete syntax of XML Schema and the
abstract component model defined in the XML Schema spec, you can analyze the
particle structure either according to its syntax or according to it's
component structure. From a concrete point of view, a particle has
content of type XSDParticleContent, which can either be a model group, a
wildcard, an element declaration, or a model group definition. If it's an
element declaration or model group definition, you have to be sure to get
the resolved element declaration / resolved model group definition to walk
from the object representing the "ref" to the actual object where it is
declared/defined (and given the model group definition, you need to walk to
it's model group). From an abstract point of view, a particle has a term,
which is either a model group, a wildcard or an element declaration, i.e.,
all the above walking has already been done. This structure is recursive on
model group which has concrete contents and abstract particles, both of
which represent more particles. Something like this is probably what you
want to do
public void visit(XSDParticle xsdParticle)
{
XSDTerm xsdTerm = xsdParticle.getTerm();
if (xsdTerm instanceof XSDModelGroup)
{
XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
for (Iterator i = xsdModelGroup.getParticles().iterator();
i.hasNext(); )
{
visit((XSDParticle)i.next());
}
}
else if (xsdTerm instanceof XSDWildcard)
{
XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
}
else
{
XSDElementDeclaration xsdElementDeclaration =
(XSDElementDeclaration)xsdTerm;
}
}
Gary J wrote:
I'm sure this is a total newbie question but I'm still oretty new to the
Schema Infoset Model and am having a little trouble wrapping my brain around
it so I'm taking it one step at a time and hopefully don't annoy people too
much with these simple questions. I'm afraid this is a little long so
please bear with me. I promise to write a "XML Schema Infoset Model for
Idiots" article when this is all done to avoid this in the future :)
I mentioned in another post that I process all the element declarations in a
schema like this:
EList elements = schema.getSchema().getElementDeclarations()
for (Iterator iter = elements.iterator(); iter.hasNext(); )
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
// do something with the element
}
So if I come to something like this:
<xsd:element name="Start">
<xsd:complexType>
<xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The object "element" will contain the XSDElementDeclaration for the schema
element "Start". I can even see the name of this element (Start) using
element.getName();
Here's where things get a little hairy for guys like me who are just
starting out with the schema model. Could someone please let me know when I
go off the rails?
At this point I've got my "element" object which contains the schema element
named "Start". Now I know that a schema element can be either a simple or
complex type. If it is a simple type I don't need to go much further for
now. If it is a complex type I need to do some further processing to get
the contents.
I check if the element is a complexType:
XSDParticle particle = type.getComplexType();
If I get back null that means the element is a simple type. In this example
I get back a particle which tells me I have a complex type.
So I obtain the contents of the complex type:
XSDParticleContent content = particle.getContent();
To me this means I'm sitting right about here in the schema:
--> <xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
That's where I get lost. I know that I should be able to see the
<xsd:choice> element as some item in "content" but I do not know where to
find it.
Could anyone give me a few pointers?
Thanks
Gary
|
|
|
Re: Traversing a schema [message #51074 is a reply to message #51048] |
Fri, 20 August 2004 17:35 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: merks.ca.ibm.com
Gary,
If you have an XSDElementDeclaration x and
x.isElementDeclarationReference() is true, then you need to do
x.getResolvedElementDeclaration() to get the actual declaration rather
then the reference to it. If it's false, then
x.getResolvedElementDeclaration() == x, i.e., it resolves to itself.
Gary J wrote:
>Hi,
>
>I'm just getting into this now. When I walk the schema I'm starting with
>the Element Declarations and determining the type of each element with
>element.getTypeDefinition() from which I will get the Particle if the type
>is complex. I can then get the Term and process as you suggested.
>
>I noticed that there are some situations when getTypeDefinition() returns
>null. One case is when the element is a reference, ie: ref= . If I don't
>get back a type, how can I traverse the elements if the referenced item is a
>complex element?
>
>Are there other situations when getTypeDefinition() returns a null?
>
>I'm slowly chewing my way through Part 1 but its not easy going :)
>
>Thanks
>
>
>
>
>"Ed Merks" <merks@ca.ibm.com> wrote in message
>news:cg20aa$hot$1@eclipse.org...
>Gary,
>
>Because XSD represents both the concrete syntax of XML Schema and the
>abstract component model defined in the XML Schema spec, you can analyze the
>particle structure either according to its syntax or according to it's
>component structure. From a concrete point of view, a particle has
>content of type XSDParticleContent, which can either be a model group, a
>wildcard, an element declaration, or a model group definition. If it's an
>element declaration or model group definition, you have to be sure to get
>the resolved element declaration / resolved model group definition to walk
>from the object representing the "ref" to the actual object where it is
>declared/defined (and given the model group definition, you need to walk to
>it's model group). From an abstract point of view, a particle has a term,
>which is either a model group, a wildcard or an element declaration, i.e.,
>all the above walking has already been done. This structure is recursive on
>model group which has concrete contents and abstract particles, both of
>which represent more particles. Something like this is probably what you
>want to do
>
>public void visit(XSDParticle xsdParticle)
>{
> XSDTerm xsdTerm = xsdParticle.getTerm();
> if (xsdTerm instanceof XSDModelGroup)
> {
> XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
> for (Iterator i = xsdModelGroup.getParticles().iterator();
>i.hasNext(); )
> {
> visit((XSDParticle)i.next());
> }
> }
> else if (xsdTerm instanceof XSDWildcard)
> {
> XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
> }
> else
> {
> XSDElementDeclaration xsdElementDeclaration =
>(XSDElementDeclaration)xsdTerm;
> }
>}
>
>Gary J wrote:
>
>I'm sure this is a total newbie question but I'm still oretty new to the
>Schema Infoset Model and am having a little trouble wrapping my brain around
>it so I'm taking it one step at a time and hopefully don't annoy people too
>much with these simple questions. I'm afraid this is a little long so
>please bear with me. I promise to write a "XML Schema Infoset Model for
>Idiots" article when this is all done to avoid this in the future :)
>
>I mentioned in another post that I process all the element declarations in a
>schema like this:
>
>EList elements = schema.getSchema().getElementDeclarations()
>for (Iterator iter = elements.iterator(); iter.hasNext(); )
> {
> XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
> // do something with the element
> }
>
>So if I come to something like this:
>
> <xsd:element name="Start">
> <xsd:complexType>
> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
> </xsd:complexType>
> </xsd:element>
>
>The object "element" will contain the XSDElementDeclaration for the schema
>element "Start". I can even see the name of this element (Start) using
>element.getName();
>
>Here's where things get a little hairy for guys like me who are just
>starting out with the schema model. Could someone please let me know when I
>go off the rails?
>
>At this point I've got my "element" object which contains the schema element
>named "Start". Now I know that a schema element can be either a simple or
>complex type. If it is a simple type I don't need to go much further for
>now. If it is a complex type I need to do some further processing to get
>the contents.
>
>I check if the element is a complexType:
>
>XSDParticle particle = type.getComplexType();
>
>If I get back null that means the element is a simple type. In this example
>I get back a particle which tells me I have a complex type.
>
>So I obtain the contents of the complex type:
>
>XSDParticleContent content = particle.getContent();
>
>To me this means I'm sitting right about here in the schema:
>
>--> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
>
>That's where I get lost. I know that I should be able to see the
><xsd:choice> element as some item in "content" but I do not know where to
>find it.
>
>Could anyone give me a few pointers?
>
>Thanks
>
>Gary
>
>
>
>
>
>
>
|
|
|
Re: Traversing a schema [message #51102 is a reply to message #50667] |
Fri, 20 August 2004 18:57 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: gj.puredge.com
Ed,
Thanks for all the patient help.
I think I have it almost figured out. I'll step through what I am doing. My
goal is to print out a simplified schema (attributes ignored for now).
I started with a very simple schema:
<xsd:element name="test">
<xsd:complexType>
<xsd:choice>
<xsd:element ref="comment1" minOccurs="1" maxOccurs="1"></xsd:element>
<xsd:element ref="comment2" minOccurs="1" maxOccurs="1"></xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<xsd:element name="comment1" type="xsd:string"></xsd:element>
<xsd:element name="comment2" type="xsd:string"></xsd:element>
</xsd:schema>
Admittedly this is a trivial schema but it should suffice. Here's the
traversing sequence:
1. I start with schema.getElementDeclarations() and process each element in
the list:
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
2. I can print the element name
System.out.println(element.getName());
3. I determine that "test" is a complexType:
XSDTypeDefinition type = element.getTypeDefinition().
** Question: how do I determine this is an anonymous type? getName() returns
null?
4. Since this is a complexType I'll get the particle:
XSDParticle particle = type.getComplexType();
5. Get the term for this particle
XSDTerm term = particle.getTerm();
6. In this case term is an instance of XSDModelGroup
XSDModelGroup modelGroup = (XSDModelGroup)term;
7. I get the compositor and print it out
XSDCompositor c = modelGroup.getCompositor();
System.out.println(c.getName()); // "choice"
8. I process each particle in the modelGroup
EList list = modelGroup.getParticles();
8. back to 5
9. this time the term is an instance of XSDElementDeclaration
XSDElementDeclaration element = (XSDElementDeclaration)term;
10. back to 2
11. This time the element is a reference
element.isElementDeclarationReference() is true
element = element.getResolvedElementDeclaration();
13. Simple type, just print name of element
etc
This should give me:
<test>
<choice>
<comment1>
<comment2>
(without the brackets)
How does this look? Am I on the right track? There's a few gaps and I
haven't even considered things like groups but its only been a week or so
since I started this.
Thanks
|
|
|
Re: Traversing a schema [message #51159 is a reply to message #51102] |
Mon, 23 August 2004 11:04 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: merks.ca.ibm.com
Gary,
This looks good. Yes, an anonymous type will have a null name.
Gary J wrote:
>Ed,
>
>Thanks for all the patient help.
>I think I have it almost figured out. I'll step through what I am doing. My
>goal is to print out a simplified schema (attributes ignored for now).
>
>I started with a very simple schema:
>
> <xsd:element name="test">
> <xsd:complexType>
> <xsd:choice>
> <xsd:element ref="comment1" minOccurs="1" maxOccurs="1"></xsd:element>
> <xsd:element ref="comment2" minOccurs="1" maxOccurs="1"></xsd:element>
> </xsd:choice>
> </xsd:complexType>
> </xsd:element>
>
> <xsd:element name="comment1" type="xsd:string"></xsd:element>
> <xsd:element name="comment2" type="xsd:string"></xsd:element>
></xsd:schema>
>
>Admittedly this is a trivial schema but it should suffice. Here's the
>traversing sequence:
>
>1. I start with schema.getElementDeclarations() and process each element in
>the list:
>XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
>
>2. I can print the element name
>System.out.println(element.getName());
>
>3. I determine that "test" is a complexType:
>XSDTypeDefinition type = element.getTypeDefinition().
>** Question: how do I determine this is an anonymous type? getName() returns
>null?
>
>4. Since this is a complexType I'll get the particle:
>XSDParticle particle = type.getComplexType();
>
>5. Get the term for this particle
>XSDTerm term = particle.getTerm();
>
>6. In this case term is an instance of XSDModelGroup
>XSDModelGroup modelGroup = (XSDModelGroup)term;
>
>7. I get the compositor and print it out
>XSDCompositor c = modelGroup.getCompositor();
>System.out.println(c.getName()); // "choice"
>
>8. I process each particle in the modelGroup
>EList list = modelGroup.getParticles();
>
>8. back to 5
>
>9. this time the term is an instance of XSDElementDeclaration
>XSDElementDeclaration element = (XSDElementDeclaration)term;
>
>10. back to 2
>
>11. This time the element is a reference
>element.isElementDeclarationReference() is true
>element = element.getResolvedElementDeclaration();
>
>13. Simple type, just print name of element
>
>etc
>
>
>This should give me:
><test>
> <choice>
> <comment1>
> <comment2>
>(without the brackets)
>
>How does this look? Am I on the right track? There's a few gaps and I
>haven't even considered things like groups but its only been a week or so
>since I started this.
>
>Thanks
>
>
>
>
|
|
|
Re: Traversing a schema [message #590402 is a reply to message #50608] |
Thu, 19 August 2004 10:46 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Ed Merks![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=ed.merks%40gmail.com) Messages: 33224 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
--------------090009000505070705070706
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Gary,
Because XSD represents both the concrete syntax of XML Schema and the
abstract component model defined in the XML Schema spec, you can analyze
the particle structure either according to its syntax or according to
it's component structure. From a concrete point of view, a particle
has content of type XSDParticleContent, which can either be a model
group, a wildcard, an element declaration, or a model group definition.
If it's an element declaration or model group definition, you have to be
sure to get the resolved element declaration / resolved model group
definition to walk from the object representing the "ref" to the actual
object where it is declared/defined (and given the model group
definition, you need to walk to it's model group). From an abstract
point of view, a particle has a term, which is either a model group, a
wildcard or an element declaration, i.e., all the above walking has
already been done. This structure is recursive on model group which has
concrete contents and abstract particles, both of which represent more
particles. Something like this is probably what you want to do
public void visit(XSDParticle xsdParticle)
{
XSDTerm xsdTerm = xsdParticle.getTerm();
if (xsdTerm instanceof XSDModelGroup)
{
XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
for (Iterator i = xsdModelGroup.getParticles().iterator();
i.hasNext(); )
{
visit((XSDParticle)i.next());
}
}
else if (xsdTerm instanceof XSDWildcard)
{
XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
}
else
{
XSDElementDeclaration xsdElementDeclaration =
(XSDElementDeclaration)xsdTerm;
}
}
Gary J wrote:
>I'm sure this is a total newbie question but I'm still oretty new to the
>Schema Infoset Model and am having a little trouble wrapping my brain around
>it so I'm taking it one step at a time and hopefully don't annoy people too
>much with these simple questions. I'm afraid this is a little long so
>please bear with me. I promise to write a "XML Schema Infoset Model for
>Idiots" article when this is all done to avoid this in the future :)
>
>I mentioned in another post that I process all the element declarations in a
>schema like this:
>
>EList elements = schema.getSchema().getElementDeclarations()
>for (Iterator iter = elements.iterator(); iter.hasNext(); )
> {
> XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
> // do something with the element
> }
>
>So if I come to something like this:
>
> <xsd:element name="Start">
> <xsd:complexType>
> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
> </xsd:complexType>
> </xsd:element>
>
>The object "element" will contain the XSDElementDeclaration for the schema
>element "Start". I can even see the name of this element (Start) using
>element.getName();
>
>Here's where things get a little hairy for guys like me who are just
>starting out with the schema model. Could someone please let me know when I
>go off the rails?
>
>At this point I've got my "element" object which contains the schema element
>named "Start". Now I know that a schema element can be either a simple or
>complex type. If it is a simple type I don't need to go much further for
>now. If it is a complex type I need to do some further processing to get
>the contents.
>
>I check if the element is a complexType:
>
>XSDParticle particle = type.getComplexType();
>
>If I get back null that means the element is a simple type. In this example
>I get back a particle which tells me I have a complex type.
>
>So I obtain the contents of the complex type:
>
>XSDParticleContent content = particle.getContent();
>
>To me this means I'm sitting right about here in the schema:
>
>--> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
>
>That's where I get lost. I know that I should be able to see the
><xsd:choice> element as some item in "content" but I do not know where to
>find it.
>
>Could anyone give me a few pointers?
>
>Thanks
>
>Gary
>
>
>
>
--------------090009000505070705070706
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Gary,<br>
<br>
Because XSD represents both the concrete syntax of XML Schema and the
abstract component model defined in the XML Schema spec, you can
analyze the particle structure either according to its syntax or
according to it's component structure. From a concrete point of
view, a particle has content of type XSDParticleContent, which can
either be a model group, a wildcard, an element declaration, or a model
group definition. If it's an element declaration or model group
definition, you have to be sure to get the resolved element declaration
/ resolved model group definition to walk from the object representing
the "ref" to the actual object where it is declared/defined (and given
the model group definition, you need to walk to it's model group).
From an abstract point of view, a particle has a term, which is either
a model group, a wildcard or an element declaration, i.e., all the
above walking has already been done. This structure is recursive on
model group which has concrete contents and abstract particles, both of
which represent more particles. Something like this is probably what
you want to do<br>
<blockquote><small>public void visit(XSDParticle xsdParticle)<br>
{<br>
XSDTerm xsdTerm = xsdParticle.getTerm();<br>
if (xsdTerm instanceof XSDModelGroup)<br>
{<br>
XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;<br>
for (Iterator i = xsdModelGroup.getParticles().iterator();
i.hasNext(); )<br>
{<br>
visit((XSDParticle)i.next());<br>
}<br>
}<br>
else if (xsdTerm instanceof XSDWildcard)<br>
{<br>
XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;<br>
}<br>
else<br>
{<br>
XSDElementDeclaration xsdElementDeclaration =
(XSDElementDeclaration)xsdTerm;<br>
}<br>
}</small><br>
</blockquote>
Gary J wrote:<br>
<blockquote cite="midcg0r14$qu2$1@eclipse.org" type="cite">
<pre wrap="">I'm sure this is a total newbie question but I'm still oretty new to the
Schema Infoset Model and am having a little trouble wrapping my brain around
it so I'm taking it one step at a time and hopefully don't annoy people too
much with these simple questions. I'm afraid this is a little long so
please bear with me. I promise to write a "XML Schema Infoset Model for
Idiots" article when this is all done to avoid this in the future :)
I mentioned in another post that I process all the element declarations in a
schema like this:
EList elements = schema.getSchema().getElementDeclarations()
for (Iterator iter = elements.iterator(); iter.hasNext(); )
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
// do something with the element
}
So if I come to something like this:
<xsd:element name="Start">
<xsd:complexType>
<xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The object "element" will contain the XSDElementDeclaration for the schema
element "Start". I can even see the name of this element (Start) using
element.getName();
Here's where things get a little hairy for guys like me who are just
starting out with the schema model. Could someone please let me know when I
go off the rails?
At this point I've got my "element" object which contains the schema element
named "Start". Now I know that a schema element can be either a simple or
complex type. If it is a simple type I don't need to go much further for
now. If it is a complex type I need to do some further processing to get
the contents.
I check if the element is a complexType:
XSDParticle particle = type.getComplexType();
If I get back null that means the element is a simple type. In this example
I get back a particle which tells me I have a complex type.
So I obtain the contents of the complex type:
XSDParticleContent content = particle.getContent();
To me this means I'm sitting right about here in the schema:
--> <xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
That's where I get lost. I know that I should be able to see the
<xsd:choice> element as some item in "content" but I do not know where to
find it.
Could anyone give me a few pointers?
Thanks
Gary
</pre>
</blockquote>
<br>
</body>
</html>
--------------090009000505070705070706--
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: Traversing a schema [message #590444 is a reply to message #50667] |
Thu, 19 August 2004 19:09 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Gary J![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=gvj%40accu.ca) Messages: 61 Registered: July 2009 |
Member |
|
|
I think I answered part of my own question. I was only checking Element
declarations, not types. I'm now checking both and get back a larger list
for "comments".
Is there any documentation about what I should be seeing in these lists?
Here's my code:
EList elements = schema.getSchema().getElementDeclarations();
EList types = schema.getSchema().getTypeDefinitions();
for (Iterator iter = elements.iterator(); iter.hasNext();)
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
System.out.println(" Element: " + element.getName());
ArrayList elist = (ArrayList)UsageCrossReferencer.find(element,
elements);
// list contents of element references
for (Iterator i = elist.iterator(); i.hasNext();)
{
Object obj = i.next();
System.out.println(" Element ref: " + obj.getClass());
}
ArrayList tlist = (ArrayList)UsageCrossReferencer.find(element, types);
// list contents of type references
for (Iterator i = tlist.iterator(); i.hasNext();)
{
Object obj = i.next();
System.out.println(" Type ref: " + obj.getClass());
}
}
Here's what I get:
For the "comment" element I get:
Element: comment
Element ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Element ref: class org.eclipse.emf.ecore.util.EObjectEList
Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
For the "purchaseOrder" element I get:
Element: purchaseOrder
Element ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
Element ref: class org.eclipse.emf.ecore.util.EObjectEList
How do I decode all this?
Thanks
Gary
"Ed Merks" <merks@ca.ibm.com> wrote in message
news:cg20aa$hot$1@eclipse.org...
Gary,
Because XSD represents both the concrete syntax of XML Schema and the
abstract component model defined in the XML Schema spec, you can analyze the
particle structure either according to its syntax or according to it's
component structure. From a concrete point of view, a particle has
content of type XSDParticleContent, which can either be a model group, a
wildcard, an element declaration, or a model group definition. If it's an
element declaration or model group definition, you have to be sure to get
the resolved element declaration / resolved model group definition to walk
from the object representing the "ref" to the actual object where it is
declared/defined (and given the model group definition, you need to walk to
it's model group). From an abstract point of view, a particle has a term,
which is either a model group, a wildcard or an element declaration, i.e.,
all the above walking has already been done. This structure is recursive on
model group which has concrete contents and abstract particles, both of
which represent more particles. Something like this is probably what you
want to do
public void visit(XSDParticle xsdParticle)
{
XSDTerm xsdTerm = xsdParticle.getTerm();
if (xsdTerm instanceof XSDModelGroup)
{
XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
for (Iterator i = xsdModelGroup.getParticles().iterator();
i.hasNext(); )
{
visit((XSDParticle)i.next());
}
}
else if (xsdTerm instanceof XSDWildcard)
{
XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
}
else
{
XSDElementDeclaration xsdElementDeclaration =
(XSDElementDeclaration)xsdTerm;
}
}
Gary J wrote:
I'm sure this is a total newbie question but I'm still oretty new to the
Schema Infoset Model and am having a little trouble wrapping my brain around
it so I'm taking it one step at a time and hopefully don't annoy people too
much with these simple questions. I'm afraid this is a little long so
please bear with me. I promise to write a "XML Schema Infoset Model for
Idiots" article when this is all done to avoid this in the future :)
I mentioned in another post that I process all the element declarations in a
schema like this:
EList elements = schema.getSchema().getElementDeclarations()
for (Iterator iter = elements.iterator(); iter.hasNext(); )
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
// do something with the element
}
So if I come to something like this:
<xsd:element name="Start">
<xsd:complexType>
<xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The object "element" will contain the XSDElementDeclaration for the schema
element "Start". I can even see the name of this element (Start) using
element.getName();
Here's where things get a little hairy for guys like me who are just
starting out with the schema model. Could someone please let me know when I
go off the rails?
At this point I've got my "element" object which contains the schema element
named "Start". Now I know that a schema element can be either a simple or
complex type. If it is a simple type I don't need to go much further for
now. If it is a complex type I need to do some further processing to get
the contents.
I check if the element is a complexType:
XSDParticle particle = type.getComplexType();
If I get back null that means the element is a simple type. In this example
I get back a particle which tells me I have a complex type.
So I obtain the contents of the complex type:
XSDParticleContent content = particle.getContent();
To me this means I'm sitting right about here in the schema:
--> <xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
That's where I get lost. I know that I should be able to see the
<xsd:choice> element as some item in "content" but I do not know where to
find it.
Could anyone give me a few pointers?
Thanks
Gary
|
|
|
Re: Traversing a schema [message #590466 is a reply to message #50749] |
Thu, 19 August 2004 19:17 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Gary J![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=gvj%40accu.ca) Messages: 61 Registered: July 2009 |
Member |
|
|
Sorry, this was posted to the wrong thread.
"Gary J" <gj@puredge.com> wrote in message news:cg2tol$7ih$1@eclipse.org...
> I think I answered part of my own question. I was only checking Element
> declarations, not types. I'm now checking both and get back a larger list
> for "comments".
>
> Is there any documentation about what I should be seeing in these lists?
>
> Here's my code:
>
> EList elements = schema.getSchema().getElementDeclarations();
> EList types = schema.getSchema().getTypeDefinitions();
>
> for (Iterator iter = elements.iterator(); iter.hasNext();)
> {
> XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
> System.out.println(" Element: " + element.getName());
>
> ArrayList elist = (ArrayList)UsageCrossReferencer.find(element,
> elements);
>
> // list contents of element references
> for (Iterator i = e
list.iterator(); i.hasNext();)
> {
> Object obj = i.next();
> System.out.println(" Element ref: " + obj.getClass());
> }
>
> ArrayList tlist = (ArrayList)UsageCrossReferencer.find(element, types);
>
> // list contents of type references
> for (Iterator i = tlist.iterator(); i.hasNext();)
> {
> Object obj = i.next();
> System.out.println(" Type ref: " + obj.getClass());
> }
>
> }
>
> Here's what I get:
>
> For the "comment" element I get:
> Element: comment
> Element ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Element ref: class org.eclipse.emf.ecore.util.EObjectEList
>
> Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Type ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
>
> For the "purchaseOrder" element I get:
> Element: purchaseOrder
> Element ref: class org.eclipse.emf.ecore.impl.BasicEObjectImpl$2
> Element ref: class org.eclipse.emf.ecore.util.EObjectEList
>
> How do I decode all this?
>
> Thanks
>
> Gary
>
>
>
>
> "Ed Merks" <merks@ca.ibm.com> wrote in message
> news:cg20aa$hot$1@eclipse.org...
> Gary,
>
> Because XSD represents both the concrete syntax of XML Schema and the
> abstract component model defined in the XML Schema spec, you can analyze
the
> particle structure either according to its syntax or according to it's
> component structure. From a concrete point of view, a particle has
> content of type XSDParticleContent, which can either be a model group, a
> wildcard, an element declaration, or a model group definition. If it's an
> element declaration or model group definition, you have to be sure to get
> the resolved element declaration / resolved model group definition to walk
> from the object representing the "ref" to the actual object where it is
> declared/defined (and given the model group definition, you need to walk
to
> it's model group). From an abstract point of view, a particle has a
term,
> which is either a model group, a wildcard or an element declaration, i.e.,
> all the above walking has already been done. This structure is recursive
on
> model group which has concrete contents and abstract particles, both of
> which represent more particles. Something like this is probably what you
> want to do
>
> public void visit(XSDParticle xsdParticle)
> {
> XSDTerm xsdTerm = xsdParticle.getTerm();
> if (xsdTerm instanceof XSDModelGroup)
> {
> XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
> for (Iterator i = xsdModelGroup.getParticles().iterator();
> i.hasNext(); )
> {
> visit((XSDParticle)i.next());
> }
> }
> else if (xsdTerm instanceof XSDWildcard)
> {
> XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
> }
> else
> {
> XSDElementDeclaration xsdElementDeclaration =
> (XSDElementDeclaration)xsdTerm;
> }
> }
>
> Gary J wrote:
>
> I'm sure this is a total newbie question but I'm still oretty new to the
> Schema Infoset Model and am having a little trouble wrapping my brain
around
> it so I'm taking it one step at a time and hopefully don't annoy people
too
> much with these simple questions. I'm afraid this is a little long so
> please bear with me. I promise to write a "XML Schema Infoset Model for
> Idiots" article when this is all done to avoid this in the future :)
>
> I mentioned in another post that I process all the element declarations in
a
> schema like this:
>
> EList elements = schema.getSchema().getElementDeclarations()
> for (Iterator iter = elements.iterator(); iter.hasNext(); )
> {
> XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
> // do something with the element
> }
>
> So if I come to something like this:
>
> <xsd:element name="Start">
> <xsd:complexType>
> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
> </xsd:complexType>
> </xsd:element>
>
> The object "element" will contain the XSDElementDeclaration for the schema
> element "Start". I can even see the name of this element (Start) using
> element.getName();
>
> Here's where things get a little hairy for guys like me who are just
> starting out with the schema model. Could someone please let me know when
I
> go off the rails?
>
> At this point I've got my "element" object which contains the schema
element
> named "Start". Now I know that a schema element can be either a simple or
> complex type. If it is a simple type I don't need to go much further for
> now. If it is a complex type I need to do some further processing to get
> the contents.
>
> I check if the element is a complexType:
>
> XSDParticle particle = type.getComplexType();
>
> If I get back null that means the element is a simple type. In this
example
> I get back a particle which tells me I have a complex type.
>
> So I obtain the contents of the complex type:
>
> XSDParticleContent content = particle.getContent();
>
> To me this means I'm sitting right about here in the schema:
>
> --> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
>
> That's where I get lost. I know that I should be able to see the
> <xsd:choice> element as some item in "content" but I do not know where to
> find it.
>
> Could anyone give me a few pointers?
>
> Thanks
>
> Gary
>
>
>
>
>
|
|
|
Re: Traversing a schema [message #590636 is a reply to message #50667] |
Fri, 20 August 2004 17:09 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Gary J![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=gvj%40accu.ca) Messages: 61 Registered: July 2009 |
Member |
|
|
Hi,
I'm just getting into this now. When I walk the schema I'm starting with
the Element Declarations and determining the type of each element with
element.getTypeDefinition() from which I will get the Particle if the type
is complex. I can then get the Term and process as you suggested.
I noticed that there are some situations when getTypeDefinition() returns
null. One case is when the element is a reference, ie: ref= . If I don't
get back a type, how can I traverse the elements if the referenced item is a
complex element?
Are there other situations when getTypeDefinition() returns a null?
I'm slowly chewing my way through Part 1 but its not easy going :)
Thanks
"Ed Merks" <merks@ca.ibm.com> wrote in message
news:cg20aa$hot$1@eclipse.org...
Gary,
Because XSD represents both the concrete syntax of XML Schema and the
abstract component model defined in the XML Schema spec, you can analyze the
particle structure either according to its syntax or according to it's
component structure. From a concrete point of view, a particle has
content of type XSDParticleContent, which can either be a model group, a
wildcard, an element declaration, or a model group definition. If it's an
element declaration or model group definition, you have to be sure to get
the resolved element declaration / resolved model group definition to walk
from the object representing the "ref" to the actual object where it is
declared/defined (and given the model group definition, you need to walk to
it's model group). From an abstract point of view, a particle has a term,
which is either a model group, a wildcard or an element declaration, i.e.,
all the above walking has already been done. This structure is recursive on
model group which has concrete contents and abstract particles, both of
which represent more particles. Something like this is probably what you
want to do
public void visit(XSDParticle xsdParticle)
{
XSDTerm xsdTerm = xsdParticle.getTerm();
if (xsdTerm instanceof XSDModelGroup)
{
XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
for (Iterator i = xsdModelGroup.getParticles().iterator();
i.hasNext(); )
{
visit((XSDParticle)i.next());
}
}
else if (xsdTerm instanceof XSDWildcard)
{
XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
}
else
{
XSDElementDeclaration xsdElementDeclaration =
(XSDElementDeclaration)xsdTerm;
}
}
Gary J wrote:
I'm sure this is a total newbie question but I'm still oretty new to the
Schema Infoset Model and am having a little trouble wrapping my brain around
it so I'm taking it one step at a time and hopefully don't annoy people too
much with these simple questions. I'm afraid this is a little long so
please bear with me. I promise to write a "XML Schema Infoset Model for
Idiots" article when this is all done to avoid this in the future :)
I mentioned in another post that I process all the element declarations in a
schema like this:
EList elements = schema.getSchema().getElementDeclarations()
for (Iterator iter = elements.iterator(); iter.hasNext(); )
{
XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
// do something with the element
}
So if I come to something like this:
<xsd:element name="Start">
<xsd:complexType>
<xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The object "element" will contain the XSDElementDeclaration for the schema
element "Start". I can even see the name of this element (Start) using
element.getName();
Here's where things get a little hairy for guys like me who are just
starting out with the schema model. Could someone please let me know when I
go off the rails?
At this point I've got my "element" object which contains the schema element
named "Start". Now I know that a schema element can be either a simple or
complex type. If it is a simple type I don't need to go much further for
now. If it is a complex type I need to do some further processing to get
the contents.
I check if the element is a complexType:
XSDParticle particle = type.getComplexType();
If I get back null that means the element is a simple type. In this example
I get back a particle which tells me I have a complex type.
So I obtain the contents of the complex type:
XSDParticleContent content = particle.getContent();
To me this means I'm sitting right about here in the schema:
--> <xsd:choice>
<xsd:element name="item1" type="xsd:string</xsd:element>
<xsd:element name="item2" type="xsd:string</xsd:element>
</xsd:choice>
That's where I get lost. I know that I should be able to see the
<xsd:choice> element as some item in "content" but I do not know where to
find it.
Could anyone give me a few pointers?
Thanks
Gary
|
|
|
Re: Traversing a schema [message #590654 is a reply to message #51048] |
Fri, 20 August 2004 17:35 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Ed Merks![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=ed.merks%40gmail.com) Messages: 33224 Registered: July 2009 |
Senior Member |
|
|
Gary,
If you have an XSDElementDeclaration x and
x.isElementDeclarationReference() is true, then you need to do
x.getResolvedElementDeclaration() to get the actual declaration rather
then the reference to it. If it's false, then
x.getResolvedElementDeclaration() == x, i.e., it resolves to itself.
Gary J wrote:
>Hi,
>
>I'm just getting into this now. When I walk the schema I'm starting with
>the Element Declarations and determining the type of each element with
>element.getTypeDefinition() from which I will get the Particle if the type
>is complex. I can then get the Term and process as you suggested.
>
>I noticed that there are some situations when getTypeDefinition() returns
>null. One case is when the element is a reference, ie: ref= . If I don't
>get back a type, how can I traverse the elements if the referenced item is a
>complex element?
>
>Are there other situations when getTypeDefinition() returns a null?
>
>I'm slowly chewing my way through Part 1 but its not easy going :)
>
>Thanks
>
>
>
>
>"Ed Merks" <merks@ca.ibm.com> wrote in message
>news:cg20aa$hot$1@eclipse.org...
>Gary,
>
>Because XSD represents both the concrete syntax of XML Schema and the
>abstract component model defined in the XML Schema spec, you can analyze the
>particle structure either according to its syntax or according to it's
>component structure. From a concrete point of view, a particle has
>content of type XSDParticleContent, which can either be a model group, a
>wildcard, an element declaration, or a model group definition. If it's an
>element declaration or model group definition, you have to be sure to get
>the resolved element declaration / resolved model group definition to walk
>from the object representing the "ref" to the actual object where it is
>declared/defined (and given the model group definition, you need to walk to
>it's model group). From an abstract point of view, a particle has a term,
>which is either a model group, a wildcard or an element declaration, i.e.,
>all the above walking has already been done. This structure is recursive on
>model group which has concrete contents and abstract particles, both of
>which represent more particles. Something like this is probably what you
>want to do
>
>public void visit(XSDParticle xsdParticle)
>{
> XSDTerm xsdTerm = xsdParticle.getTerm();
> if (xsdTerm instanceof XSDModelGroup)
> {
> XSDModelGroup xsdModelGroup = (XSDModelGroup)xsdTerm;
> for (Iterator i = xsdModelGroup.getParticles().iterator();
>i.hasNext(); )
> {
> visit((XSDParticle)i.next());
> }
> }
> else if (xsdTerm instanceof XSDWildcard)
> {
> XSDWildcard xsdWildcard = (XSDWildcard)xsdTerm;
> }
> else
> {
> XSDElementDeclaration xsdElementDeclaration =
>(XSDElementDeclaration)xsdTerm;
> }
>}
>
>Gary J wrote:
>
>I'm sure this is a total newbie question but I'm still oretty new to the
>Schema Infoset Model and am having a little trouble wrapping my brain around
>it so I'm taking it one step at a time and hopefully don't annoy people too
>much with these simple questions. I'm afraid this is a little long so
>please bear with me. I promise to write a "XML Schema Infoset Model for
>Idiots" article when this is all done to avoid this in the future :)
>
>I mentioned in another post that I process all the element declarations in a
>schema like this:
>
>EList elements = schema.getSchema().getElementDeclarations()
>for (Iterator iter = elements.iterator(); iter.hasNext(); )
> {
> XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
> // do something with the element
> }
>
>So if I come to something like this:
>
> <xsd:element name="Start">
> <xsd:complexType>
> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
> </xsd:complexType>
> </xsd:element>
>
>The object "element" will contain the XSDElementDeclaration for the schema
>element "Start". I can even see the name of this element (Start) using
>element.getName();
>
>Here's where things get a little hairy for guys like me who are just
>starting out with the schema model. Could someone please let me know when I
>go off the rails?
>
>At this point I've got my "element" object which contains the schema element
>named "Start". Now I know that a schema element can be either a simple or
>complex type. If it is a simple type I don't need to go much further for
>now. If it is a complex type I need to do some further processing to get
>the contents.
>
>I check if the element is a complexType:
>
>XSDParticle particle = type.getComplexType();
>
>If I get back null that means the element is a simple type. In this example
>I get back a particle which tells me I have a complex type.
>
>So I obtain the contents of the complex type:
>
>XSDParticleContent content = particle.getContent();
>
>To me this means I'm sitting right about here in the schema:
>
>--> <xsd:choice>
> <xsd:element name="item1" type="xsd:string</xsd:element>
> <xsd:element name="item2" type="xsd:string</xsd:element>
> </xsd:choice>
>
>That's where I get lost. I know that I should be able to see the
><xsd:choice> element as some item in "content" but I do not know where to
>find it.
>
>Could anyone give me a few pointers?
>
>Thanks
>
>Gary
>
>
>
>
>
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: Traversing a schema [message #590698 is a reply to message #51102] |
Mon, 23 August 2004 11:04 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Ed Merks![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=ed.merks%40gmail.com) Messages: 33224 Registered: July 2009 |
Senior Member |
|
|
Gary,
This looks good. Yes, an anonymous type will have a null name.
Gary J wrote:
>Ed,
>
>Thanks for all the patient help.
>I think I have it almost figured out. I'll step through what I am doing. My
>goal is to print out a simplified schema (attributes ignored for now).
>
>I started with a very simple schema:
>
> <xsd:element name="test">
> <xsd:complexType>
> <xsd:choice>
> <xsd:element ref="comment1" minOccurs="1" maxOccurs="1"></xsd:element>
> <xsd:element ref="comment2" minOccurs="1" maxOccurs="1"></xsd:element>
> </xsd:choice>
> </xsd:complexType>
> </xsd:element>
>
> <xsd:element name="comment1" type="xsd:string"></xsd:element>
> <xsd:element name="comment2" type="xsd:string"></xsd:element>
></xsd:schema>
>
>Admittedly this is a trivial schema but it should suffice. Here's the
>traversing sequence:
>
>1. I start with schema.getElementDeclarations() and process each element in
>the list:
>XSDElementDeclaration element = (XSDElementDeclaration)iter.next();
>
>2. I can print the element name
>System.out.println(element.getName());
>
>3. I determine that "test" is a complexType:
>XSDTypeDefinition type = element.getTypeDefinition().
>** Question: how do I determine this is an anonymous type? getName() returns
>null?
>
>4. Since this is a complexType I'll get the particle:
>XSDParticle particle = type.getComplexType();
>
>5. Get the term for this particle
>XSDTerm term = particle.getTerm();
>
>6. In this case term is an instance of XSDModelGroup
>XSDModelGroup modelGroup = (XSDModelGroup)term;
>
>7. I get the compositor and print it out
>XSDCompositor c = modelGroup.getCompositor();
>System.out.println(c.getName()); // "choice"
>
>8. I process each particle in the modelGroup
>EList list = modelGroup.getParticles();
>
>8. back to 5
>
>9. this time the term is an instance of XSDElementDeclaration
>XSDElementDeclaration element = (XSDElementDeclaration)term;
>
>10. back to 2
>
>11. This time the element is a reference
>element.isElementDeclarationReference() is true
>element = element.getResolvedElementDeclaration();
>
>13. Simple type, just print name of element
>
>etc
>
>
>This should give me:
><test>
> <choice>
> <comment1>
> <comment2>
>(without the brackets)
>
>How does this look? Am I on the right track? There's a few gaps and I
>haven't even considered things like groups but its only been a week or so
>since I started this.
>
>Thanks
>
>
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Sun Oct 06 12:55:06 GMT 2024
Powered by FUDForum. Page generated in 0.39243 seconds
|