Code to Evaluate an Expression Stored as an XML Fragment

This section describes how to parse and evaluate an expression stored as an Extensible Markup Langauge (XML) fragment. An XML fragment is equivalent to the ExprBody field of the expression Recordset object. The XML fragment is first parsed for syntactic correctness. This is only performed at design-time since the EvalXML method also parses the fragment. If the expression parses correctly, it is then evaluated.

  1. Create an XML expression fragment. This expression will return True when evaluated against a context containing a UserObject object and an Address object, where the first_name property of the UserObject object = "Joe" AND the region_code of the Address object = "WA". In this example, the string representing the expression is not formatted properly for scripting with respect to quotation marks and continuation in order to increase readability.

    Dim strXMLExpression
    
    sXMLExpression =
     "<TERM TYPE="and">
        <CLAUSE OPER="equal">
           <PROPERTY ID="UserObject.GeneralInfo.first_name" TYPE="string" />
           <IMMED-VAL TYPE="string">Joe</IMMED-VAL>
        </CLAUSE>
        <CLAUSE OPER="equal">
           <PROPERTY ID="Address.GeneralInfo.region_code" TYPE="string" />
           <IMMED-VAL TYPE="string">WA</IMMED-VAL>
        </CLAUSE>
      </TERM>"
                   
    
  2. Create an ExpressionEval object and parse the string to determine if it represents a syntactically valid XML expression.

    Dim bResult
    Dim oExpressionEval
    Set oExpressionEval = CreateObject("Commerce.ExpressionEvaluator")
    
    bResult = oExpressionEval.ParseXML(sXMLExpression)
    

The result, bResult, equals True since this fragment is syntactically valid.

  1. Evaluate the XML fragment against the data contained in the dictProfiles context created in Code to Create an Evaluation Context. The properties of interest in the dictProfiles context are:

    User.GeneralInfo.first_name = "Joe"
    Address.GeneralInfo.region_code = "WA"
    
    Dim vResult
    vResult = oExpressionEval.EvalXML(sXMLExpression, dictProfiles)
    
    Set oExpressionEval = Nothing
    

The result of evaluating the expression, vResult, is True.


All rights reserved.