XQuery: // vs descendant-or-self :: node ()

Recently, I needed to evaluate XQuery in a Node HTML document. Basically, I needed to select all the elements with the href attribute from the first child of the body. I added a small example to explain:

<html>
    <body>
        <a href="http://www.google.be"/>
    </body>
</html>

The desired extraction result in this case is obviously:

<a href="http://www.google.be"/>

My first idea was to use //body/*[1]//*[@href]because:

  • //body matches the body element, wherever it is.
  • /*[1] matches the first child of body
  • //*[@href] matches all descendants or itself of the current element

I decided that this would work, but in the above example, XQuery does not produce any results.

However, I read a little and found the following (source: http://www.keller.com/xslt/8/ ):

Alternate notation for "//": descendant-or-self::node()

, XQuery //body/*[1]/descendant-or-self::node()[@href], .

: // -: node()? ( // node /descendant:: node xpath?) (http://www.w3.org/TR/xpath/#axes):

// /descendant-or-self::node()/. , //parashort /descendant-or-self::node()/child::para.

, // /descendant-or-self::node() (, - / ?), - , /descendant-or-self::node()?

+4
2

XPath (//body/*[1]//*[@href]) , : //body/*[1] - body, //*[@href] (), @href.

, . Fore xample,

<html>
    <body>
        <p>
            <a href="http://www.google.be"/>
        </p>
    </body>
</html>

- :

//body/*[1]/descendant-or-self::node()/*[@href]

, :

//body/*[1]/descendant-or-self::node()[@href]
+4

, , !

:

<html>
    <body>
        <a href="http://www.google.be"/>
    </body>
</html>

:

" href body"

XPath:

//body/*[1]//*[@href]

. , , ... , :

<a href="http://www.google.be"/>

, XPath, , :

" body href", XPath:

//*[@href][parent::body][1]

, , . , , :

" href"

, XPath:

($input//*[@href][ancestor::body])[1]

, , .. '(' ')' (-) , , .

+1

All Articles