XQuery在定界符之间获取字符串

发布于 2025-01-23 08:11:09 字数 446 浏览 2 评论 0原文

我有(tei)xml,有像 < ref target =“#a1”/>,但< ref target =“#a1#b2#c3”/> 我需要编写Xquery代码以将每个 -target转换为链接;如果我只有一个target =“#a1”,那么这没问题,然后我使用substring-fer($ node/@target,'#')并寻找XML-ID与子字符串相同,但是如果子字符串比所需的时间长,当然我会遇到问题,则无匹配。 有没有一种方法可以在Space之间选择substring? (我是Xquery的新手,很抱歉,如果我问一些非常明显的事情,但找不到经济解决方案)

I have (TEI)XML with bits like
<ref target="#a1"/> but also <ref target="#a1 #b2 #c3"/>
I need to write the xquery code to transform each #-target to a link; this is no problem if I have just one target="#a1", then I use substring-after($node/@target, '#') and look for xml-ids that are the same as the substring, but then of course I get problems if the substring is longer than needed, no match is possible.
Is there a way to select the substring between # and space or # and "?
(I am very new to xquery so sorry if I am asking something very obvious, but I could not find an economic solution)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2025-01-30 08:11:09

您可以 tokenize() 可选空间的属性值\ s带有此Regex \ s?#,然后用谓词过滤出一个空项目使用normalize-space()或测试字符串长度字符串长度(。)gt 0

let $ref :=  <ref target="#a1 #b2 #c3"/>
let $targets := tokenize($ref/@target, '\s?#')[normalize-space()]
return  
  $targets

或者您可以通过Space \ S或空间和#\ s#,然后 translate() 剩下的任何 nothens:

let $ref :=  <ref target="#a1 #b2 #c3"/>
let $targets := tokenize($ref/@target, '\s#') ! translate(., '#', '')
return  
  $targets

读取空间分离ref/@target属性值作为序列的另一种方法是将它们读为xs:nmtokens(它们是空间分离值),然后您只需要担心从每个值中删除

let $ref :=  <ref target="#a1 #b2 #c3"/>
let $target-tokens as xs:NMTOKENS := $ref/@target
let $targets :=  $target-tokens ! translate(., '#', '')
return
  $targets 

You could tokenize() the attribute value by an optional space \s and # with this regex \s?# and then filter out an empty item with a predicate using normalize-space() or testing the string length string-length(.) gt 0:

let $ref :=  <ref target="#a1 #b2 #c3"/>
let $targets := tokenize($ref/@target, '\s?#')[normalize-space()]
return  
  $targets

or you could tokenize by space \s or space and # \s#, then translate() any remaining # into nothing:

let $ref :=  <ref target="#a1 #b2 #c3"/>
let $targets := tokenize($ref/@target, '\s#') ! translate(., '#', '')
return  
  $targets

Another way to read the space separated ref/@target attribute values as a sequence is to read them as xs:NMTOKENS (which are space separated values), and then you just need to worry about removing the # from each value:

let $ref :=  <ref target="#a1 #b2 #c3"/>
let $target-tokens as xs:NMTOKENS := $ref/@target
let $targets :=  $target-tokens ! translate(., '#', '')
return
  $targets 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文