I think the culprit is the line
 
    if (ps==null) 
               
This will be true only the first time the extension element is processed.
 

Michael Kay
Software AG
home: Michael.H.Kay@ntlworld.com
work: Michael.Kay@softwareag.com

 
-----Original Message-----
From: saxon-help-admin@lists.sourceforge.net [mailto:saxon-help-admin@lists.sourceforge.net] On Behalf Of Gerfen, Michael
Sent: 23 October 2002 05:59
To: saxon-help@lists.sourceforge.net
Subject: [saxon] Extension Element Problem

Using the SQL extension elements sample as a starting point, I've written an extension element that performs a lookup for a value based on a value passed as an attribute to the extension element.  I am experiencing a problem, in that the "process" method for the element is only getting called one time.  The lookup is performed successfully.  Each subsequent reference to the element returns the exact same lookup even though the attribute value is different.  I'm currently using SAXON 6.4.3.  I would be grateful if anyone could point out where I may be making a mistake.
 
Thanks!
 
Michael
 
My XSLT looks like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sql="http://trw.com/saxon/extensions/org.trw.webservices.sql.SQLElementFactory" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon" exclude-result-prefixes="sql">
 <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" indent="no"/>
 <xsl:param name="key"/>
 <xsl:template match="/">
       <xsl:message terminate="no">connectionKey =  <xsl:value-of select="$key"/> </xsl:message>
       <sql:connect connectionKey="{$key}" xsl:extension-element-prefixes="sql">
            <xsl:fallback>
                <xsl:message terminate="yes">SQL extensions are not installed</xsl:message>
            </xsl:fallback>
       </sql:connect>
       <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="@*|node()">
  <xsl:choose>
     <xsl:when test="local-name()='immunization' ">
        <immunization>
              <xsl:for-each select="./*">
                    <xsl:choose>
                         <xsl:when test="local-name()='hl7Code'">
                               <xsl:copy-of select="."/>
                               <xsl:message>hl7Code = <xsl:value-of select="."/></xsl:message>
                               <vaccine>
                                         <sql:acip hl7Code="{.}" xsl:extension-element-prefixes="sql"/>
                               </vaccine>
                         </xsl:when>
                          <xsl:otherwise>
                            <xsl:copy-of select="."/>
                          </xsl:otherwise>
                    </xsl:choose>
                 </xsl:for-each>
              </immunization>
           </xsl:when>
         <xsl:otherwise>
             <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
          </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
</xsl:stylesheet>
 
The original XML looks like this:
 
<recommendations status="RECOMMENDATIONS">
  <current>
    <immunization>
      <hl7Code>052</hl7Code>
      <date>10/22/2002</date>
    </immunization>
    <immunization>
      <hl7Code>045</hl7Code>
      <date>10/22/2002</date>
    </immunization>
    <immunization>
      <hl7Code>003</hl7Code>
      <date>10/22/2002</date>
    </immunization>
  </current>
  <future>
    <nextDueDate>12/03/2002</nextDueDate>
    <immunization>
      <hl7Code>045</hl7Code>
      <date>12/03/2002</date>
    </immunization>
  </future>
</recommendations>
 
The results of the transform look like this:
 
<recommendations status="RECOMMENDATIONS">
  <current>
    <immunization>
       <hl7Code>052</hl7Code>
       <vaccine>HepA(adult)</vaccine>
       <date>10/22/2002</date>
     </immunization>
     <immunization>
        <hl7Code>045</hl7Code>
        <vaccine>HepA(adult)</vaccine>
        <date>10/22/2002</date>
      </immunization>
      <immunization>
           <hl7Code>003</hl7Code>
           <vaccine>HepA(adult)</vaccine>
           <date>10/22/2002</date>
       </immunization>
   </current>
  <future>
    <nextDueDate>12/03/2002</nextDueDate>
    <immunization>
           <hl7Code>045</hl7Code>
           <vaccine>HepA(adult)</vaccine>
            <date>12/03/2002</date>
     </immunization>
   </future>
</recommendations>
 
Some output to the command window:
hl7Code = 052                                     <-- from xsl:message
VaccineLookup - hl7Code =052          <-- from System.out.println in the extension element
hl7Code = 045                                     <-- from xsl:meessage
hl7Code = 003                                     <-- from xsl:message
hl7Code = 045                                     <-- from xsl:message
 
And finally my extension element Java code looks like this:
 
import com.icl.saxon.*;
import com.icl.saxon.output.*;
import com.icl.saxon.style.*;
import com.icl.saxon.om.NodeInfo;
import com.icl.saxon.expr.*;
import org.xml.sax.SAXException;
import org.xml.sax.AttributeList;
import org.w3c.dom.Node;
import java.sql.*;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
/**
 *
 * This class provides an XSLT extension element that queries a database for the
 * vaccine associated with the hl7Code passed to the element.  It assumes that the connection to the database has been
 * open via &lt;sql:connect&gt;
 * @see SQLConnect
 * @author  mgerfen
 * @version 1.0.0
 */
public class VaccineLookup extends StyleElement{
    //Expression hl7Code;
    /**
    * Determine whether this node is an instruction.
    * @return true - it is an instruction
    */
    public boolean isInstruction() {
        return true;
    }
    /**
    * Determine whether this type of element is allowed to contain a template-body
    * @return true: yes, it may contain a template-body (this is done only so that
    * it can contain xsl:fallback)
    */
    public boolean mayContainTemplateBody() {
        return true;
    }
   
    public void prepareAttributes() throws TransformerConfigurationException
    {
        String hl7 = attributeList.getValue("hl7Code");
          if (hl7==null)
            reportAbsence("hl7Code");
        hl7Code = makeAttributeValueTemplate(hl7);
      
    }
    public void validate() throws TransformerConfigurationException {
        checkWithinTemplate();
    }
    public void process(com.icl.saxon.Context context) throws javax.xml.transform.TransformerException {
       
      
        Controller controller = context.getController();
        NodeInfo sourceDoc = (context.getCurrentNodeInfo()).getDocumentRoot();
        Connection connection = (Connection)controller.getUserData(
                                              sourceDoc, "sql:connection");
        if (connection==null) {
            throw styleError("No SQL connection has been established");
        }
        PreparedStatement ps =(PreparedStatement)controller.getUserData(
                                              this, "sql:statement");
        String vaccine = null;                                     
        try
        {
                if (ps==null)
                {

                   String hl7 = hl7Code..evaluateAsString(context);
                    System.out.println("VaccineLookup - hl7Code =" + hl7);
                    statement.append("SELECT vxcn.vxcn_shr_nm vaccine FROM phds_vaccines vxcn WHERE vxcn_hl7_cd = " + hl7 + "");
                    ps=connection.prepareStatement(statement.toString());
                    controller.setUserData(this, "sql:statement", ps);
                }       
                ResultSet rs = ps.executeQuery();
                Outputter out = controller.getOutputter();
                while (rs.next())
                {
                    vaccine = rs.getString("VACCINE");
                }
               
                out.writeContent(vaccine);
                rs.close();
                connection.commit();
           } catch (SQLException ex) {
                   throw styleError("(SQL) " + ex.getMessage());
           }
      }
   
}