Skip to main content

Using xsom to return a list of attributes in definition order

3 replies [Last post]
jacob_northey
Offline
Joined: 2006-09-29

I just started using xsom for a project that I am working on. Currently I need to get a list of all attributes from a specific element declaration. That can be done using the code below. The problem that I run into is that the attributes are listed in alphabetical order (by their name). What is really needed is to have the attributes in the order that they are defined in the schema. How do I get a list of the attributes in the order that they were defined in the schema document (see below for example)?
[pre]
private Table parseDeclaration(XSElementDecl decl)
{
Table table = new Table();
for (XSAttGroupDecl group : decl.getType().asComplexType().getAttGroups())
{
for (XSAttributeUse attr : group.getAttributeUses())
{
table.addField(attr.getDecl().getName());
}
}
return table;
}

[/pre]

Attributes returned in order [pre]id, name, value[/pre] when the order should be [pre]id, value, name[/pre].

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09

I don't think the "definition order" is a well-defined concept in XML Schema. Between attribute groups referencing multiple (potentialyl dupliate) attribute uses, derivation, etc.

But it certainly makes to try to make it look like the order in the schema documents, so I replaced TreeMap by LinkedHashMap.

jacob_northey
Offline
Joined: 2006-09-29

Thank you for following up so fast.

I do realize that the complexities of XML Schema make it hard to determine the order in which attributes are defined. Luckily I have only run into instances where there is a single AttributeGroup used.

jacob_northey
Offline
Joined: 2006-09-29

[pre]
Collection attributes = ( Collection ) attrGroup.getAttributeUses();

Collections.sort(( List )attributes, new Comparator () {
public int compare(XSAttributeUse x, XSAttributeUse y) {
Locator xLocator = x.getDecl().getLocator();
Locator yLocator = y.getDecl().getLocator();
if (xLocator.getLineNumber() == yLocator.getLineNumber())
return xLocator.getColumnNumber() - yLocator.getColumnNumber();
return xLocator.getLineNumber() - yLocator.getLineNumber();
}});
[/pre]

I ran into the locator field in the attribute declaration object. By adding a sort method I can sort the list to get the original document order. Is there a more efficient way to do this? It seems that the attributes must be loaded in document order at some point, so there must be a list of attributes in that order around.