Re: XML: Getting "special" attribute
Hi Staminir and thanks for your reply, see below.
Stanimir Stamenkov wrote:
Sun, 31 Aug 2008 14:54:51 +0200, /WP/:
I'm using Xerces to create an XSModel and from a schema and from this
XSModel I obtain an element declaration map (basically to get the root
element from which I can get all sub-elements).
[...]
<xs:element name="movie" type="xs:string"
foo:bar="somevalue"/>
[...]
When I inspect the element name I don't see the attribute
foo:bar="somevalue" among its attributes. Anyone know how to "see" it?
I can provide a lot more details if needed. Right now my code depends
on a very old, modified version of xerces that is able to see
attributes in other namespaces (if that is the correct way to describe
the attribute foo:bar?).
I'm not 100% sure whether this is your case but enhancements to the XML
Schema annotation support was first introduced in Xerces 2.8.0
<http://xerces.apache.org/xerces2-j/releases.html>:
All of the schema component interfaces in the XML Schema API now have
a getAnnotations() method which returns a list of XSAnnotations. This
includes annotations on particles and attribute uses which were
previously "lost".
If you notice in the original XML Schema specification particle
components <http://www.w3.org/TR/xmlschema-1/#Particle_details> don't
have annotations.
Thanks for your suggestions. I wrote this complete test program, it uses
Xerces-J 2.9.1:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.xerces.xs.XSComplexTypeDefinition;
import org.apache.xerces.xs.XSConstants;
import org.apache.xerces.xs.XSElementDeclaration;
import org.apache.xerces.xs.XSImplementation;
import org.apache.xerces.xs.XSLoader;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.xs.XSModelGroup;
import org.apache.xerces.xs.XSNamedMap;
import org.apache.xerces.xs.XSObjectList;
import org.apache.xerces.xs.XSParticle;
import org.apache.xerces.xs.XSTerm;
import org.apache.xerces.xs.XSTypeDefinition;
import org.apache.xerces.impl.xs.XSImplementationImpl;
public class ParseXMLSchema {
public static void main(String[] args) {
ParseXMLSchema instance = new ParseXMLSchema();
instance.parseXMLSchema(new File("test-1.xsd"));
}
private void parseXMLSchema(final File f) {
parseXMLSchema(f.getAbsolutePath());
}
private void parseXMLSchema(final String schemaFileName) {
XSImplementation xsImplementation = new XSImplementationImpl();
XSLoader xsLoader = xsImplementation.createXSLoader(null); // or
new XSLoaderImpl();
XSModel xsModel = xsLoader.loadURI(schemaFileName);
XSNamedMap elementMap =
xsModel.getComponents(XSConstants.ELEMENT_DECLARATION);
XSElementDeclaration topElementDecl =
(XSElementDeclaration)elementMap.item(0);
process(topElementDecl);
}
private void process(final XSElementDeclaration elementDecl) {
if (elementDecl == null) {
System.err.println("elementDecl is null.");
return;
}
XSComplexTypeDefinition typeDef =
(XSComplexTypeDefinition)elementDecl.getTypeDefinition();
System.out.print("Complex element: " + elementDecl.getName());
XSObjectList attributeUsesList = typeDef.getAttributeUses();
if (attributeUsesList.getLength() == 0) {
System.out.print(", no attributes");
}
else {
for (int i = 0; i < attributeUsesList.getLength(); ++i) {
System.out.println("Attribute " + i + 1 + ": " +
attributeUsesList.item(i).getName());
}
}
List<XSElementDeclaration> ces = new
ArrayList<XSElementDeclaration>();
XSModelGroup modelGroup =
(XSModelGroup)typeDef.getParticle().getTerm();
XSObjectList particles = modelGroup.getParticles();
for (int i = 0; i < particles.getLength(); ++i) {
XSParticle particle = (XSParticle)particles.item(i);
XSTerm term = particle.getTerm();
XSObjectList annotations = particle.getAnnotations();
printAnnotations(annotations);
if (term instanceof XSElementDeclaration) {
XSElementDeclaration newElement = (XSElementDeclaration)term;
if (newElement.getTypeDefinition().getTypeCategory() ==
XSTypeDefinition.SIMPLE_TYPE) {
System.out.print("Simple element: " + newElement.getName());
XSObjectList simpleElementAnnotations =
newElement.getAnnotations();
printAnnotations(simpleElementAnnotations);
}
else if (newElement.getTypeDefinition().getTypeCategory()
== XSTypeDefinition.COMPLEX_TYPE) {
ces.add(newElement);
}
else {
System.out.println("Other type.");
}
}
else {
System.out.println("Something else.");
}
}
for (XSElementDeclaration e : ces) {
process(e);
}
}
private void printAnnotations(final XSObjectList annotations) {
if (annotations.getLength() == 0) {
System.out.println(", no annotations");
}
else {
System.out.println(", " + annotations.getLength() + "
annotations");
}
}
}
Sorry for the length, but I wanted it to be complete. For the following
test schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="myns" xmlns="myns"
elementFormDefault="qualified"
xmlns:foo="http://www.foobarbaz.com/foo">
<xs:element name="movies">
<xs:complexType>
<xs:sequence>
<xs:element name="movie" type="xs:string"
maxOccurs="unbounded" foo:bar="baz"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
it produces the following output:
Complex element: movies, no attributes, no annotations
Simple element: movie, no annotations
As you can see, I am unable to detect foo:bar="baz" on the element
movie. Any ideas what I'm missing in my code?
In any case you would get better support on the Xerces-J Users mailing
list:
http://mail-archives.apache.org/mod_mbox/xerces-j-users/
I posted there over a week ago without a single reply and activity on
that list is very low in general so please forgive me. :( But I will
post their again with the complete example program I used in this post.
- Eric (WP)