0

I want to get all book titles published before yr2000

XML

<bookstore xmlns:old="http://www.w3.org/TR/html4/" xmlns:new="http://www.w3.org/1999/XSL/Transform">
<old:book>
    <old:title lang="en">Harry Potter I</old:title>
    <old:author>J K. Rowling</old:author>
    <old:year>1997</old:year>
    <old:price>450</old:price>
</old:book>
<old:book>
    <old:title lang="hn">Malgudi Days </old:title>
    <old:author>R.K. Narayan </old:author>
    <old:year>2006</old:year>
    <old:price>160</old:price>
</old:book>

<new:book>
    <new:title lang="en">Can Love Happen Twice?</new:title>
    <new:author>Ravinder Singh</new:author>
    <new:year>2011</new:year>
    <new:price>150</new:price>
</new:book>
<new:book>
    <new:title lang="en">The Lowland</new:title>
    <new:author>Jhumpa Lahiri</new:author>
    <new:year>2013</new:year>
    <new:price>240</new:price>
</new:book>

i am getting all the titles using expression

pathExpr = "/bookstore/*[local-name() = 'book']/*[local-name() = 'title']";

now i want to add condition (year > 2000). I tried with

pathExpr = "/bookstore/*[local-name() = 'book'][local-name() = 'year' < 2000]/*[local-name() = 'title']";

and this

pathExpr = "/bookstore/*[local-name() = 'book']||[local-name() = 'year' < 2000]/*[local-name() = 'title']";    

but its not working. Not getting how to deal with namespace. Thanks in advance.

4

1 回答 1

2

This should work:

/bookstore/*[local-name() = 'book']
            [./*[local-name() = 'year'] < 2000]/*[local-name() = 'title']

I added ./*[...] in your test for a year child of book

  • ./*[local-name() = 'year'] selects the child node
  • [./*[local-name() = 'year'] < 2000] tests it's content value
于 2013-09-13T10:18:52.930 回答