CRM Open Source Business & Social CRM Software

Results 1 to 1 of 1

Thread: Using soap XML / XSLT

  1. #1
    spiderglobe is offline Junior Member
    Join Date
    Dec 2008
    Posts
    3

    Default Using soap XML / XSLT

    After searching the forums for a good tutorial about using the sugarcrm soap interface and using XSL stylesheets I found no examples. I needed to connect the users within a Joomla environment with the contact // acount details of the SugarCRM enivornment

    Hereby my first contribution to the Sugar community.

    The purpose of this tutorial is to provide an example of how you can use the SugarCRM SOAP and using XSL stylesheets for the presentation. In this example I will fetch a record from the contacts and present it with an XSL stylesheet.


    Step 1 is to make a soap connection to the SUGAR CRM environment and request the contact record:
    PHP Code:
    require_once ( JPATH_BASE DS 'nusoap' DS 'lib' DS 'nusoap.php'    );
    $client = new nusoap_client$this->portal_config->sugarhost '/soap.php?wsdl'true    );

    $auth_array = array( 'user_auth' => array ('user_name' => $this->portal_config->sugar_username,
                                         
    'password' => md5($this->portal_config->sugar_password),
                                    )
                                );
    $login_results $client->call('login',$auth_array);
    $sugar_session_id $login_results['id'];

    // The sugar_crm user is ID is stored in the J user table parameters;
    $contact_record $client->call'get_entry', array( $sugar_session_id'Contacts'$sugar_crm_user_idnull));


    // Load the xsl stylesheet
    $xsl_file 'contacts.xslt';
    $XSL = new DOMDocument();
    $XSL->load$xsl_file );
    $xp->importStylesheet$XSL );

    $xml_doc = new DomDocument();
    $xml_doc->loadXML$client->responseData )

    // transform the XML into HTML using the XSL file
    if ($html $xp->transformToXML($xml_doc)) {
        echo 
    $html;
    } else {
        
    trigger_error('XSL transformation failed.'E_USER_ERROR);
    }
    // Thats all folks ;-) 
    The next step is to generate the HTML code through the XSL stylesheet, that is the hard part. The main issue is how you can fetch the specific attributes from the soap xml response data.

    To make it easy you can define within your XSL stylesheet so called keys for the lookup of the specific attributes. In this example I need the label and the value from the specific attribute. Thereby you can define a key as follows:
    HTML Code:
    <!-- define a key for the value -->
    <xsl:key name="myvalue" match="item//name_value_list//item" use="name"  />
    <!-- define a key for the label  -->
    <xsl:key name="mylabel" match="field_list//item" use="name"  />
    further on in your template you can fetch a specific field with the following action in the style sheet, in this case I need the user assigned user name:
    HTML Code:
    <xsl:value-of select="key('mylabel', 'assigned_user_name')/label" /> : <xsl:value-of select="key('myvalue', 'assigned_user_name')/value" />
    Notice the fact that I using the key 'name' myvalue and mylabel to look up the value of the assigned_user_name.

    See below the whole stylesheet:
    HTML Code:
    <?xml version='1.0'?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:output method='html'/>
    
    <!-- param values may be changed during the XSL Transformation -->
    <xsl:param name="title">Dumping the contact record</xsl:param>
    
    <!-- define the keys for looking up the attributes of the contact record -->
    
    <xsl:key name="myvalue" match="item//name_value_list//item" use="name"  />
    <xsl:key name="mylabel" match="field_list//item" use="name"  />
    
    <xsl:template match="/">
    
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title><xsl:value-of select="$title"/></title>
    </head>
    <body>
       Below is an example of fetching an atribute and the label from Sugar:<br/>
       <xsl:value-of select="key('mylabel', 'assigned_user_name')/label" /> : <xsl:value-of select="key('myvalue', 'assigned_user_name')/value" />
       <hr/>
       Below is an example of looping the attributes from the requested value email1<br/>
       <xsl:for-each select="key('myvalue', 'email1')" >
     	 	<xsl:value-of select="name"/> : <xsl:value-of select="value"/>
      </xsl:for-each>
      <hr/>
      <div class="center">
    
      <table border="0">
        <caption><xsl:value-of select="$title"/></caption>
        <thead>
          <tr>
            <th>Field name</th>
            <th>Value</th>
          </tr>
        </thead>
    
        <tbody>
          <xsl:apply-templates select="//name_value_list" />
        </tbody>
    
      </table>
    
      <!-- insert the page navigation links -->
    
      </div>
    </body>
    </html>
    
    </xsl:template>
    
    <xsl:template match="name_value_list">
    
      <xsl:for-each select="item">
      <tr>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="value"/></td>
      </tr>
      </xsl:for-each>
    
    </xsl:template>
    
    </xsl:stylesheet>
    The style sheet also makes a dump of the provide record of the contact person.

    See also the attached XML file if you want to test it out local (I recommend this when you are developing since this is much much faster then using the nusoap interface). This file is called sugar_contact.xml. If you want to test it out replace the load of the xml file with the lines below:
    PHP Code:
    $xml_doc = new DomDocument;
    $xml_doc->load'sugar_contact.xml' ); 
    regards,

    Richard
    Attached Files Attached Files
    Last edited by spiderglobe; 2008-12-01 at 05:36 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Set_entry XML fields empty on SOAP call
    By jsgoecke in forum Developer Help
    Replies: 2
    Last Post: 2008-11-28, 08:05 PM
  2. XML error parsing SOAP payload
    By fedepia in forum Help
    Replies: 0
    Last Post: 2008-06-11, 01:04 PM
  3. Replies: 1
    Last Post: 2006-12-11, 05:35 PM
  4. xml parsing error under soap
    By sugarcare in forum Developer Help
    Replies: 8
    Last Post: 2006-10-27, 01:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •