counting all symbols "\"

i’m looking for a function that counts all “” symbols.

my xml-file looks like:

<files>
  <file>
  <owner>quatsch</owner> 
  <name>beispieldatei2</name> 
  <size>1024</size> 
  <extension>.doc</extension> 
  <creationTime>22.05.2010 16:50</creationTime> 
  <lastWriteTime>22.05.2010 16:51</lastWriteTime> 
  <lastAccessTime>22.05.2010 16:52</lastAccessTime> 
  <path>D:\Dokumente\beispieldatei2.doc</path> 
  <parentDirectory>D:\Dokumente</parentDirectory> 
  <rootDirectory>D:\</rootDirectory> 
  <directory>D:\</directory> 
  </file>
</files>

function searched like:

for $q in input()/files
return (count-string($q/file/path, '\')

returnvalue in this example: 2

thanks for any input.

See the following example:


let $string:="1234\56789\1x3\45"
return count
(for $i in 1 to string-length($string)
return substring($string,$i,1) [.="\"])

or


let $string:="1234\56789\1x3\45"
let $matches:=(for $i in 1 to string-length($string) return substring($string,$i,1) [.="\"])
return count ($matches)

or even cleaner


let $string:="1234\567891x3\45"
let $chars:=(for $i in 1 to string-length($string) return substring($string,$i,1))
return count ($chars [.="\"])

Thanks a lot for the reply.

I see, there is quit a better why than mine. In meantime i programmed a function:

declare namespace functx = "...";
declare function functx:index-of-string
( $arg as xs:string? ,
	$substring as xs:string )  as xs:integer* {
	
	if (contains($arg, $substring))
	then (string-length(substring-before($arg, $substring))+1,
	for $other in
		functx:index-of-string(substring-after($arg, $substring),
		$substring)
	return
	$other +
	string-length(substring-before($arg, $substring)) +
	string-length($substring))
	else ()
	} ;

so I can call the function like:

count(functx:index-of-string($path, '\'))