简体   繁体   中英

xquery regular expression…is there an easier way

Hi I am a newbie to xquery and the use of reguler expressions in xquery. I have a xml tag that I want to find a certain bit of it..Ie. somethingjpg but not have it look for the .jpg. The problem is that the somethingjpg isn't always in the same space..

Here is an xml example:

  <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..

  <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. 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

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

In particular, update replace expr with exprSingle documentation shows that

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

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_', '')

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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