I have an XML document named employees.xml:
<employees>
<row>
<emp_no>10001</emp_no>
<first_name>Georgi</first_name>
<last_name>Facello</last_name>
</row>
<row>
<emp_no>10002</emp_no>
<first_name>Bezalel</first_name>
<last_name>Simmel</last_name>
</row>
</employees>
I want to write a function named my-remove-elements
that will remove non-selected attributes. For example, only keep first_name
and last_name
in the XML document:
<employees>
<row>
<first_name>Georgi</first_name>
<last_name>Facello</last_name>
</row>
<row>
<first_name>Bezalel</first_name>
<last_name>Simmel</last_name>
</row>
</employees>
The definition of my function is:
declare function local:my-remove-elements($input as element(), $remove-names as xs:string*) as element() {
element {node-name($input) }
{$input/@*,
for $child in $input/node()[name(.)=$remove-names]
return
if ($child instance of element())
then local:my-remove-elements($child, $remove-names)
else $child
}
};
This is the way I call it:
let $doc := doc("employees.xml")
return
local:my-remove-elements($doc, ('first_name', 'last_name'))
It throws me "err:XPTY0004 ... is not a sub-type of element()..." I have changed the code:
let $rows:= doc("employees.xml")//row
return
local:my-remove-elements($rows, ('first_name', 'last_name'))
this time is still: "err:XPTY0004: The actual cardinality for parameter 1 does not match the cardinality in the function's signature ...". Do you know how to fix this and make it work?