You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mu...@apache.org on 2026/04/16 09:49:55 UTC

(xalan-java) branch xalan-j_xslt3.0_mvn updated: committing xalan-j xslt3.0 implementation improvements for xsl:next-match instruction

This is an automated email from the ASF dual-hosted git repository.

mukulg pushed a commit to branch xalan-j_xslt3.0_mvn
in repository https://gitbox.apache.org/repos/asf/xalan-java.git


The following commit(s) were added to refs/heads/xalan-j_xslt3.0_mvn by this push:
     new 62a753fcb committing xalan-j xslt3.0 implementation improvements for xsl:next-match instruction
62a753fcb is described below

commit 62a753fcb943f432c6adbb131d1d74999e6c996b
Author: Mukul Gandhi <ga...@gmail.com>
AuthorDate: Thu Apr 16 15:19:39 2026 +0530

    committing xalan-j xslt3.0 implementation improvements for xsl:next-match instruction
---
 README                                             |   6 +-
 .../apache/xalan/templates/ElemApplyTemplates.java | 187 +++++++++++++--------
 .../xalan/tests/xslt3/XslNextMatchTests.java       |  12 +-
 src/test/resources/xsl_next_match/gold/test4.out   |   9 +
 src/test/resources/xsl_next_match/test2.xml        |  10 ++
 src/test/resources/xsl_next_match/test4.xsl        |  47 ++++++
 6 files changed, 201 insertions(+), 70 deletions(-)

diff --git a/README b/README
index a549225b7..ecdc3e257 100644
--- a/README
+++ b/README
@@ -98,16 +98,16 @@ When this Maven build command is run, results like following shall be produced:
 [INFO]  T E S T S
 [INFO] -------------------------------------------------------
 [INFO] Running org.apache.xalan.tests.main.XalanXsl3TestSuite2
-[INFO] Tests run: 536, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 18.796 s - in org.apache.xalan.tests.main.XalanXsl3TestSuite2
+[INFO] Tests run: 537, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.746 s - in org.apache.xalan.tests.main.XalanXsl3TestSuite2
 [INFO]
 [INFO] Results:
 [INFO]
-[INFO] Tests run: 536, Failures: 0, Errors: 0, Skipped: 0
+[INFO] Tests run: 537, Failures: 0, Errors: 0, Skipped: 0
 
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
-[INFO] Total time:  51.827 s
+[INFO] Total time:  43.297 s
 
 4) This code base repos branch also has Xalan Java's XSLT 3.0 test drivers for W3C XSLT 3.0 test suite (located at, 
 https://github.com/w3c/xslt30-test).
diff --git a/src/main/java/org/apache/xalan/templates/ElemApplyTemplates.java b/src/main/java/org/apache/xalan/templates/ElemApplyTemplates.java
index f0ac8e76a..5493350d1 100644
--- a/src/main/java/org/apache/xalan/templates/ElemApplyTemplates.java
+++ b/src/main/java/org/apache/xalan/templates/ElemApplyTemplates.java
@@ -382,7 +382,7 @@ public class ElemApplyTemplates extends ElemCallTemplate
 				  XObject xObj = resultSeq.item(idx);
 				  if (xObj instanceof XMLNodeCursorImpl) {
 					  sourceNodes = ((XMLNodeCursorImpl)xObj).iterRaw();					  
-					  xslApplyTemplatesToNodes(transformer, xctxt, contextNode, sourceNodes, srcLocator);
+					  applyTemplatesToNodes(transformer, xctxt, contextNode, sourceNodes, srcLocator);
 				  }
 				  else {
 					  ResultSequence rSeq = new ResultSequence();
@@ -474,7 +474,7 @@ public class ElemApplyTemplates extends ElemCallTemplate
 	  }
 
 	  if (sourceNodes != null) {		 
-	     xslApplyTemplatesToNodes(transformer, xctxt, contextNode, sourceNodes, srcLocator);
+	     applyTemplatesToNodes(transformer, xctxt, contextNode, sourceNodes, srcLocator);
 	  }
   }
 
@@ -489,7 +489,7 @@ public class ElemApplyTemplates extends ElemCallTemplate
    * @param srcLocator                             XPath SourceLocator object instance
    * @throws TransformerException
    */
