Hi, I came across the same requirement, except that I focus on compact syntax. Here is one way to do what you want, but YMMV.
To give some context, my goal is in 2 steps: (a) Trying to smooth out RelaxNG Compact Syntax and go through the object / tree to create the Spring 4 POJOs used in the Spring 4 Rest Controller. (b) From there, I want to develop a query validator that uses RNG Compact and automatically validates the request before Spring de-serializes the request. Mostly JSON REST API scaffold development using RelaxNG Compact Syntax for both design and documentation and definition / validation of JSON schemas.
For the first purpose, I thought about annotating CompactSyntax with JJTree, but I obviously do not own JavaCC, so I decided to move on to a more programmatic approach ...
I analyzed and tested the code in several ways to determine if there was a tree implementation in binary, digested, and / or nc packages, but I don't think there is one (om / tree) as such.
So my last really successful approach was to build on a binary code and extend SchemaBuilderImpl, implement a visitor interface and pass my own SchemaBuilderImpl to CompactSyntax using a long constructor: CompactSyntax (CompactParseable parseable, Reader r, String sourceUri, SchemaBuilder sb, ErrorHandler eh, String inheritedNs)
When you call CompactParseable.parse, you get structured events in the visitor interface, and I think it is good enough to cross the rng diagram, and from here you can easily create an OM or tree.
But I'm not sure if this is the best approach. Maybe I missed something, and there really is an OM / Tree created by the rngom implementation (in my case CompactSyntax) that you can traverse to more easily define the relationship between parents and children. Or maybe there are other approaches to this.
Anyway, this is one approach that seems to work for what I want. It is mainly a visitor template, and since the interfaces were there, I decided to use them. Maybe this will work for you. In the bottom line, I could not find OM / AST, which can be traversed implemented anywhere in the implementation packages (nc, binary, digested).