0

我想在 Marklogic 服务器中获取上传文件的文件扩展名。我知道如何获取文件名。但这给出了文件名和扩展名,如 new.txt。但我只想要扩展名而不是完整的文件名。我怎样才能得到文件扩展名?

4

3 回答 3

2

从文件名获取文件扩展名的方法有很多。例如,您可以使用functx:substring-after-last($filename, '.')或其他方法(fn:substring-after)在点之后获取子字符串。请参阅:xqueryfunctions.com

附言fn:tokenize($filename, '\.')[fn:last()]

于 2012-07-02T09:05:30.877 回答
1

我经常使用以下替换:

fn:replace("c:\a\b\c.d.e.txt", '^(.*\.)?([^\.]+)$', '$2')

但正如 Andrew 所建议的那样,使用 functx 也是一个好主意。functx 库的副本作为 MarkLogic 后期版本的一部分分发。只需添加以下导入即可使它们可用:

import module namespace functx = "http://www.functx.com" at "/MarkLogic/functx/functx-1.0-nodoc-2007-01.xqy";

于 2012-07-02T11:09:01.047 回答
0

只是为了多样性,另一个产生扩展的表达式:):

reversed-string(substring-before(reversed-string($filePath), '.'))

其中reversed-string($s)可以定义为:

codepoints-to-string(reverse(string-to-code-points($s)))

所以带有替换的整个表达式是:

codepoints-to-string(
             reverse(
              string-to-codepoints(
               substring-before(codepoints-to-string(reverse(string-to-codepoints($filePath))),
                               '.')
                                   )
                     )
                              )
于 2012-07-02T13:17:08.903 回答