-  private void xslApplyTemplatesToNodes(TransformerImpl transformer, XPathContext xctxt, int contextNode,
+  private void applyTemplatesToNodes(TransformerImpl transformer, XPathContext xctxt, int contextNode,
 		                                                                        DTMCursorIterator sourceNodes, SourceLocator srcLocator) 
 		                                                                        		                                             throws TransformerException {
 	  
@@ -692,7 +692,7 @@ public class ElemApplyTemplates extends ElemCallTemplate
 
 			  final int exNodeType = dtm.getExpandedTypeID(child);
 
-			  final int nodeType = dtm.getNodeType(child);
+			  final short nodeType = dtm.getNodeType(child);
 			  
 			  String str2 = dtm.getNodeValue(child);			  			  
     		  if ((Constants.XSL_DOCUMENT_INSTRUCTION_MARKER).equals(str2)) {
@@ -708,7 +708,7 @@ public class ElemApplyTemplates extends ElemCallTemplate
 			  if (template == null)
 			  {
 				  String nodeTypeStr = null;
-
+				  
 				  switch (nodeType)
 				  {
 				  case DTM.DOCUMENT_FRAGMENT_NODE :
@@ -717,36 +717,8 @@ public class ElemApplyTemplates extends ElemCallTemplate
 					  if (elemMode != null) {						  						  
 						  String nodeNameStr = dtm.getNodeName(child);						  
 						  if (onNoMatchStr != null) {							  
-							  if ((Constants.ATTRVAL_TEXT_ONLY_COPY).equals(onNoMatchStr)) {
-								  template = sroot.getTextOnlyCopyRule(elemMode.getName(), nodeType, mode);								  								  								  
-							  }
-							  else if ((Constants.ATTRVAL_DEEP_COPY).equals(onNoMatchStr)) {
-								  template = sroot.getDeepCopyRule(elemMode.getName());
-							  }
-							  else if ((Constants.ATTRVAL_SHALLOW_COPY).equals(onNoMatchStr)) {
-								  template = sroot.getShallowCopyRule(elemMode.getName(), nodeType, mode);
-							  }
-							  else if ((Constants.ATTRVAL_DEEP_SKIP).equals(onNoMatchStr)) {
-								  template = sroot.getDeepSkipRule(elemMode.getName(), nodeType, mode);
-							  }
-							  else if ((Constants.ATTRVAL_SHALLOW_SKIP).equals(onNoMatchStr)) {
-								  template = sroot.getShallowSkipRule(elemMode.getName(), nodeType, mode);
-							  }							  
-							  else if ((Constants.ATTRVAL_FAIL).equals(onNoMatchStr)) {
-								  String errMesg = "XTDE0555 : An XSL template declaration could not be found to process an XML " + nodeTypeStr + " node";
-								  if (nodeNameStr != null) {
-									  errMesg = (errMesg + " '" + nodeNameStr + "'."); 
-								  }
-								  else {
-									  errMesg = (errMesg + "."); 
-								  }
-
-								  throw new TransformerException(errMesg, srcLocator);
-							  }
-							  else {
-								  throw new TransformerException("XTTE0505 : An XSL mode instruction's attribute \"on-no-match\" has disallowed "
-										  																		+ "value '" + onNoMatchStr + "'.", elemMode);
-							  }
+							  template = getDefaultTemplateRuleForMode(sroot, mode, elemMode, onNoMatchStr, nodeType, nodeTypeStr, 
+									  																							nodeNameStr, srcLocator);
 						  }
 
 						  if (elemMode.isWarningOnNoMatch()) {
@@ -774,36 +746,8 @@ public class ElemApplyTemplates extends ElemCallTemplate
 					  if (elemMode != null) {						  						  
 						  String nodeNameStr = dtm.getNodeName(child);
 						  if (onNoMatchStr != null) {
-							  if ((Constants.ATTRVAL_TEXT_ONLY_COPY).equals(onNoMatchStr)) {
-								  template = sroot.getTextOnlyCopyRule(elemMode.getName(), nodeType, mode);								  
-							  }
-							  else if ((Constants.ATTRVAL_DEEP_COPY).equals(onNoMatchStr)) {
-								  template = sroot.getDeepCopyRule(elemMode.getName());
-							  }
-							  else if ((Constants.ATTRVAL_SHALLOW_COPY).equals(onNoMatchStr)) {
-								  template = sroot.getShallowCopyRule(elemMode.getName(), nodeType, mode);
-							  }
-							  else if ((Constants.ATTRVAL_DEEP_SKIP).equals(onNoMatchStr)) {
-								  template = sroot.getDeepSkipRule(elemMode.getName(), nodeType, mode);
-							  }
-							  else if ((Constants.ATTRVAL_SHALLOW_SKIP).equals(onNoMatchStr)) {
-								  template = sroot.getShallowSkipRule(elemMode.getName(), nodeType, mode);
-							  }
-							  else if ((Constants.ATTRVAL_FAIL).equals(onNoMatchStr)) {
-								  String errMesg = "XTDE0555 : An XSL template declaration could not be found to process an XML " + nodeTypeStr + " node";
-								  if (nodeNameStr != null) {
-									  errMesg = (errMesg + " '" + nodeNameStr + "'."); 
-								  }
-								  else {
-									  errMesg = (errMesg + "."); 
-								  }
-
-								  throw new TransformerException(errMesg, srcLocator);
-							  }
-							  else {
-								  throw new TransformerException("XTTE0505 : An XSL mode instruction's attribute \"on-no-match\" has disallowed "
-										  																		+ "value '" + onNoMatchStr + "'.", elemMode);
-							  }
+							  template = getDefaultTemplateRuleForMode(sroot, mode, elemMode, onNoMatchStr, nodeType, nodeTypeStr, 
+									  																							nodeNameStr, srcLocator);
 						  }
 
 						  if (elemMode.isWarningOnNoMatch()) {
@@ -1247,6 +1191,7 @@ public class ElemApplyTemplates extends ElemCallTemplate
 							  }
 							  
 							  if (desiredTemplate != null) {
+								  // An XSL template with lower priority, is available in the stylesheet document
 								  xctxt.setSAXLocator(desiredTemplate);
 								  transformer.pushElemTemplateElement(desiredTemplate);
 								  ElemNextMatch elemNextMatch = (ElemNextMatch)t;
@@ -1260,7 +1205,62 @@ public class ElemApplyTemplates extends ElemCallTemplate
 								  
 								  desiredTemplate.setXslNextMatchWithParamList(xslWithParamList);								  
 								  desiredTemplate.execute(transformer);
-							  }							  
+							  }
+							  else {
+								  // Get default XSL stylesheet template rule
+								  
+								  String nodeTypeStr = null;
+								  
+								  switch (nodeType)
+								  {
+								  case DTM.DOCUMENT_FRAGMENT_NODE :
+								  case DTM.ELEMENT_NODE :
+									  nodeTypeStr = Constants.ELEMNAME_ELEMENT_STRING;					  
+									  if (elemMode != null) {						  						  
+										  String nodeNameStr = dtm.getNodeName(child);						  
+										  if (onNoMatchStr != null) {							  
+											  desiredTemplate = getDefaultTemplateRuleForMode(sroot, mode, elemMode, onNoMatchStr, nodeType, nodeTypeStr, 
+													                                                                                                   nodeNameStr, srcLocator);
+										  }
+
+										  if (elemMode.isWarningOnNoMatch()) {
+											  /**
+											   * Emit an, XSL stylesheet processing warning, when xsl:mode
+											   * instruction specifies an attribute 'warning-on-no-match' with
+											   * boolean value true.
+											   */							  
+											  emitXslTransformNoRuleMatchWarning(nodeNameStr, nodeTypeStr, xctxt, srcLocator);
+										  }
+
+										  if ((Constants.ATTRVAL_TEXT_ONLY_COPY).equals(onNoMatchStr)) {
+											  serializeXdmStringValueWithDefaultXslTextRule(transformer, rth, sroot, dtm, child, desiredTemplate);
+
+											  continue;
+										  }
+									  }
+									  else {
+										  desiredTemplate = sroot.getDefaultRule();
+									  }
+									  
+									  break;
+								  case DTM.CDATA_SECTION_NODE :
+								  case DTM.TEXT_NODE :
+								  case DTM.ATTRIBUTE_NODE :
+									  desiredTemplate = stylesheetRoot.getDefaultTextRule();
+									  break;
+								  case DTM.DOCUMENT_NODE :
+									  desiredTemplate = stylesheetRoot.getDefaultRootRule();          
+									  break;
+								  default :
+									  // no op
+								  }
+								  
+								  if (desiredTemplate != null) {
+									  xctxt.setSAXLocator(desiredTemplate);
+									  transformer.pushElemTemplateElement(desiredTemplate);
+									  desiredTemplate.execute(transformer);
+								  }
+							  }
 						  }
 						  else {
 							  xctxt.setSAXLocator(t);
@@ -1311,6 +1311,61 @@ public class ElemApplyTemplates extends ElemCallTemplate
 	  }
   }
 
+  /**
+   * Method definition, to get XSL default template rule object instance, 
+   * when a mode needs to be considered.
+   * 
+   * @param sroot                       An XSL stylesheet StylesheetRoot object
+   * @param mode                        Name of the mode
+   * @param elemMode                    An ElemMode object
+   * @param onNoMatchStr                Value of xsl:mode on-no-match attribute's value
+   * @param nodeType                    Value indicating the node's type
+   * @param nodeTypeStr                 The node's type as string value used for error handling
+   * @param nodeNameStr                 Node's name as string value
+   * @param srcLocator                  An XSL transformation SourceLocator object
+   * @return                            An XSL template rule ElemTemplate object instance
+   * @throws TransformerException
+   */
+  private ElemTemplate getDefaultTemplateRuleForMode(StylesheetRoot sroot, QName mode, ElemMode elemMode, String onNoMatchStr, 
+		                                                                              short nodeType, String nodeTypeStr,
+																					  String nodeNameStr, SourceLocator srcLocator) throws TransformerException {
+	
+	  ElemTemplate result = null;
+
+	  if ((Constants.ATTRVAL_TEXT_ONLY_COPY).equals(onNoMatchStr)) {
+		  result = sroot.getTextOnlyCopyRule(elemMode.getName(), nodeType, mode);								  								  								  
+	  }
+	  else if ((Constants.ATTRVAL_DEEP_COPY).equals(onNoMatchStr)) {
+		  result = sroot.getDeepCopyRule(elemMode.getName());
+	  }
+	  else if ((Constants.ATTRVAL_SHALLOW_COPY).equals(onNoMatchStr)) {
+		  result = sroot.getShallowCopyRule(elemMode.getName(), nodeType, mode);
+	  }
+	  else if ((Constants.ATTRVAL_DEEP_SKIP).equals(onNoMatchStr)) {
+		  result = sroot.getDeepSkipRule(elemMode.getName(), nodeType, mode);
+	  }
+	  else if ((Constants.ATTRVAL_SHALLOW_SKIP).equals(onNoMatchStr)) {
+		  result = sroot.getShallowSkipRule(elemMode.getName(), nodeType, mode);
+	  }							  
+	  else if ((Constants.ATTRVAL_FAIL).equals(onNoMatchStr)) {
+		  String errMesg = "XTDE0555 : An XSL template declaration could not be found to process an XML " + nodeTypeStr + " node";
+		  if (nodeNameStr != null) {
+			  errMesg = (errMesg + " '" + nodeNameStr + "'."); 
+		  }
+		  else {
+			  errMesg = (errMesg + "."); 
+		  }
+
+		  throw new TransformerException(errMesg, srcLocator);
+	  }
+	  else {
+		  throw new TransformerException("XTTE0505 : An XSL mode instruction's attribute \"on-no-match\" has disallowed value '" + onNoMatchStr + "'.", elemMode);
+	  }
+
+	  return result;
+	
+   }
+
   /**
    * Method definition, to do type checking and required modification of xsl:template's xsl:param
    * value using the XPath sequence type expression from xsl:param's 'as' attribute.
diff --git a/src/test/java/org/apache/xalan/tests/xslt3/XslNextMatchTests.java b/src/test/java/org/apache/xalan/tests/xslt3/XslNextMatchTests.java
index 96cbec807..7b44f94eb 100644
--- a/src/test/java/org/apache/xalan/tests/xslt3/XslNextMatchTests.java
+++ b/src/test/java/org/apache/xalan/tests/xslt3/XslNextMatchTests.java
@@ -23,7 +23,7 @@ import org.junit.BeforeClass;
 import org.junit.Test;
 
 /**
- * XSL stylesheet test case to test, xsl:next-match instruction.
+ * XSL 3 stylesheet test cases, to test xsl:next-match instruction.
  * 
  * @author Mukul Gandhi <mu...@apache.org>
  * 
@@ -77,5 +77,15 @@ public class XslNextMatchTests extends XslTransformTestsUtil {
         
         runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);   
     }
+    
+    @Test
+    public void xslNextMatchTest4() {
+        String xmlFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test2.xml"; 
+        String xslFilePath = XSL_TRANSFORM_INPUT_DIRPATH + "test4.xsl";
+        
+        String goldFilePath = XSL_TRANSFORM_GOLD_DIRPATH + "test4.out";                
+        
+        runXslTransformAndAssertOutput(xmlFilePath, xslFilePath, goldFilePath, null);   
+    }
 
 }
diff --git a/src/test/resources/xsl_next_match/gold/test4.out b/src/test/resources/xsl_next_match/gold/test4.out
new file mode 100644
index 000000000..bd6a85d7d
--- /dev/null
+++ b/src/test/resources/xsl_next_match/gold/test4.out
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?><root>
+   <!--Processing div--><div>
+     <p>p 1</p>
+     <p>p 2</p>
+   </div>
+   <!--Processing div--><div>
+     <p>p 3</p>
+   </div>
+</root>
diff --git a/src/test/resources/xsl_next_match/test2.xml b/src/test/resources/xsl_next_match/test2.xml
new file mode 100644
index 000000000..c88a3d289
--- /dev/null
+++ b/src/test/resources/xsl_next_match/test2.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<root>
+   <div>
+     <p>p 1</p>
+     <p>p 2</p>
+   </div>
+   <div>
+     <p>p 3</p>
+   </div>
+</root>
\ No newline at end of file
diff --git a/src/test/resources/xsl_next_match/test4.xsl b/src/test/resources/xsl_next_match/test4.xsl
new file mode 100644
index 000000000..86b8fbd17
--- /dev/null
+++ b/src/test/resources/xsl_next_match/test4.xsl
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"			    
+			    xmlns:xs="http://www.w3.org/2001/XMLSchema"
+			    exclude-result-prefixes="#all" 
+				expand-text="yes"
+				version="3.0">				
+				
+   <!-- Authors: mukulg@apache.org -->                              
+				
+   <!-- use with test2.xml -->
+    
+   <!-- An XSL 3 stylesheet test case, to test xsl:next-match instruction. -->					
+
+   <xsl:output method="xml" indent="yes"/>
+
+   <xsl:mode on-no-match="shallow-copy"/>
+
+   <xsl:template match="div">
+      <xsl:comment>Processing {node-name()}</xsl:comment>
+      <xsl:next-match/>
+   </xsl:template>
+
+   <xsl:template match="/">
+     <xsl:copy>
+       <xsl:apply-templates/>
+     </xsl:copy>
+   </xsl:template>
+   
+   <!--
+      * Licensed to the Apache Software Foundation (ASF) under one
+      * or more contributor license agreements. See the NOTICE file
+      * distributed with this work for additional information
+      * regarding copyright ownership. The ASF licenses this file
+      * to you under the Apache License, Version 2.0 (the "License");
+      * you may not use this file except in compliance with the License.
+      * You may obtain a copy of the License at
+      *
+      *     http://www.apache.org/licenses/LICENSE-2.0
+      *
+      * Unless required by applicable law or agreed to in writing, software
+      * distributed under the License is distributed on an "AS IS" BASIS,
+      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+      * See the License for the specific language governing permissions and
+      * limitations under the License.
+    -->
+
+</xsl:stylesheet>
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xalan.apache.org
For additional commands, e-mail: commits-help@xalan.apache.org