我将我的实体存储在eXist XML数据库中,并使用文件名(资源ID)作为实体的ID。
示例:
String xquery = "for $movie in collection('/db/movie')//movie "
+ "return $movie";
执行此查询后,我检索org.xmldb.api.base.Resource
实例,其内容用于创建实体。当我想设置这个实体的id时,我这样做:
dvd.setId(rs.getId());
问题在于,如果我执行这样的查询:
String xquery = "for $dvd in collection('/db/dvd')//dvd "
+ "return <dvd>"
+ "{$dvd/title}"
+ "{$dvd/type}"
+ "{"
+ "<content>"
+ " {"
+ " for $movie in $dvd/content//movie"
+ " let $movieIn := doc(concat(\"/db/movie/\", $movie/@id))/movie"
+ " return "
+ " <movie id=\"{$movie/@id}\">"
+ " {$movieIn/name}"
+ " {$movieIn/director}"
+ " {$movieIn/year}"
+ " {$movieIn/country}"
+ " {$movieIn/actors}"
+ " {$movieIn/genres}"
+ " </movie>"
+ " }"
+ "</content>"
+ "}"
+ "</dvd>";
rs.getId()
返回null
。我还尝试了来自this class的方法getDocumentId()
,但它也返回null
。有没有办法让它返回资源的id(这是存储实体的文件的名称)?
如果不可能,有没有办法(函数或其他)获取我正在使用的文件的文件名(我的意思是,数据库从中检索数据)和XQuery查询?
我尝试更换此行:
+ "return <dvd>"
用这个:
+ "return <dvd id=\"{$dvd}\">"
(这样我就可以从属性中获取文件的名称),但它不会返回文件名。
答案 0 :(得分:8)
由于您使用的是eXist-db,因此可以使用util:document-name()功能:
util:document-name($dvd)
答案 1 :(得分:7)
您可能正在寻找fn:base-uri()
。请参阅here。
答案 2 :(得分:0)
fn:base-uri
将返回整个URI而不是URI的最后一部分,但您可以将正则表达式解决方案与fn:base-uri
函数结合使用,以获得不依赖于特定于eXist的函数的解决方案
这个特殊的正则表达式使用 \。\ w + $ 修剪扩展名,并在捕获组2中捕获没有扩展名的文件名
declare namespace db="http://basex.org/modules/db";
declare namespace file="http://expath.org/ns/file";
declare variable $path as xs:string external;
let $docs := collection($path)
for $doc in $docs
return
let $file := replace(fn:base-uri($doc),'^(.*/)(.*?)\.\w+$','$2')
return db:replace('13F', $file, $doc)
或者对于扩展名为
的整个文件名 let $file := replace(fn:base-uri($doc),'^(.*/)(.*?\.\w+$)','$2')