title: How do I write a predicate for this in a single xpath?
body: <p>I'm trying to write a function to return a value based on attributes that adhere to a certain logic.</p>
  
  <pre><code>if &lt;Position isAdded="1"&gt; exists then this is what I need.
  
  if &lt;Position isAdded="1"&gt; doesn't exist, and &lt;Position isUpdated=1&gt; exists, that I need this element.
  
  I never should pull &lt;Position isDeleted=1&gt;
  
  If isAdded and isUpdated don't exist, then I need to get the value from 
  &lt;Position&gt; without any attributes.
  </code></pre>
  
  <p>This is what I've tried so far
      /Root/Effective_Change/Position[not(exists(@isDeleted))][exists(@isAdded)]</p>
  
  <p>get's me the isAdded element.
  I can add multiple choose when and test for every attribute. I'm just 
  trying to see if this can be done in a single xpath. Since there are a lot of elements in my xml, I don't want to do this for every element. Hence 
  trying to build a function.
  Like</p>
  
  <pre><code>/Root/Effective_Change/Position[if (exists(@isAdded then
       .[@isAdded]
  else if exists(@isUpdated) then 
  .[@isUpdated] else not(exists(@isDeleted))] 
  </code></pre>
  
  <p>Sample xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Root&gt;
      &lt;Effective_Change Sequence="1"&gt;
          &lt;Position isUpdated="1"&gt;
              &lt;Business_Title&gt;Updated Product Engineer&lt;/Business_Title&gt;
              &lt;Worker_Type&gt;Regular&lt;/Worker_Type&gt;
          &lt;/Position&gt;
          &lt;Position isAdded="1"&gt;
              &lt;Business_Title&gt;Product Engineer&lt;/Business_Title&gt;
              &lt;Worker_Type&gt;Regular&lt;/Worker_Type&gt;
          &lt;/Position&gt;
          &lt;Position isDeleted="1"&gt;
              &lt;Business_Title&gt;Deleted Product Engineer&lt;/Business_Title&gt;
              &lt;Worker_Type&gt;Regular&lt;/Worker_Type&gt;
          &lt;/Position&gt;
          &lt;Position&gt;
              &lt;Business_Title&gt;Product Engineer&lt;/Business_Title&gt;
              &lt;Worker_Type&gt;Regular&lt;/Worker_Type&gt;
          &lt;/Position&gt;
      &lt;/Effective_Change&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>The actual file can have these attributes at multiple levels. for 
  example:</p>
  
  <pre><code>&lt;Position isUpdated="1"&gt;
      &lt;Business_Title&gt;Updated Product Engineer&lt;/Business_Title&gt;
      &lt;Worker_Type isDeleted="1"&gt;Temporary&lt;/Worker_Type&gt;
      &lt;Worker_Type isAdded="1"&gt;Regular&lt;/Worker_Type&gt;
  &lt;/Position&gt;
  </code></pre>
  
  <p>I use this function to get non-deleted data</p>
  
  <pre><code>&lt;xsl:function name="this:getNonDeletedElement"&gt;
      &lt;xsl:param name="element"/&gt;
      &lt;xsl:variable name="nodeName"&gt;
          &lt;xsl:value-of select="$element[1]/name()"/&gt;
      &lt;/xsl:variable&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="exists($element/ancestor::*[name() = 'Effective_Change']//*[not(exists(@isDeleted))]/*[name() = $nodeName][not(exists(@isDeleted))])"&gt;
              &lt;xsl:value-of select="$element/ancestor::*[name() = 'Effective_Change']//*[not(exists(@isDeleted))]/*[name() = $nodeName][not(exists(@isDeleted))]"/&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:value-of select="''"/&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:function&gt;
  </code></pre>
  
  <p>This function, however, would still return isAdded, isUpdated and non-attributed rows. In that case, if isAdded exists, I need that, if isAdded doesnt exist and isUpdated does, then I need that, else the non-attributed row.</p>
  
  <p>I'm using this to check for isAdded</p>
  
  <pre><code>&lt;xsl:function name="this:isElementAdded"&gt;
      &lt;xsl:param name="element"/&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="$element/../@isAdded or $element/@isAdded or exists($element/ancestor::*[@isAdded and name(..)='Effective_Change'])"&gt;
              &lt;xsl:value-of select="true()"/&gt;                                                 
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:value-of select="false()"/&gt;                        
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:function&gt; 
  </code></pre>
  
  <p>I'm wondering if there's a simpler way to doing this.</p>