update on context nodes
Friday, 12 November 2010 · 2 messages
CG
Quoted text · Stefano Santoro · 2 lines
> when I bind a DOM tree as the context item in XQJ against an XQuery script > that performs updates, the passed DOM tree is not modified.
Exactly – the tree is only modified within BaseX, but not converted back to the original DOM representation. You might be more successful with a transform/copy expression:
copy $c := .
modify (insert node <x/> into $c, .....)
return $cHope this helps,
Christian
Quoted text · Stefano Santoro · 64 lines
Here is a groovy > example > import groovy.xml.XmlUtil > import groovy.xml.DOMBuilder > import groovy.xml.dom.DOMCategory > import javax.xml.xquery.XQConstants > docbuilder = DOMBuilder.newInstance() > doc = docbuilder.sample() { > renameMe(id:11) { > renameInner(isit:"true","yes") > } > deleteMe(id:12) { > alongWithMe("bummer") > } > insertBeforeMe(id:14) { > alongWith(me:"true","Oh Yeah") > andMe(too:"yes","Oh Yeah") > } > insertAfterMe(id:15) { > atLevelThree(level:3) { > atLevelFour(level:4, "fourth level") > atLevelFour(level:4, "fourth level again") > } > alongWith(me:"true","Oh Yeah") > } > } > // prints the DOM tree before updates > XmlUtil.serialize(doc, System.out) > ds = Class.forName("org.basex.api.xqj.BXQDataSource").newInstance() > cnn = ds.getConnection() > stmt = """ > let \$r := . > return ( > rename node \$r/renameMe as 'gotRenamed', > delete node \$r/deleteMe[@id='12'], > insert node > <gotInsertedBefore id='13'> > <alongWithMe>Cool</alongWithMe> > </gotInsertedBefore> > before \$r/insertBeforeMe[@id='14'], > insert node > <gotInsertedAfter id='16'> > <alongWithMe>Cool</alongWithMe> > </gotInsertedAfter> > after \$r/insertAfterMe[@id='15'] > ) > """ > xe = cnn.prepareExpression(stmt) > xe.bindNode(XQConstants.CONTEXT_ITEM, doc, null) > xqs = xe.executeQuery() > // the DOM tree was not modified > XmlUtil.serialize(doc, System.out) > xqs.close() > xe.close() > cnn.close() > Should I pass it as an external variable instead? > Ciao > Stefano > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > >
SS
Stefano Santoro12 Nov 2010 19:19 #
Hi,
when I bind a DOM tree as the context item in XQJ against an XQuery script that performs updates, the passed DOM tree is not modified. Here is a groovy example
import groovy.xml.XmlUtil
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategoryimport javax.xml.xquery.XQConstants
docbuilder = DOMBuilder.newInstance()
doc = docbuilder.sample() {
renameMe(id:11) {
renameInner(isit:"true","yes")
}
deleteMe(id:12) {
alongWithMe("bummer")
}
insertBeforeMe(id:14) {
alongWith(me:"true","Oh Yeah")
andMe(too:"yes","Oh Yeah")
}
insertAfterMe(id:15) {
atLevelThree(level:3) {
atLevelFour(level:4, "fourth level")
atLevelFour(level:4, "fourth level again")
}
alongWith(me:"true","Oh Yeah")
}
}// prints the DOM tree before updates
XmlUtil.serialize(doc, System.out)
ds = Class.forName("org.basex.api.xqj.BXQDataSource").newInstance()
cnn = ds.getConnection()
stmt = """
let \$r := .
return (
rename node \$r/renameMe as 'gotRenamed',
delete node \$r/deleteMe[@id='12'],
insert node
<gotInsertedBefore id='13'>
<alongWithMe>Cool</alongWithMe>
</gotInsertedBefore>
before \$r/insertBeforeMe[@id='14'],
insert node
<gotInsertedAfter id='16'>
<alongWithMe>Cool</alongWithMe>
</gotInsertedAfter>
after \$r/insertAfterMe[@id='15']
)
"""xe = cnn.prepareExpression(stmt)
xe.bindNode(XQConstants.CONTEXT_ITEM, doc, null)
xqs = xe.executeQuery()
// the DOM tree was not modified
XmlUtil.serialize(doc, System.out)
xqs.close()
xe.close()
cnn.close()
Should I pass it as an external variable instead?
Ciao
Stefano
Written for BaseX 6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery