Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions exist-core/src/main/antlr/org/exist/xquery/parser/XQuery.g
Original file line number Diff line number Diff line change
Expand Up @@ -1271,14 +1271,17 @@ forwardAxis : forwardAxisSpecifier COLON! COLON! ;
forwardAxisSpecifier
:
"child" | "self" | "attribute" | "descendant" | "descendant-or-self"
| "following-sibling" | "following"
| "following-sibling-or-self" | "following-sibling"
| "following-or-self" | "following"
;

reverseAxis : reverseAxisSpecifier COLON! COLON! ;

reverseAxisSpecifier
:
"parent" | "ancestor" | "ancestor-or-self" | "preceding-sibling" | "preceding"
"parent" | "ancestor" | "ancestor-or-self"
| "preceding-sibling-or-self" | "preceding-sibling"
| "preceding-or-self" | "preceding"
;

nodeTest throws XPathException
Expand Down Expand Up @@ -2117,12 +2120,20 @@ reservedKeywords returns [String name]
|
"ancestor-or-self" { name= "ancestor-or-self"; }
|
"preceding-sibling-or-self" { name= "preceding-sibling-or-self"; }
|
"preceding-sibling" { name= "preceding-sibling"; }
|
"following-sibling-or-self" { name= "following-sibling-or-self"; }
|
"following-sibling" { name= "following-sibling"; }
|
"following-or-self" { name = "following-or-self"; }
|
"following" { name = "following"; }
|
"preceding-or-self" { name = "preceding-or-self"; }
|
"preceding" { name = "preceding"; }
|
"item" { name= "item"; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3310,12 +3310,20 @@ throws PermissionDeniedException, EXistException
|
"descendant-or-self" { axis= Constants.DESCENDANT_SELF_AXIS; }
|
"following-sibling-or-self" { axis= Constants.FOLLOWING_SIBLING_OR_SELF_AXIS; }
|
"following-sibling" { axis= Constants.FOLLOWING_SIBLING_AXIS; }
|
"following-or-self" { axis= Constants.FOLLOWING_OR_SELF_AXIS; }
|
"following" { axis= Constants.FOLLOWING_AXIS; }
|
"preceding-sibling-or-self" { axis= Constants.PRECEDING_SIBLING_OR_SELF_AXIS; }
|
"preceding-sibling" { axis= Constants.PRECEDING_SIBLING_AXIS; }
|
"preceding-or-self" { axis= Constants.PRECEDING_OR_SELF_AXIS; }
|
"preceding" { axis= Constants.PRECEDING_AXIS; }
|
"ancestor" { axis= Constants.ANCESTOR_AXIS; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ public NodeSet selectFollowing(final NodeSet pl, final int position, final int c
if(!reference.getNodeId().isDescendantOf(nodes[j].getNodeId())) {
if(position < 0 || ++n == position) {
if (contextId != Expression.IGNORE_CONTEXT
&& contextId != Expression.NO_CONTEXT_ID
&& nodes[j].getContext() != null
&& reference.getContext() != null
&& nodes[j].getContext().getContextId() == reference.getContext().getContextId()) {
Expand Down Expand Up @@ -846,6 +847,7 @@ public NodeSet selectPreceding(final NodeSet pl, final int position,
if(!reference.getNodeId().isDescendantOf(nodes[j].getNodeId())) {
if(position < 0 || ++n == position) {
if (contextId != Expression.IGNORE_CONTEXT
&& contextId != Expression.NO_CONTEXT_ID
&& nodes[j].getContext() != null
&& reference.getContext() != null
&& nodes[j].getContext().getContextId() == reference.getContext().getContextId()) {
Expand Down
8 changes: 8 additions & 0 deletions exist-core/src/main/java/org/exist/xquery/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public interface Constants {
//combines /descendant-or-self::node()/attribute:*
int DESCENDANT_ATTRIBUTE_AXIS = 13;

// --- XQuery 4.0 axes ---
int FOLLOWING_OR_SELF_AXIS = 14;
int FOLLOWING_SIBLING_OR_SELF_AXIS = 15;
/** Reverse axis */
int PRECEDING_OR_SELF_AXIS = 16;
/** Reverse axis */
int PRECEDING_SIBLING_OR_SELF_AXIS = 17;

/**
* Node types
*/
Expand Down
72 changes: 72 additions & 0 deletions exist-core/src/main/java/org/exist/xquery/LocationStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,21 @@ public Sequence eval(Sequence contextSequence, final Item contextItem)
result = getSiblings(context, contextSequence);
break;

// --- XQuery 4.0 combined axes ---
case Constants.FOLLOWING_OR_SELF_AXIS:
case Constants.PRECEDING_OR_SELF_AXIS:
result = getOrSelfAxis(context, contextSequence,
axis == Constants.FOLLOWING_OR_SELF_AXIS
? Constants.FOLLOWING_AXIS : Constants.PRECEDING_AXIS);
break;

case Constants.FOLLOWING_SIBLING_OR_SELF_AXIS:
case Constants.PRECEDING_SIBLING_OR_SELF_AXIS:
result = getOrSelfAxis(context, contextSequence,
axis == Constants.FOLLOWING_SIBLING_OR_SELF_AXIS
? Constants.FOLLOWING_SIBLING_AXIS : Constants.PRECEDING_SIBLING_AXIS);
break;

default:
throw new IllegalArgumentException("Unsupported axis specified");
}
Expand Down Expand Up @@ -912,6 +927,63 @@ protected Sequence getSiblings(final XQueryContext context, final Sequence conte
*
* @throws XPathException if an error occurs
*/
/**
* Evaluates an XQuery 4.0 combined axis (e.g., following-or-self, preceding-sibling-or-self).
* Returns the union of the self axis result and the base axis result, preserving document order.
*
* @param context the XQuery context
* @param contextSequence the context sequence
* @param baseAxis the base axis constant (e.g., Constants.FOLLOWING_AXIS)
* @return the combined result in document order
*/
private Sequence getOrSelfAxis(final XQueryContext context, final Sequence contextSequence,
final int baseAxis) throws XPathException {
// Save and temporarily switch axis to get results
final int savedAxis = this.axis;
try {
final Sequence selfOrRelatedResult;
final Sequence baseResult;

if (baseAxis == Constants.FOLLOWING_AXIS) {
// following-or-self = descendant-or-self | following
// (all nodes at or after context node in document order)
this.axis = Constants.DESCENDANT_SELF_AXIS;
selfOrRelatedResult = getDescendants(context, contextSequence);
this.axis = Constants.FOLLOWING_AXIS;
baseResult = getPrecedingOrFollowing(context, contextSequence);
} else if (baseAxis == Constants.PRECEDING_AXIS) {
// preceding-or-self = ancestor-or-self | preceding
// (all nodes at or before context node in document order)
this.axis = Constants.ANCESTOR_SELF_AXIS;
selfOrRelatedResult = getAncestors(context, contextSequence);
this.axis = Constants.PRECEDING_AXIS;
baseResult = getPrecedingOrFollowing(context, contextSequence);
} else {
// following-sibling-or-self / preceding-sibling-or-self = self | sibling
this.axis = Constants.SELF_AXIS;
selfOrRelatedResult = getSelf(context, contextSequence);
this.axis = baseAxis;
baseResult = getSiblings(context, contextSequence);
}

// Union preserving document order
if (selfOrRelatedResult.isEmpty()) {
return baseResult;
}
if (baseResult.isEmpty()) {
return selfOrRelatedResult;
}
final ValueSequence combined = new ValueSequence();
combined.addAll(selfOrRelatedResult);
combined.addAll(baseResult);
combined.removeDuplicates();
combined.sortInDocumentOrder();
return combined;
} finally {
this.axis = savedAxis;
}
}

private Sequence getPrecedingOrFollowing(final XQueryContext context, final Sequence contextSequence)
throws XPathException {
final int position = computeLimit();
Expand Down
8 changes: 8 additions & 0 deletions exist-core/src/main/java/org/exist/xquery/Predicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,21 @@ private Sequence selectByPosition(final Sequence outerSequence,
temp = contextSet.selectPrecedingSiblings(p, Expression.IGNORE_CONTEXT);
break;
case Constants.FOLLOWING_SIBLING_AXIS:
case Constants.FOLLOWING_SIBLING_OR_SELF_AXIS:
temp = contextSet.selectFollowingSiblings(p, Expression.IGNORE_CONTEXT);
reverseAxis = false;
break;
case Constants.FOLLOWING_AXIS:
case Constants.FOLLOWING_OR_SELF_AXIS:
temp = contextSet.selectFollowing(p, Expression.IGNORE_CONTEXT);
reverseAxis = false;
break;
case Constants.PRECEDING_OR_SELF_AXIS:
temp = contextSet.selectPreceding(p, Expression.IGNORE_CONTEXT);
break;
case Constants.PRECEDING_SIBLING_OR_SELF_AXIS:
temp = contextSet.selectPrecedingSiblings(p, Expression.IGNORE_CONTEXT);
break;
case Constants.SELF_AXIS:
temp = p;
reverseAxis = false;
Expand Down
115 changes: 115 additions & 0 deletions exist-core/src/test/java/org/exist/xquery/XQ4AxesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
* info@exist-db.org
* http://www.exist-db.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.exist.xquery;

import org.exist.test.ExistXmldbEmbeddedServer;
import org.junit.ClassRule;
import org.junit.Test;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XQueryService;

import static org.junit.Assert.assertEquals;

/**
* Tests for XQuery 4.0 combined axes:
* following-or-self, following-sibling-or-self,
* preceding-or-self, preceding-sibling-or-self.
*/
public class XQ4AxesTest {

@ClassRule
public static final ExistXmldbEmbeddedServer server =
new ExistXmldbEmbeddedServer(false, true, true);

private static final String DATA =
"<root>" +
" <a id='1'>" +
" <b id='2'/>" +
" <c id='3'><d id='4'/></c>" +
" <e id='5'/>" +
" </a>" +
" <f id='6'/>" +
"</root>";

private String query(final String xquery) throws XMLDBException {
final XQueryService qs = server.getRoot().getService(XQueryService.class);
final String fullQuery = "let $data := " + DATA +
" return string-join(" + xquery + ", ',')";
final ResourceSet result = qs.query(fullQuery);
return result.getResource(0).getContent().toString();
}

// --- following-or-self ---

@Test
public void followingOrSelf() throws XMLDBException {
// following-or-self = context node and all nodes after it in document order
assertEquals("3,4,5,6", query("$data//c/following-or-self::*/@id/string()"));
}

@Test
public void followingOrSelfFromFirst() throws XMLDBException {
// following-or-self from a = a plus all nodes after a in document order (descendants + following)
assertEquals("1,2,3,4,5,6", query("$data/a/following-or-self::*/@id/string()"));
}

// --- following-sibling-or-self ---

@Test
public void followingSiblingOrSelf() throws XMLDBException {
assertEquals("3,5", query("$data/a/c/following-sibling-or-self::*/@id/string()"));
}

@Test
public void followingSiblingOrSelfFromFirst() throws XMLDBException {
assertEquals("2,3,5", query("$data/a/b/following-sibling-or-self::*/@id/string()"));
}

// --- preceding-or-self ---

@Test
public void precedingOrSelf() throws XMLDBException {
// preceding-or-self = context node and all nodes before it in document order (ancestors + preceding)
assertEquals("1,2,3", query("$data//c/preceding-or-self::*/@id/string()"));
}

// --- preceding-sibling-or-self ---

@Test
public void precedingSiblingOrSelf() throws XMLDBException {
assertEquals("2,3", query("$data/a/c/preceding-sibling-or-self::*/@id/string()"));
}

@Test
public void precedingSiblingOrSelfNameTest() throws XMLDBException {
assertEquals("3", query("$data/a/c/preceding-sibling-or-self::c/@id/string()"));
}

// --- self included in name-specific test ---

@Test
public void followingSiblingOrSelfNameMatch() throws XMLDBException {
// c has no following sibling named 'c', but self is 'c'
assertEquals("3", query("$data/a/c/following-sibling-or-self::c/@id/string()"));
}
}
32 changes: 32 additions & 0 deletions exist-core/src/test/java/xquery/xquery4/XQuery4Tests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
* info@exist-db.org
* http://www.exist-db.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package xquery.xquery4;

import org.exist.test.runner.XSuite;
import org.junit.runner.RunWith;

@RunWith(XSuite.class)
@XSuite.XSuiteFiles({
"src/test/xquery/xquery4",
})
public class XQuery4Tests {
}
Loading
Loading