简体   繁体   English

xquery正则表达式...有没有更简单的方法

[英]xquery regular expression…is there an easier way

Hi I am a newbie to xquery and the use of reguler expressions in xquery. 嗨,我是xquery的新手,并且在xquery中使用了调节器表达式。 I have a xml tag that I want to find a certain bit of it..Ie. 我有一个xml标记,我想找到它的某个位置。 somethingjpg but not have it look for the .jpg. somethingjpg,但找不到.jpg。 The problem is that the somethingjpg isn't always in the same space.. 问题是somethingjpg并不总是在同一空间内。

Here is an xml example: 这是一个xml示例:

  <book title="Harry Potter">
    <description
   xlink:type="simple"
   xlink:href="http://book.com/2012/12/28/20121228-there_is_somethingjpg_123456789a1s23e4.jpg"
  xlink:show="new">
 As his fifth year at Hogwarts School of Witchcraft and
  Wizardry approaches, 15-year-old Harry Potter is.......
    </description>
   </book>

or the xlink:href can be like this.. 或xlink:href可以像这样。

  <book title="Harry Potter">
    <description
   xlink:type="simple"
   xlink:href="http://book.com/2012/12/28/20121228-there_is_always_more_to_somethingelsejpg_123456789a1s23e4.jpg"
  xlink:show="new">
 As his fifth year at Hogwarts School of Witchcraft and
  Wizardry approaches, 15-year-old Harry Potter is.......
    </description>
   </book>

What I am trying to achieve (if it even possible) is a piece of xquery code that will look for that somethingjpg or somethingelsejpg. 我试图实现的目标(如果可能的话)是一段x​​query代码,该代码将查找somethingjpg或somethingelsejpg。 then fix the somethingjpg or somethingelsejpg to just say something or somethingelse, concat the link all together again and replace the new link over the old link in an eXist-db 然后修复somethingjpg或somethingelsejpg只是说些什么或其他东西,再次将链接合并在一起,然后将新链接替换为eXist-db中的旧链接

Code wise I have.. 我有代码明智的..

let $a := collection('/db/articles/')//book
for $b in $a//@xlink:href[contains(.,'jpg_')]
let $c := tokenize(replace($b, 'Some sort of redex I can't figure out', '$1;$2;$3'), ';')
let $d := $c[2]
let $e := replace(substring-before($d, 'jpg_'). '_')
let $f := concat ($c[1]. $e, $c[3])
return update replace $b with $f

I just can't figure out the rest...Help!! 我只是想不通其余的...帮助!

You probably want to use the XQuery Update facility of eXist 您可能要使用eXistXQuery Update工具

In particular, update replace expr with exprSingle documentation shows that 特别是, update replace expr with exprSingle文档update replace expr with exprSingle显示

If it [expr] is an attribute or text node, the value of the attribute or the text node is set to the concatenated string values of all nodes in exprSingle 如果[expr]是属性或文本节点,则将属性或文本节点的值设置为exprSingle中所有节点的串联字符串值

So find the attribute nodes whose values contain the 'jpg_' string just as you did, and then simply replace 'jpg_' with the empty string (you don't even need a regex): replace($attr, 'jpg_', '') 因此,就像查找值一样,找到其值包含'jpg_'字符串的属性节点,然后简单地将'jpg_'替换为空字符串(您甚至不需要正则表达式): replace($attr, 'jpg_', '')

for $attr in $a//@xlink:href[contains(.,'jpg_')]
  let $newval = replace ($attr, 'jpg_', '')
    return update replace $attr with $newval

Also see the XQuery Update facility documentation (which of course eXist may or may not fully implement, though it seems it supports enough) 另请参阅XQuery Update工具文档 (当然,eXist可能会或可能不会完全实现,尽管它似乎足够支持)

It's not perfectly clear what exactly you want to replace and why you were trying to tokenize -- you need to add more details in case you don't want to simply delete 'jpg_' from attribute values. 目前尚不清楚您确切要替换的内容以及为何要标记化的原因-您需要添加更多详细信息,以防您不想简单地从属性值中删除“ jpg_”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM