问题
I want to get the file extension of uploaded file in Marklogic server. I know how to get the file name. But that gives filename plus extension like new.txt. But I want only extension not the full file name. How can I get just the file extension ?
回答1:
There are many methods of getting file extensions from filename. For instance you can use functx:substring-after-last($filename, '.')
or other methods (fn:substring-after)
of getting substring after dot. Please see: xqueryfunctions.com
P.S. fn:tokenize($filename, '\.')[fn:last()]
回答2:
I often use the following replace:
fn:replace("c:\a\b\c.d.e.txt", '^(.*\.)?([^\.]+)$', '$2')
But using functx is a good idea too, as suggested by Andrew. A copy of the functx library is distributed as part of the latter versions of MarkLogic. Just add the following import to get them available:
import module namespace functx = "http://www.functx.com" at "/MarkLogic/functx/functx-1.0-nodoc-2007-01.xqy";
HTH!
回答3:
Just for variety, yet another expression that produces the extension :) :
reversed-string(substring-before(reversed-string($filePath), '.'))
where reversed-string($s)
can be defined as:
codepoints-to-string(reverse(string-to-code-points($s)))
So the whole expression with the substitution is:
codepoints-to-string(
reverse(
string-to-codepoints(
substring-before(codepoints-to-string(reverse(string-to-codepoints($filePath))),
'.')
)
)
)
来源:https://stackoverflow.com/questions/11290464/how-to-get-file-extension-in-marklogic-server