spss.EvaluateXPath Function (Python)

spss.EvaluateXPath(handle,context,xpath). Evaluates an XPath expression against a specified XPath DOM and returns the result as a list. The argument handle specifies the particular XPath DOM and must be a valid handle name defined by a previous spss.CreateXPathDictionary function or IBM® SPSS® Statistics OMS command. The argument context defines the XPath context for the expression and should be set to "/dictionary" for a dictionary DOM or "/outputTree" for an output XML DOM created by the OMS command. The argument xpath specifies the remainder of the XPath expression and must be quoted.

Example

#retrieve a list of all variable names for the active dataset. 
handle='demo' 
spss.CreateXPathDictionary(handle)
context = "/dictionary" 
xpath = "variable/@name" 
varnames = spss.EvaluateXPath(handle,context,xpath)

Example

*Use OMS and a Python program to determine the number of uniques values
  for a specific variable.
OMS SELECT TABLES 
  /IF COMMANDs=['Frequencies'] SUBTYPES=['Frequencies']
 /DESTINATION FORMAT=OXML XMLWORKSPACE='freq_table'.
FREQUENCIES VARIABLES=var1.
OMSEND.

BEGIN PROGRAM.
import spss
handle='freq_table'
context="/outputTree"
#get rows that are totals by looking for varName attribute
#use the group element to skip split file category text attributes
xpath="//group/category[@varName]/@text" 
values=spss.EvaluateXPath(handle,context,xpath)
#the "set" of values is the list of unique values
#and the length of that set is the number of unique values
uniqueValuesCount=len(set(values))
END PROGRAM.

Note: In the IBM SPSS Statistics documentation, XPath examples for the OMS command use a namespace prefix in front of each element name (the prefix oms: is used in the OMS examples). Namespace prefixes are not valid for EvaluateXPath.

Documentation for the output schema and the dictionary schema is available from the Help system.