XQUERY SELECT()

From Lianjapedia
Jump to: navigation, search

Purpose

Used to query nodes by attribute and value in an XML file previously opened with XQUERY_OPEN()

Syntax

XQUERY_SELECT(<expC1>)

See Also

MQCLOSE(), MQCREATE(), MQCURMSGS(), MQOPEN(), MQSEND(), MQRECEIVE(), MQUNLINK(), PRINT XML(), XML, XML_DECODE(), XML_DECODE_FILE(), XML_ENCODE(), XML_GATHER(), XML_SCATTER(), XQUERY(), XQUERY_ATTRIBUTES(), XQUERY_CLOSE(), XQUERY_COUNT(), XQUERY_DECODE(), XQUERY_FILE(), XQUERY_FIND(), XQUERY_NODE(), XQUERY_OPEN()

Description

The XQUERY_SELECT() function is used to query nodes by attribute and value in an XML file previously opened with XQUERY_OPEN(). It returns the integer number of the matches found.

<expC1>

The character expression <expC1> is the query expression.

To query all nodes that have a specific attribute use the notation [@name<operator>value] e.g.

xquery_select("book[@category='children']")

To query all nodes that have a specific value use the notation [node<operator>value] e.g.

xquery_select("/bookstore/book[year>'2010']")

Supported <operator> operators are:

  • =
  •  !=
  • >
  • >=
  • <
  • <=

After the XQUERY_SELECT() function completes, the internal XML document is replaced by the result nodes and non-zero is returned if there are any matching nodes. If there are no matching nodes then 0 is returned. The next call to XQUERY_SELECT() operates on the previously selected nodes. To reload the XML document use XQUERY_OPEN() again. Use XQUERY_CLOSE() to free up any resources allocated by XQUERY_OPEN().

XQuery functions:

The XQUERY_OPEN() function is used to open an XML file for parsing using XPath notation. After opening using XQUERY_OPEN(), the XQUERY_FIND() or XQUERY_NODE() functions can be used to find and return a specified XML node value. The XQUERY_ATTRIBUTES() function finds and returns a complete node and its attributes. XQUERY_COUNT() returns a count of a specified XML node. The XQUERY_SELECT() function provides the ability to query nodes by attribute and value. The XQUERY_CLOSE() function is closes the XML file.

The XQUERY() function is used to parse XML strings using XPath notation.

The XQUERY_FILE() function is used to parse XML files using XPath notation.

The XQUERY_DECODE() function is used to return an object from an XML string.

Example

mybooks.xml

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
 
<book category="COOKING">
  <title lang="en">Pulse</title>
  <author>Jenny Chandler</author>
  <year>2013</year>
  <format>Hardback</format>
</book>
 
<book category="COOKING">
  <title lang="en">Riverford Farm Cook Book</title>
  <author>Guy Watson</author>
  <author>Jane Baxter</author>
  <year>2008</year>
  <format>Paperback</format>
</book>
 
<book category="CHILDREN">
  <title lang="en">The House At Pooh Corner</title>
  <author>A. A. Milne</author>
  <year>1928</year>
  <format>Hardback</format>
</book>
 
<book category="CRIME">
  <title lang="en">Knots and Crosses</title>
  <author>Ian Rankin</author>
  <year>2008</year>
  <format>EPUB</format>
</book>
 
<book category="PHILOSOPHY">
  <title lang="fr">Le mythe de Sisyphe</title>
  <author>Albert Camus</author>
  <year>1943</year>
  <format>Paperback</format>
</book>
 
</bookstore>
// Examples using mybooks.xml
xquery_open("mybooks.xml")
xquery_select("book[@category='children']")
obj = xquery_decode()
? obj
xquery_close()
 
// Results:
 
Dynarray (refcnt=1)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => The House At Pooh Corner
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => en
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => A. A. Milne
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 1928
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => Hardback
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => CHILDREN
                        )
                )
        )
)
 
xquery_open("mybooks.xml")
xquery_select("/bookstore/book[year>'2010']")
obj = xquery_decode()
? obj
xquery_close()
 
// Results:
 
Dynarray (refcnt=1)
(
    [book] => Dynarray (refcnt=1)
        (
            [1] => Dynarray (refcnt=1)
                (
                    [title] => Dynarray (refcnt=1)
                        (
                            [text] => Pulse
                            [attributes] => Dynarray (refcnt=1)
                                (
                                    [lang] => en
                                )
                        )
                    [author] => Dynarray (refcnt=1)
                        (
                            [text] => Jenny Chandler
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [year] => Dynarray (refcnt=1)
                        (
                            [text] => 2013
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [format] => Dynarray (refcnt=1)
                        (
                            [text] => Hardback
                            [attributes] => Dynarray (refcnt=1)
                                (
                                )
                        )
                    [attributes] => Dynarray (refcnt=1)
                        (
                            [category] => COOKING
                        )
                )
        )
)