<xquery>my xquery / xpath2 expression</xquery> gets evaluated.
This commit is contained in:
parent
47302d9d02
commit
0856957b93
|
@ -34,10 +34,13 @@ import bsddb3.db as db
|
|||
import dbxml
|
||||
import libxml2
|
||||
|
||||
import expression.core.elements as elements
|
||||
import expression.core.environs as environs
|
||||
import expression.core.dataholders as dataholders
|
||||
import expression.core.html as html
|
||||
import expression.core.logs as logs
|
||||
import expression.core.namespaces as namespaces
|
||||
import expression.core.things as things
|
||||
|
||||
import expression.modules.dbxmlcontainers as dbxmlcontainers
|
||||
|
||||
|
@ -84,6 +87,33 @@ class DbXmlDatabase(dataholders.DataHolder):
|
|||
return super(DbXmlDatabase, self).walkToItem(uriPathFragments, command, instruction)
|
||||
|
||||
|
||||
class XQuery(things.Thing):
|
||||
""" Element xquery contains an XQuery expression to be evaluated.
|
||||
"""
|
||||
def generateXml(self, layout):
|
||||
""" Appends "layout" with the result of this query
|
||||
"""
|
||||
manager = DbXmlDatabase._manager
|
||||
queryContext = manager.createQueryContext()
|
||||
try:
|
||||
results = manager.query(self.node.content, queryContext)
|
||||
except RuntimeError, e:
|
||||
logs.error("Error in query: %s" % e)
|
||||
layout.append(html.p(_("Error in query: %s") % e))
|
||||
else:
|
||||
output = "\n".join([value.asString() for value in results])
|
||||
try:
|
||||
doc = libxml2.readDoc(output, None, None, 0)
|
||||
except libxml2.treeError:
|
||||
# output is not an XML document, considering it as plain text
|
||||
layout.append(html.html(html.body(html.pre(output))))
|
||||
else:
|
||||
layout.append(doc.getRootElement())
|
||||
return True
|
||||
|
||||
elements.registerElement(namespaces.yep.uri, "xquery", XQuery)
|
||||
|
||||
|
||||
def __init__():
|
||||
""" Opens a Berkeley DB environment and an XmlManager around it.
|
||||
Then opens all configured containers.
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0"?>
|
||||
<yep:xquery xmlns:yep="http://www.entrouvert.org/namespaces/expression/0.0">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<body>
|
||||
<p>
|
||||
collection("test.dbxml")//*/text()
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
</yep:xquery>
|
Reference in New Issue