home contribute faq download

FunctX XQuery Functions

fn:id

Retrieves elements by their ID

Google
Webxqueryfunctions.com

Description

The fn:id function accepts a sequence of IDs and returns the elements with those IDs. If $node is not provided, the function looks for elements in the same document as the current context item. If $node is provided, the function looks for elements in the same document as $node.

The strings in the $arg sequence can either be individual ID values, or space-separated lists of ID values. Although $arg is a sequence of xs:string values, you can also pass values of type xs:IDREF or xs:IDREFS, since these types are derived from xs:string.

An element is considered to have a particular ID if either:

  • It has an attribute, that is marked as an ID, that has that ID value, or
  • The type of the element itself is marked as an ID and its content is that particular ID value.

An element or attribute node can become "marked as an ID" by being validated by a schema where it is declared as having type xs:ID, or (for an attribute) being described in a DTD as being of type ID.

The default collation is not used to match ID values; a simple comparison of Unicode code points is used.

The function returns the nodes in document order, not the order designated by the sequence of $arg values. The result sequence contains no duplicate elements, even if an ID value was included twice in $arg.

This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book.

Arguments and Return Type

NameTypeDescription
$arg xs:string* the ID value to find
$node node() a node of the document to look in
return value element()*

Examples

let $book :=
doc('http://www.functx.com/input/book.xml')
return
XQuery ExampleResults
These examples assume that the input document has been validated, and that the type of the id attribute is xs:ID.
$book/id('preface')
<section id="preface">This book introduces XQuery...
  The examples are downloadable<fnref ref="fn1"/>...
</section>
$book/id(
  ('context','preface'))
<section id="preface">This book introduces XQuery...
  The examples are downloadable<fnref ref="fn1"/>...
</section>
<section id="context">...</section>
$book/id('context preface')
<section id="preface">This book introduces XQuery...
  The examples are downloadable<fnref ref="fn1"/>...
</section>
<section id="context">...</section>
$book//section[3]/secRef/id(
  @refs)
<section id="context">...</section>
$book//section[4]/secRef/id(
  @refs)
<section id="context">...</section>
<section id="language">...Expressions, introduced
 in <secRef refs="context"/>, are...
</section>
$book//secRef/id(@refs)
<section id="context">...</section>
<section id="language">...Expressions, introduced
in <secRef refs="context"/>, are...
</section>
$book/id(
  ('preface','noMatch',
   'in!valid'))
<section id="preface">This book introduces XQuery...
  The examples are downloadable<fnref ref="fn1"/>...
</section>

See Also

fn:idrefRetrieves elements that refer to other elements based on ID
functx:id-untypedGets XML element(s) that have an attribute with a particular value
functx:id-from-elementGets the ID of an XML element

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XQuery Services

Recommended Reading:

XQuery