Home > Mobile >  Xquery - Between two letters
Xquery - Between two letters

Time:08-22

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)

CodePudding user response:

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

CodePudding user response:

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

  • Related