---
title: "nodes(forXPath:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/xmlnode/nodes(forxpath:)"
---

# nodes(forXPath:)

Returns the nodes resulting from executing an XPath query upon the receiver.

## Declaration

```swift
func nodes(forXPath xpath: String) throws -> [XMLNode]
```

## Parameters

- `xpath`: A string that expresses an XPath query.

## Return Value

Return Value An array of NSXMLNode objects that match the query, or an empty array if there are no matches.

## Discussion

Discussion The receiver acts as the context item for the query (”.”).  If you have explicitly added adjacent text nodes as children of an element, you should invoke the NSXMLElement method normalizeAdjacentTextNodesPreservingCDATA(_:) (with an argument of false) on the element before applying any XPath queries to it; this method coalesces these text nodes. The same precaution applies if you have processed a document preserving CDATA sections and these sections are adjacent to text nodes. note: In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure. You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Executing Queries

- [objects(forXQuery:)](foundation/xmlnode/objects(forxquery:).md)
- [objects(forXQuery:constants:)](foundation/xmlnode/objects(forxquery:constants:).md)
- [xPath](foundation/xmlnode/xpath.md)
