# Using DocumentFragment with document.evaluate()

**URL:** https://es.discourse.group/t/using-documentfragment-with-document-evaluate/1457
**Category:** Research
**Created:** [September 15, 2022, 12:21am UTC](https://es.discourse.group/t/using-documentfragment-with-document-evaluate/1457 "2022-09-15T00:21:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jonnytest1](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jonnytest1/32/1501_2.png) [@jonnytest1](https://es.discourse.group/u/jonnytest1)
#### Post date: [September 15, 2022, 12:21am UTC](https://es.discourse.group/t/using-documentfragment-with-document-evaluate/1457/1 "2022-09-15T00:21:21Z")

</div>

when using a template tag

```javascript
<template>
            <div>test</div>
            <div>{{text}}</div>
</template>

```

and then creating an instance of it with  
`document.querySelector("template").cloneNode(true)`  
you get a template instance with `.content` being a document-fragment of all the children of the original template

now if i want to query the elements in this document-fragment for changes for example for variable replacement (only returning those whose textContent ends and start with brackets in this case)  
my prefered way of doing this would be using document.evaluate with the contextNode being the document fragment ,  
`document.evaluate('.//*[starts-with(text(),'{{')]',contextNode, null, XPathResult.ANY_TYPE,null )`

```javascript
// omitted ends-with as it is quite the lengthy code as xpath in the browser is only v1.0 but thats a differnet matter 😅

```

unfortunately when doing this i get an error that document-fragment is not a valid node 🤔 and well ... i wanna know why it isnt 😇  
`uncaught DOMException: Failed to execute 'evaluate' on 'Document': The node provided is '#document-fragment', which is not a valid context node type.`

---

<div class="post-metadata">

### Author: ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)
#### Post date: [September 15, 2022, 2:56am UTC](https://es.discourse.group/t/using-documentfragment-with-document-evaluate/1457/2 "2022-09-15T02:56:59Z")

</div>

DocumentFragment and everything DOM-related is not part of JavaScript, it's part of HTML, so this isn't the right place to discuss it. See [html/CONTRIBUTING.md at 823a14b4353266c7885c7b06da0d6ba1e4f1bb20 · whatwg/html · GitHub](https://github.com/whatwg/html/blob/823a14b4353266c7885c7b06da0d6ba1e4f1bb20/CONTRIBUTING.md)
