Question
Deleting node returned from xmlSearch()
G'day
Consider this sample XML:
<aaa id="1">
<bbb id="2">
<ccc id="3"></ccc>
<ccc id="4"></ccc>
<ccc id="5"></ccc>
</bbb>
<bbb id="6"></bbb>
</aaa>
I am using the following xpath to single out a specific node:
//ccc[@id='4']
Having located that node, I need to delete it.
The best solution I've come up with is this:
<cfset a = xmlSearch(x, "//ccc[@id='4']/preceding-sibling::ccc")>
<cfset i = xmlChildPos(a[1].xmlParent, "ccc", arrayLen(a)+1)>
<cfset arrayDeleteAt(x.aaa.bbb.xmlChildren, i)>
Now this seems like a really silly approach (I'm also not certain it's
particularly robust). I would have thought - given I have the exact node I
want to delete - I should be able to do something along the lines of:
<cfset someDeleteFunction(a[1])>
Anyone done this sort of thing before?
PS: pls resist the temptation to offer suggestions that involve a loop
going "Is it this one? No. This one? No. This one? Yes! OK, delete
it". This does not - in my mind - constitute an appropriate solution (and
I've already thought of it, and cringed even more than with my current
"solution").
Cheers.
--
Adam
Consider this sample XML:
<aaa id="1">
<bbb id="2">
<ccc id="3"></ccc>
<ccc id="4"></ccc>
<ccc id="5"></ccc>
</bbb>
<bbb id="6"></bbb>
</aaa>
I am using the following xpath to single out a specific node:
//ccc[@id='4']
Having located that node, I need to delete it.
The best solution I've come up with is this:
<cfset a = xmlSearch(x, "//ccc[@id='4']/preceding-sibling::ccc")>
<cfset i = xmlChildPos(a[1].xmlParent, "ccc", arrayLen(a)+1)>
<cfset arrayDeleteAt(x.aaa.bbb.xmlChildren, i)>
Now this seems like a really silly approach (I'm also not certain it's
particularly robust). I would have thought - given I have the exact node I
want to delete - I should be able to do something along the lines of:
<cfset someDeleteFunction(a[1])>
Anyone done this sort of thing before?
PS: pls resist the temptation to offer suggestions that involve a loop
going "Is it this one? No. This one? No. This one? Yes! OK, delete
it". This does not - in my mind - constitute an appropriate solution (and
I've already thought of it, and cringed even more than with my current
"solution").
Cheers.
--
Adam