@@ -263,40 +263,61 @@ public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException
263263 this .axis = Constants .DESCENDANT_AXIS ;
264264 }
265265
266+ final Expression contextStep ;
267+ final NodeTest stepTest ;
268+ final NodeTest contextStepTest ;
269+
266270 // static analysis for empty-sequence
267271 switch (axis ) {
268272 case Constants .SELF_AXIS :
269- if (getTest ().getType () != Type .NODE ) {
270- final Expression contextStep = contextInfo .getContextStep ();
271- if (contextStep instanceof LocationStep cStep ) {
272-
273- // WM: the following checks will only work on simple filters like //a[self::b], so we
274- // have to make sure they are not applied to more complex expression types
275- if (parent .getSubExpressionCount () == 1 && !Type .subTypeOf (getTest ().getType (), cStep .getTest ().getType ())) {
276- throw new XPathException (this ,
277- ErrorCodes .XPST0005 , "Got nothing from self::" + getTest () + ", because parent node kind " + Type .getTypeName (cStep .getTest ().getType ()));
278- }
273+ if (getTest ().getType () == Type .NODE ) {
274+ break ;
275+ }
279276
280- if (parent .getSubExpressionCount () == 1 && !(cStep .getTest ().isWildcardTest () || getTest ().isWildcardTest ()) && !cStep .getTest ().equals (getTest ())) {
281- throw new XPathException (this ,
282- ErrorCodes .XPST0005 , "Self::" + getTest () + " called on set of nodes which do not contain any nodes of this name." );
283- }
284- }
277+ // WM: the following checks will only work on simple filters like //a[self::b], so we
278+ // have to make sure they are not applied to more complex expression types
279+ if (parent .getSubExpressionCount () > 1 ) {
280+ break ;
281+ }
282+
283+ contextStep = contextInfo .getContextStep ();
284+ if (!(contextStep instanceof LocationStep cStep )) {
285+ break ;
286+ }
287+
288+ stepTest = getTest ();
289+ contextStepTest = cStep .getTest ();
290+
291+ if (!Type .subTypeOf (stepTest .getType (), contextStepTest .getType ())) {
292+ // return empty sequence
293+ contextInfo .setStaticType (Type .EMPTY );
294+ staticReturnType = Type .EMPTY ;
295+ break ;
296+ }
297+
298+ if (!stepTest .isWildcardTest () &&
299+ !contextStepTest .isWildcardTest () &&
300+ !contextStepTest .equals (stepTest )) {
301+ // return empty sequence
302+ contextInfo .setStaticType (Type .EMPTY );
303+ staticReturnType = Type .EMPTY ;
285304 }
286305 break ;
287- // case Constants.DESCENDANT_AXIS:
306+ // case Constants.DESCENDANT_AXIS:
288307 case Constants .DESCENDANT_SELF_AXIS :
289- final Expression contextStep = contextInfo .getContextStep ();
290- if (contextStep instanceof LocationStep cStep ) {
291-
292- if ((
293- cStep .getTest ().getType () == Type .ATTRIBUTE ||
294- cStep .getTest ().getType () == Type .TEXT
295- )
296- && cStep .getTest () != getTest ()) {
297- throw new XPathException (this ,
298- ErrorCodes .XPST0005 , "Descendant-or-self::" + getTest () + " from an attribute gets nothing." );
299- }
308+ contextStep = contextInfo .getContextStep ();
309+ if (!(contextStep instanceof LocationStep cStep )) {
310+ break ;
311+ }
312+
313+ stepTest = getTest ();
314+ contextStepTest = cStep .getTest ();
315+
316+ if ((contextStepTest .getType () == Type .ATTRIBUTE || contextStepTest .getType () == Type .TEXT ) &&
317+ contextStepTest != stepTest ) {
318+ // return empty sequence
319+ contextInfo .setStaticType (Type .EMPTY );
320+ staticReturnType = Type .EMPTY ;
300321 }
301322 break ;
302323// case Constants.PARENT_AXIS:
0 commit comments