Description
In my schema, I am allowing extensibility using the xsi:type attribute. To faciliate code separation, I generally have schema components which define a particular inherited type (generally by extension of a base type) for which particular instances will be inserted into the document. These types have generic properties that I want all code to be able to understand, and only compile the schema into the more specific code which needs more information about the type.
However, for both debugging, logging, and for using in Map keys I'd like easy access to the QName defined in the xsi:type attribute even if that type is not understood by the compiled schema.
I have implemented the following utility method to accomplish this:
—
public static QName getXsiType(final XmlObject object)
{
XmlCursor cursor = object.newCursor();
final String xsiType =
cursor.getAttributeText(new QName("http://www.w3.org/2001/XMLSchema-instance", "type"));
if (xsiType == null)
return null;
final String[] toks = xsiType.split(":");
String prefix;
String localName;
if (toks.length > 1)
else
{ prefix = ""; localName = toks[0]; } QName namespace = null;
cursor = object.newCursor();
do
while (namespace == null && !cursor.isEnd());
// final String namespace = cursor.toNextAttribute()
// object.getDomNode().getAttributes().getNamedItem("xmlns:" + prefix).getNodeValue();
// TODO: Obviously we don't support this too well here, we're borked
// if xmlns isn't defined directly on the node. We should probably
// be okay for the near future in any case.
if (namespace == null)
return new QName(localName);
else
return new QName(namespace.getNamespaceURI(), localName);
}
–
I would think this sort of information would be ideal to be added onto the XmlObject base class.