comparison Expressions

The comparison expression implements a simple conditional choice for expression sequences. It has the following general form:

<comparison [expr=[“=|!=”]] lhs="..." rhs="..." success="..." failure="...">
        <arg name="lhs">...</arg>
        <arg name="rhs">...</arg>
        <arg name="success">...</arg>
        <arg name="failure">...</arg>
     </comparison>

Notice that unlike other expressions, the arguments are identified by name rather than by position. The names are not optional, and the order of the arguments does not matter. All arguments/attributes are optional. The expr attribute defaults to the string ‘=’, but all other attributes and arguments default to the empty string ““.

The meaning of the comparison expression is to perform a simple conditional choice. The arguments/attributes ‘lhs’ and ‘rhs’ are abbreviations for ‘left-hand side’ and ‘right-hand side’. These two expressions are evaluated and compared. The comparison type is determined by the ‘expr’ attribute. If the expr attribute is ‘=’ and the comparison is true (that is, if the left hand side is equal to the right hand side), then the success argument is evaluated and returned, otherwise the failure argument is evaluated and returned. If the expr attribute is ‘!=’, for ‘not equal’, then the sense of the comparison is reversed; i.e. if the comparison is true, then failure is evaluated, otherwise success is evaluated.

Note: This is not a flow-of-control conditional expression, but rather simply chooses between expression arguments to evaluate.

The example below demonstrates a case in which news articles are tagged with their source language, ‘fr’ for ‘French’ and ‘en’ for English. Suppose that we want to extract all the author’s names, one per feature. But since the french word for ‘name’ is ‘nom’ (and ‘histoire’ for ‘story’), we want to selectively extract the author’s name based on the stories source language. We do this by assigning an attribute to a feature, and setting that attribute’s value as a comparison expression that chooses based on the ‘lang’ attribute.

Example

stories.xml

<?xml version="1.0" encoding="UTF-8"?>
<entries>
   <entry lang=”en”>
      <name>Mark</name>
      <story> ... </story>
   </entry>
<entry lang=”fr”>
      <nom>Jean-Sebastian</nom>
      <histoire> ... </histoire>
   </entry>
</entries>

stories.xmp

<?xml version="1.0" encoding="UTF-8"?>
<xfmap> 
   <feature-map>
      <mapping match="entry">
         <feature-type><literal expr="author"/></feature-type>
         <attributes>
            <attribute>
               <name><literal expr=”author”/></name>
<!-- Here we check if the ‘lang’ attribute is equal to the string ‘Name’ and if 
it is, we extract the Name element. Otherwise, we extract the Nom element -->
               <value>
                  <comparison lhs="en">
                     <arg name="rhs"><extract expr="@lang"/></arg>
                     <arg name="success"><extract expr="./Name"/></arg>
                     <arg name="failure"><extract expr="./Nom"/></arg>
                  </comparison>
               </value>
            </attribute>
         </attributes>
      </mapping>
   </feature-map>
</xfmap>
==========================================

      Features Constructed	

|INFORM|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|INFORM|Feature Type: `entry'
|INFORM|Attribute(string): `author' has value `Mark'
|INFORM|Attribute(string): `xml_type' has value `xml_no_geom'
|INFORM|Geometry Type: Unknown (0)
|INFORM|=============================================================
|INFORM|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|INFORM|Feature Type: `entry'
|INFORM|Attribute(string): `author' has value `Jean-Sebastian'
|INFORM|Attribute(string): `xml_type' has value `xml_no_geom'
|INFORM|Geometry Type: Unknown (0)
|INFORM|=============================================================