Use XPath/XQuery to do this. Refer to the examples page for an example on how to do this.
Return the maximum offset for any DOM position under the given node.
For CharacterData nodes, this is the length of the data. For other nodes this is the number of children.
Refer to domQuery for more information on how to import and use this function.
The length of the given node