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 2023/05/14 06:17:56 UTC

[xalan-java] branch xalan-j_xslt3.0 updated: implementing xsl:attribute element's 'select' attribute. xslt 1.0 didn't allow 'select' attribute on element xsl:attribute, but xslt 3.0 allows this.

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

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


The following commit(s) were added to refs/heads/xalan-j_xslt3.0 by this push:
     new 8183028d implementing xsl:attribute element's 'select' attribute. xslt 1.0 didn't allow 'select' attribute on element xsl:attribute, but xslt 3.0 allows this.
8183028d is described below

commit 8183028dd6784586cd44f642d78f5496cb0199d5
Author: Mukul Gandhi <ga...@gmail.com>
AuthorDate: Sun May 14 11:47:28 2023 +0530

    implementing xsl:attribute element's 'select' attribute. xslt 1.0 didn't allow 'select' attribute on element xsl:attribute, but xslt 3.0 allows this.
---
 src/org/apache/xalan/processor/XSLTSchema.java    |   4 +-
 src/org/apache/xalan/templates/ElemAttribute.java | 169 +++++++++++-----------
 2 files changed, 90 insertions(+), 83 deletions(-)

diff --git a/src/org/apache/xalan/processor/XSLTSchema.java b/src/org/apache/xalan/processor/XSLTSchema.java
index 4190f290..7040cbd7 100644
--- a/src/org/apache/xalan/processor/XSLTSchema.java
+++ b/src/org/apache/xalan/processor/XSLTSchema.java
@@ -223,7 +223,7 @@ public class XSLTSchema extends XSLTElementDef
                                                   XSLTAttributeDef.T_EXPR, false, false, XSLTAttributeDef.ERROR);
 
     // Optional.                                          
-    // xsl:variable, xsl:param, xsl:with-param                                       
+    // xsl:variable, xsl:param, xsl:with-param, xsl:attribute                                       
     XSLTAttributeDef selectAttrOpt = new XSLTAttributeDef(null, "select",
                                        XSLTAttributeDef.T_EXPR, false, false,XSLTAttributeDef.ERROR);
 
@@ -517,7 +517,7 @@ public class XSLTSchema extends XSLTElementDef
                                     charTemplateElements /* elements */,  // %char-template;>
                                     new XSLTAttributeDef[]{ nameAVTRequired,
                                                             namespaceAVTOpt,
-                                                            spaceAttr }, 
+                                                            spaceAttr, selectAttrOpt }, 
                                     new ProcessorTemplateElem(),
                                     ElemAttribute.class /* class object */, 20, true);
     XSLTElementDef xslCallTemplate =
diff --git a/src/org/apache/xalan/templates/ElemAttribute.java b/src/org/apache/xalan/templates/ElemAttribute.java
index 924f4406..cd9fc4e7 100644
--- a/src/org/apache/xalan/templates/ElemAttribute.java
+++ b/src/org/apache/xalan/templates/ElemAttribute.java
@@ -28,25 +28,36 @@ import org.apache.xml.serializer.NamespaceMappings;
 import org.apache.xml.serializer.SerializationHandler;
 import org.apache.xml.utils.QName;
 import org.apache.xml.utils.XML11Char;
