idref
Retrieves elements that refer to other elements based on ID
Description
The fn:idref function returns the nodes (elements or attributes) that reference one of a specified sequence of IDs. If $node is not provided, the function looks for elements and attributes in the same document as the current context item. If $node is provided, the function looks for elements and attributes in the same document as $node.
In order to be considered to reference an ID, the node must be marked as an IDREF or IDREFS type, and it must contain that ID value. Generally, this means that it was declared to be of type xs:IDREF or xs:IDREFS in an in-scope schema definition. If it is an IDREFS value, only one of the values in the list need match the 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. However, there may be several elements or attributes in the results that reference the same ID.
Parameters |
Description |
|---|---|
arg:string() |
the IDREF value to find references to |
node:node() |
a node of the document to look in |
Examples
The examples below use this input document:
<!-- $book -->
<!DOCTYPE book [
<!ELEMENT book (section*) >
<!ELEMENT section ANY >
<!ATTLIST section id ID #REQUIRED >
<!ELEMENT fn ANY >
<!ATTLIST fn fnid ID #REQUIRED >
<!ELEMENT fnref EMPTY >
<!ATTLIST fnref ref IDREF #REQUIRED >
<!ELEMENT secRef EMPTY >
<!ATTLIST secRef refs IDREFS #REQUIRED >
]>
<book>
<section id='preface'>This book introduces XQuery...
The examples are downloadable<fnref ref="fn1"/>...
</section>
<section id='context'>...</section>
<section id='language'>...Expressions, introduced
in <secRef refs='context'/>, are...
</section>
<section id='types'>...As described in
<secRef refs='context language'/>, you can...
</section>
<fn fnid='fn1'>See http://datypic.com.</fn>
</book>
It is assumed that the input document has been validated, that the type of the id and fnid attributes is xs:ID, the type of the ref attribute is xs:IDREF, and the type of the refs attribute is xs:IDREFS.
XPath |
Results |
Explanation |
|---|---|---|
$book/idref('language')/string(.) |
context language |
String value of the refs attribute of the second secRef element |
$book/idref('context')/string(.) |
('context', 'context language') |
String value of the refs attributes of both secRef elements |
$book/idref(('context', 'language'))/string(.) |
('context', 'context language') |
String value of the refs attributes of both secRef elements |
$book//fn[1]/idref(@fnid)/string(.) |
fn1 |
String value of the ref attribute of the fnRef element |
$book/idref(('language', 'noMatch', 'in!valid'))/string(.) |
context language |
String value of the refs attribute of the second secRef element |
In a generator
Copy the snippet below and paste it in Freemapper, under a language node inside the generators element. Each line of the pattern prints the result of one example; to see them, run mi generate <project> <language> example-idref.
[e] example-idref
[e] vars
[e] book
[e] select
[v] parse-xml('<!DOCTYPE book [ <!ELEMENT book (section*) ><!ELEMENT section ANY ><!ATTLIST section id ID #REQUIRED ><!ELEMENT fn ANY ><!ATTLIST fn fnid ID #REQUIRED ><!ELEMENT fnref EMPTY ><!ATTLIST fnref ref IDREF #REQUIRED ><!ELEMENT secRef EMPTY ><!ATTLIST secRef refs IDREFS #REQUIRED >]><book><section id=''preface''>This book introduces XQuery... The examples are downloadable<fnref ref="fn1"/>... </section><section id=''context''>...</section><section id=''language''>...Expressions, introduced in <secRef refs=''context''/>, are... </section><section id=''types''>...As described in <secRef refs=''context language''/>, you can... </section><fn fnid=''fn1''>See http://datypic.com.</fn></book>')
[e] start
[e] match
[v] /
[e] body
[e] write-pattern
[v] examples
[e] patterns
[e] examples
[v] E01: {{ $book/idref('language')/string(.) }}
[v] E02: {{ $book/idref('context')/string(.) }}
[v] E03: {{ $book/idref(('context', 'language'))/string(.) }}
[v] E04: {{ $book//fn[1]/idref(@fnid)/string(.) }}
[v] E05: {{ $book/idref(('language', 'noMatch', 'in!valid'))/string(.) }}