XPath bilan #text tugun qiymati eng uzun bola bilan XML tugunini qanday qilib tanlashim mumkin?

XPathni ushbu so'rovni ishlatishdan oldin eng katta tamsayt id qiymatini tanlash uchun ishlatganman:

//somenode[not(@id <= preceding::somenode/@id) and not(@id <= following::somenode/@id)]

Men shunga o'xshash bir narsani qilishim mumkinligini umid qilgan edim:

//entry[not(string-length(child::text()) <= string-length(preceding::entry/child::text())) and not(string-length(child::text()) <= string-length(following::entry/child::text()))]

Ammo birgina o'rniga bitta tugunni qaytaradi.

Misol XML:


  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  Nam dignissim mi a massa mattis rutrum eu eget mauris.
  Ut at diam a sem scelerisque pretium nec pulvinar purus.
  Nunc in nisi nec dolor accumsan suscipit vel a quam.
  Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.
  Aenean eget ipsum et nunc eleifend scelerisque.
  In eu magna et diam volutpat molestie.
  In volutpat luctus mi, eu laoreet orci dictum vel.
  In mattis mi nec magna sodales eu bibendum felis aliquet.
<!-- etc for 800 more lines or so -->
  Duis auctor felis id neque gravida ut auctor ipsum ullamcorper.
  Sed vel tortor mauris, et aliquet tellus.

XPath test: http://chris.photobooks.com/xml/default.htm?state=1o

5

1 javoblar

The wanted element(s) cannot be selected with a single XPath 1.0 expression, because in XPath 1.0 it is not possible to apply a function to all selected nodes (string-length(someNodeSet) is applied only on the first node of this node-set). Another reason is that in XPath 1.0 it isn't possible to name and reference range variables.

In XPath 2.0 this is trivial:

/*/entry[not(string-length(.) < /*/entry/string-length(.))]

Yuqorida keltirilgan kodning maksimal uzunligi bo'lgan entry elementlarini tanlaydi.

/*/entry[not(string-length(.) < /*/entry/string-length(.))] [1]

Yuqorida keltirilgan kod ( entry elementi) birinchi (hujjat tartibida) tanlanadi.

XSLT 2.0 - based verification:

Ushbu konvertatsiya:



 
  
 

when applied on the provided xml document:


  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  Nam dignissim mi a massa mattis rutrum eu eget mauris.
  Ut at diam a sem scelerisque pretium nec pulvinar purus.
  Nunc in nisi nec dolor accumsan suscipit vel a quam.
  Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.
  Aenean eget ipsum et nunc eleifend scelerisque.
  In eu magna et diam volutpat molestie.
  In volutpat luctus mi, eu laoreet orci dictum vel.
  In mattis mi nec magna sodales eu bibendum felis aliquet.
<!-- etc for 800 more lines or so -->
  Duis auctor felis id neque gravida ut auctor ipsum ullamcorper.
  Sed vel tortor mauris, et aliquet tellus.

selects the entry elements (in this case only one) with the maximum string-length and outputs the selected elements:

Nunc suscipit lobortis arcu, nec adipiscing libero bibendum nec.
2
qo'shib qo'ydi
Ehtimol, ikkinchi so'rovda bir narsa yo'qolgan deb o'ylayman, ehtimol, [1] ?
qo'shib qo'ydi muallif travis, manba
Men bu so'rovni bajarish imkoniyatini topdim va u har bir tugunni qaytarish uchun paydo bo'ldi. Shunday qilib, ikkinchisi dokumentning birinchi tugunini qaytaradi. SaxonHE9.3N: C: \ Program Files \ Saxonica \ SaxonHE9.3N \ ming> so'rovlar -qs: "/ */entry [string-length (.)> =/*/Entry/string-length (. ]] [1] "-s: sample.xml <? xml versiyasi =" 1.0 "kodirovkalashni =" UTF-8 "?> Bu funksiyani qaytaradi.
qo'shib qo'ydi muallif travis, manba
Biroq, bu so'rov ishlamaydi ko'rinishida ishlaydi: C: \ Program Files \ Saxonica \ SaxonHE9.3N \ bin> query -qs: "// kirish [emas (string-length (child :: <= previous = input/string-length (child :: text ())) emas, balki (string-length (child :: text ()) <= following :: kirish/string-length (child: : text ())] "-s: sample.xml returns: <? xml version =" 1.0 "encoding =" UTF-8 "?> Natijada, not() ni olib tashlaganimda va belgilarni teskari aylantirishda u ishlamaydi.
qo'shib qo'ydi muallif travis, manba
Qani endi, u hozir ishlayapti, rahmat!
qo'shib qo'ydi muallif travis, manba
@travis: Yaxshi ohang - men kechqurun bir oz tashvishga tushmayapman. Tahrirlangan.
qo'shib qo'ydi muallif Dimitre Novatchev, manba
@lwburk: Tashakkur. Ha, vakillik test ma'lumotlarining etishmasligi, bizda mavjud bo'lgan eng katta muammolardan biridir. Ushbu muammoni hal qilishning bir yo'li birinchi savolni umumlashtirib, uni turli xil nurlarda ko'rishga harakat qilmoqda.
qo'shib qo'ydi muallif Dimitre Novatchev, manba
@travis: Kechirasiz, bu mantiqsiz xatolik bo'lishi kerak. Javobimni darhol tuzatdim - iltimos, hoziroq ko'ring.
qo'shib qo'ydi muallif Dimitre Novatchev, manba
@travis: Yangilangan javob to'g'ri ekanini men shaxsan tasdiqladim. Javob uchun XPath 2.0 ifodasini ishlatadigan XSLT kodi maksimal mag'lubiyat uzunligi bilan kirish elementini tanlash uchun qo'shilgan va ularni chiqaradi.
qo'shib qo'ydi muallif Dimitre Novatchev, manba
@travis: Xush kelibsiz. Ushbu muammoni ko'rsatganingiz uchun tashakkur. Ba'zan, juda kamdan-kam hollarda, men kecha kechqurun yoki ertalab juda ko'p savollarga javob beraman, yoki ish uchun shoshilaman va bu sog'ayib ketgan bo'lishi mumkin :)
qo'shib qo'ydi muallif Dimitre Novatchev, manba
+1 - "(string-length (someNodeSet) faqat ushbu tugunning birinchi tugmachasiga qo'llaniladi)" - men noto'g'ri xato qildim va keyin juda ko'p holatlarda aldashdi. Bu eng yaxshi javob.
qo'shib qo'ydi muallif Wayne Burkett, manba