Hello, I am writing a custom QueryModule that can be used to parse information from a given XML element. The module has a function like the following:
public ParsedType parse (Object arg) throws QueryException { if (arg instanceof ANode) { return utils.parse((ANode) arg); } else if (arg instanceof BXElem) { return utils.parse((Element) arg); } else { ... } }
It turned out that parsing requires additional information that can be found either on the XML element itself or - more importantly for my question - in one of its ancestors. The XPath expression to determine the information looks like this: <context XML element>/ancestor-or-self::*[@attributeX or nsX:SomeElement/*/@attributeX][1]. I can evaluate this expression in the XQuery before calling method "parse" from my custom query module, and add the value of attributeX as an argument. To do so, I actually have a function to determine the value of attributeX:
declare function local:determineAttributeX($elmt as element()) { let $elementWithAttributeX := $elmt/ancestor-or-self::*[@attributeX or nsX:SomeElement/*/@attributeX][1] return if (empty($elementWithAttributeX)) then () else if($elementWithAttributeX/@attributeX) then $ elementWithAttributeX/data(@attributeX) else $ elementWithAttributeX/nsX:SomeElement/*/data(@attributeX) };
However, I wonder if it is possible to evaluate the expression also within the query module itself, i.e. when method "parse" is called. Is there a way to do so? That would reduce the complexity of the XQuery that calls "parse" (and the value of "attributeX" is always of interest when "parse" is called, so ideally "parse" can determine the value itself). I searched the internet (the archive of this mailing list as well as stackoverflow) for quite some time but could not find an answer to this question. Any help would therefore be greatly appreciated. Best regards, Johannes Echterhoff