1

I need the title and position of the books whose title begins between "M" and "Y".

XML document:

<books>
    <book release="2003" edition="1">
        <title>XML Advanced</title>
        <author>
            <lastname>Smith</lastname>
            <name>Rosa</name>
        </author>
    </book>
    <book release="2002" edition="1">
        <title>Learning XPath</title>
        <author>
            <lastname>Taylor</lastname>
            <name>Claudia</name>
        </author>
    </book>
    <book release="2022" edition="2">
        <title>DTD</title>
        <author>
            <lastname>Jones</lastname>
            <name>Robert M.</name>
        </author>
    </book>
    <book release="2020" edition="2">
        <title>XSLT Professional</title>
        <author>
            <lastname>Jones</lastname>
            <name>David</name>
        </author>
    </book>
    <book release="2021" edition="2">
        <title>Xquery</title>
        <author>
            <lastname>Lewis</lastname>
            <name>Martha</name>
        </author>
    </book>
</books>

XQuery. I tried something like this, but I don't know yet how to filter it.

 for $tit at $posit in doc("books.xml")/books/book/title

 return concat($posit, ".- ", $tit)
Cryptter
  • 45
  • 3

2 Answers2

3

After looking at this answer:

//*[matches(title,'^[M-Y]')]/title

results in :

XML Advanced
XSLT Professional
Xquery

The regular expression ^[M-Y], matches the titles that begin with a letter between M and Y, see: regex101

Luuk
  • 12,245
  • 5
  • 22
  • 33
-1

Ok, I don't know if it's the best way but this does the job: where $tit >= "M" and $tit <= "Y"

Cryptter
  • 45
  • 3
  • 1
    On my reading of the question (which admittedly allows multiple interpretations) It doesn't do the job: it would match the title "Y" but not the title "Youth". You could use `$tit < "Z"`, but a regular expression seems better. – Michael Kay Aug 21 '22 at 10:53
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 24 '22 at 15:24