-
+import org.apache.xpath.Expression;
+import org.apache.xpath.XPath;
+import org.apache.xpath.XPathContext;
+import org.apache.xpath.objects.XObject;
+import org.w3c.dom.DOMException;
 import org.xml.sax.SAXException;
 
 /**
- * Implement xsl:attribute.
- * <pre>
- * &amp;!ELEMENT xsl:attribute %char-template;>
- * &amp;!ATTLIST xsl:attribute
- *   name %avt; #REQUIRED
- *   namespace %avt; #IMPLIED
- *   %space-att;
- * &amp;
- * </pre>
- * @see <a href="http://www.w3.org/TR/xslt#creating-attributes">creating-attributes in XSLT Specification</a>
+ * XSLT 3.0 xsl:attribute element.
+ * 
+ * <xsl:attribute
+           name = { qname }
+           namespace? = { uri }
+           select? = expression
+           separator? = { string }
+           type? = eqname
+           validation? = "strict" | "lax" | "preserve" | "strip" >
+        <!-- Content: sequence-constructor -->
+   </xsl:attribute>
+
  * @xsl.usage advanced
  */
 public class ElemAttribute extends ElemElement
 {
     static final long serialVersionUID = 8817220961566919187L;
+    
+    /**
+     * The "select" expression.
+     */
+    protected Expression m_selectExpression = null;
 
   /**
    * Get an int constant identifying the type of element.
@@ -68,48 +79,26 @@ public class ElemAttribute extends ElemElement
   {
     return Constants.ELEMNAME_ATTRIBUTE_STRING;
   }
-
+  
   /**
-   * Create an attribute in the result tree.
-   * @see <a href="http://www.w3.org/TR/xslt#creating-attributes">creating-attributes in XSLT Specification</a>
-   *
-   * @param transformer non-null reference to the the current transform-time state.
+   * Set the "select" attribute.
    *
-   * @throws TransformerException
+   * @param xpath The XPath expression for the "select" attribute.
    */
-//  public void execute(
-//          TransformerImpl transformer)
-//            throws TransformerException
-//  {
-    //SerializationHandler rhandler = transformer.getSerializationHandler();
+  public void setSelect(XPath xpath)
+  {
+      m_selectExpression = xpath.getExpression();  
+  }
 
-    // If they are trying to add an attribute when there isn't an 
-    // element pending, it is an error.
-    // I don't think we need this check here because it is checked in 
-    // ResultTreeHandler.addAttribute.  (is)
-//    if (!rhandler.isElementPending())
-//    {
-//      // Make sure the trace event is sent.
-//      if (TransformerImpl.S_DEBUG)
-//        transformer.getTraceManager().fireTraceEvent(this);
-//
-//      XPathContext xctxt = transformer.getXPathContext();
-//      int sourceNode = xctxt.getCurrentNode();
-//      String attrName = m_name_avt.evaluate(xctxt, sourceNode, this);
-//      transformer.getMsgMgr().warn(this,
-//                                   XSLTErrorResources.WG_ILLEGAL_ATTRIBUTE_POSITION,
-//                                   new Object[]{ attrName });
-//
-//      if (TransformerImpl.S_DEBUG)
-//        transformer.getTraceManager().fireTraceEndEvent(this);
-//      return;
-//
-//      // warn(templateChild, sourceNode, "Trying to add attribute after element child has been added, ignoring...");
-//    }
-    
-//    super.execute(transformer);
-    
-//  }
+  /**
+   * Get the "select" attribute.
+   *
+   * @return The XPath expression for the "select" attribute.
+   */
+  public Expression getSelect()
+  {
+      return m_selectExpression;
+  }
   
   /**
    * Resolve the namespace into a prefix.  At this level, if no prefix exists, 
@@ -164,43 +153,61 @@ public class ElemAttribute extends ElemElement
    }
   
   /**
-   * Construct a node in the result tree.  This method is overloaded by 
-   * xsl:attribute. At this class level, this method creates an element.
+   * Construct an xsl:attribute node within the XSLT transformation's result tree.
    *
-   * @param nodeName The name of the node, which may be null.
-   * @param prefix The prefix for the namespace, which may be null.
-   * @param nodeNamespace The namespace of the node, which may be null.
-   * @param transformer non-null reference to the the current transform-time state.
-   * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
-   * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
+   * @param nodeName              The name of the node, which may be null.
+   * @param prefix                The prefix for the namespace, which may be null.
+   * @param nodeNamespace         The namespace of the node, which may be null.
+   * @param transformer           non-null reference to the current transform-time state.
    *
    * @throws TransformerException
    */
-  void constructNode(
-          String nodeName, String prefix, String nodeNamespace, TransformerImpl transformer)
-            throws TransformerException
-  {
-
-    if(null != nodeName && nodeName.length() > 0)
-    {
-      SerializationHandler rhandler = transformer.getSerializationHandler();
-
-      // Evaluate the value of this attribute
-      String val = transformer.transformToString(this);
-      try 
-      {
-        // Let the result tree handler add the attribute and its String value.
-        String localName = QName.getLocalPart(nodeName);
-        if(prefix != null && prefix.length() > 0){
-            rhandler.addAttribute(nodeNamespace, localName, nodeName, "CDATA", val, true);
-        }else{
-            rhandler.addAttribute("", localName, nodeName, "CDATA", val, true);
+  void constructNode(String nodeName, String prefix, String nodeNamespace, 
+                                                                 TransformerImpl transformer)
+                                                                            throws TransformerException {
+    
+        XPathContext xctxt = transformer.getXPathContext();
+    
+        if (null != nodeName && nodeName.length() > 0) {
+              SerializationHandler rhandler = transformer.getSerializationHandler();
+              
+              // XSLT 3.0 spec notes : the string value of the new attribute node may be defined either 
+              // by using the select attribute, or by the sequence constructor that forms the content of 
+              // the xsl:attribute element. These are mutually exclusive: if the select attribute is present 
+              // then the sequence constructor must be empty, and if the sequence constructor is non-empty  
+              // then the select attribute must be absent.              
+              if (m_selectExpression != null && this.m_firstChild != null) {
+                  throw new TransformerException("XTSE0840 : An xsl:attribute element with a select attribute must "
+                                                     + "be empty.", xctxt.getSAXLocator());   
+              }
+        
+              String val = null;
+                            
+              if (m_selectExpression != null) {
+                  // evaluate the value of xsl:attribute's "select" attribute
+                  XObject xpathEvalResult = m_selectExpression.execute(xctxt);              
+                  val = xpathEvalResult.str();
+              }
+              else {
+                  // evaluate the value of xsl:attribute element's child contents          
+                  val = transformer.transformToString(this);
+              }
+              
+              try {
+                    // let the result tree handler add the attribute and its String value.
+                    String localName = QName.getLocalPart(nodeName);
+                    if (prefix != null && prefix.length() > 0) {
+                        rhandler.addAttribute(nodeNamespace, localName, nodeName, "CDATA", val, true);
+                    }
+                    else {
+                        rhandler.addAttribute("", localName, nodeName, "CDATA", val, true);
+                    }
+              }
+              catch (SAXException e) {
+                   // no op
+              }
         }
-      }
-      catch (SAXException e)
-      {
-      }
-    }
+    
   }
 
 


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