Re: XML: Getting "special" attribute
Sun, 31 Aug 2008 18:13:47 +0300, /Stanimir Stamenkov/:
Sun, 31 Aug 2008 14:54:51 +0200, /WP/:
[...]
<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>:
Lookin at it a bit further and trying it out I think this is more
what you're after
<http://xerces.apache.org/xerces2-j/features.html#generate-synthetic-annotations>:
Enable generation of synthetic annotations. A synthetic annotation
will be generated when a schema component has non-schema attributes
but no child annotation.
Since: Xerces-J 2.7.0
Given your original example schema packaged as "example.xsd" with
the following class, appears to work for me using Xerces 2.9.1 (but
I guess 2.7.0 should be fine, too):
-----XSModelTest.java
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.apache.xerces.xs.XSAnnotation;
import org.apache.xerces.xs.XSComplexTypeDefinition;
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.XSObjectList;
import org.apache.xerces.xs.XSParticle;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
public class XSModelTest {
private static SAXTransformerFactory transformerFactory =
(SAXTransformerFactory) TransformerFactory.newInstance();
public static void main(String[] args) throws Exception {
DOMImplementationRegistry registry =
DOMImplementationRegistry.newInstance();
XSImplementation impl = (XSImplementation)
registry.getDOMImplementation("XS-Loader");
XSLoader schemaLoader = impl.createXSLoader(null);
DOMConfiguration config = schemaLoader.getConfig();
config.setParameter("http://apache.org/xml/features/"
+ "generate-synthetic-annotations",
Boolean.TRUE);
XSModel schema = schemaLoader.loadURI(XSModelTest.class
.getResource("example.xsd").toExternalForm());
XSElementDeclaration elemDecl =
schema.getElementDeclaration("movies", null);
XSComplexTypeDefinition typeDef = (XSComplexTypeDefinition)
elemDecl.getTypeDefinition();
XSParticle contentModelParticle = typeDef.getParticle();
printAnnotations(contentModelParticle.getAnnotations());
XSModelGroup contentModelGroup = (XSModelGroup)
contentModelParticle.getTerm();
XSParticle elemParticle = (XSParticle)
contentModelGroup.getParticles().item(0);
System.out.println("--- Particle annotations:");
printAnnotations(elemParticle.getAnnotations());
System.out.println("--- Element declaration annotations:");
elemDecl = (XSElementDeclaration) elemParticle.getTerm();
printAnnotations(elemDecl.getAnnotations());
System.out.println("--- End.");
}
static void printAnnotations(XSObjectList annotations)
throws Exception {
for (int i = 0, len = annotations.getLength();
i < len; i++) {
TransformerHandler handler =
transformerFactory.newTransformerHandler();
Transformer transformer = handler.getTransformer();
transformer.setOutputProperty(OutputKeys
.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys
.INDENT, "yes");
handler.setResult(new StreamResult(System.out));
((XSAnnotation) annotations.item(i))
.writeAnnotation(handler,
XSAnnotation.SAX_CONTENTHANDLER);
}
}
}
-----XSModelTest.java--
--
Stanimir