Search code examples
xsltxpathxqueryroot-nodexquery-sql

XPath/XQuery: Select a root node with its attributes without childs


I have an xml:

<Customer id="">
 <Name />
 <Address />
</Customer>

I would like to select ONLY a root node with its attributes without its child nodes:

<Customer id=""/ >

Is such thing possible with XPath?


Solution

  • No, this is not possible in XPath.

    You can't select the node without its children, because without its children it would be a different node, hence you would not be selecting a node from the original document.

    To create the output you want you need to use a language which allows you to create new nodes, so you can't do it in XPath. You can use XQuery to create new nodes, this should work:

    element {fn:node-name(/*)} {/*/@*}