<!--
{
  "availability" : [
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/XMLNode/objects(forXQuery:constants:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSXMLNode(im)objectsForXQuery:constants:error:"
  },
  "title" : "objects(forXQuery:constants:)"
}
-->

# objects(forXQuery:constants:)

Returns the objects resulting from executing an XQuery query upon the receiver.

```
func objects(forXQuery xquery: String, constants: [String : Any]?) throws -> [Any]
```

## Parameters

`xquery`

A string that expresses an XQuery query.

`constants`

A dictionary containing externally declared constants where the name of each constant variable is a key.

## Discussion

The receiver acts as the context item for the query (”.”). If the receiver has been changed after parsing to have multiple adjacent text nodes, you should invoke the `NSXMLElement` method [`normalizeAdjacentTextNodesPreservingCDATA(_:)`](/documentation/Foundation/XMLElement/normalizeAdjacentTextNodesPreservingCDATA(_:)) (with an argument of <doc://com.apple.documentation/documentation/Swift/false>) to coalesce the text nodes before querying.

> Handling Errors in Swift:
> 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](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)