<?php
$xpath = '//node[@cat="top" and node[@rel="--" and @cat="smain" and node[@rel="su" and @pt="vnw" and @word="Dit" and @lemma="dit" and number(@begin) < ../node[@rel="hd" and @pt="ww" and @lemma="zijn"]/number(@begin)] and node[@rel="hd" and @pt="ww" and @lemma="zijn" and number(@begin) < ../node[@rel="predc" and @cat="np"]/node[@rel="det" and @pt="lid" and @word="een" and @cs="no" and @lemma="een"]/number(@begin)] and node[@rel="predc" and @cat="np" and node[@rel="det" and @pt="lid" and @word="een" and @cs="no" and @lemma="een" and number(@begin) < ../node[@rel="hd" and @pt="n" and @cs="no" and @lemma="zin"]/number(@begin)] and node[@rel="hd" and @pt="n" and @cs="no" and @lemma="zin" and number(@begin) < ../../../node[@rel="--" and @pt="let"]/number(@begin)]]] and node[@rel="--" and @pt="let"]]';
$xpath = applyCs($xpath);
var_dump($xpath);
  function applyCs($xpath) {
    if (strpos($xpath, '@cs="no"') !== false) {
      preg_match_all("/(?<=node\[).*?(?=node\[|\])/", $xpath, $matches);
      
      foreach ($matches[0] as $match) {
        if (strpos($match, '@cs="no"') !== false) {
          $dummyMatch = preg_replace('/(?: and )?@cs="no"/', '', $match);

            if (strpos($dummyMatch, '@word="') !== false) {
                $dummyMatch = str_replace('@word="', 'lower-case(@word)="', $dummyMatch);
            }
            if (strpos($dummyMatch, '@lemma="') !== false) {
                $dummyMatch = str_replace('@lemma="', 'lower-case(@lemma)="', $dummyMatch);
            }
            $xpath = preg_replace('/'.preg_quote($match, '/').'/', $dummyMatch, $xpath, 1);
            var_dump($dummyMatch);
        }
      }
    }
    return $xpath;
  }
