www.espertech.comDocumentation
This section provides information for using org.w3c.dom.Node
XML to represent events.
For NEsper .NET also see Section I.9, “.NET XML Events”.
Events can be represented as org.w3c.dom.Node
instances and send into the engine via the sendEvent
method on EPRuntime
or via EventSender
. Please note that configuration is required so the event type name and root element name is known. See Chapter 15, Configuration.
If a XML schema document (XSD file) can be made available as part of the configuration, then Esper can read the schema and appropriately present event type metadata and validate statements that use the event type and its properties. See Section H.2, “Schema-Provided XML Events”.
When no XML schema document is provided, XML events can still be queried, however the return type and return values of property expressions are string-only and no event type metadata is available other than for explicitly configured properties. See Section H.3, “No-Schema-Provided XML Events”.
In all cases Esper allows you to configure explicit XPath expressions as event properties. You can specify arbitrary XPath functions or expressions and provide a property name and type by which result values will be available for use in EPL statements. See Section H.4, “Explicitly-Configured Properties”.
Nested, mapped and indexed event properties are also supported in expressions against org.w3c.dom.Node
events. Thus XML trees can conveniently be interrogated via the property expression syntax.
Only one event type per root element name may be configured. The engine recognizes each event by its root element name or you may use EventSender
to send events.
This section uses the following XML document as an example:
<?xml version="1.0" encoding="UTF-8"?> <Sensor xmlns="SensorSchema"> <ID>urn:epc:1:4.16.36</ID> <Observation Command="READ_PALLET_TAGS_ONLY"> <ID>00000001</ID> <Tag> <ID>urn:epc:1:2.24.400</ID> </Tag> <Tag> <ID>urn:epc:1:2.24.401</ID> </Tag> </Observation> </Sensor>
The schema for the example is:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Sensor"> <xs:complexType> <xs:sequence> <xs:element name="ID" type="xs:string"/> <xs:element ref="Observation" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Observation"> <xs:complexType> <xs:sequence> <xs:element name="ID" type="xs:string"/> <xs:element ref="Tag" maxOccurs="unbounded" /> </xs:sequence> <xs:attribute name="Command" type="xs:string" use="required" /> </xs:complexType> </xs:element> <xs:element name="Tag"> <xs:complexType> <xs:sequence> <xs:element name="ID" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
If you have a XSD schema document available for your XML events, Esper can interrogate the schema. The benefits are:
New EPL statements that refer to event properties are validated against the types provided in the schema.
Event type metadata becomes available for retrieval as part of the EventType
interface.
The engine reads a XSD schema file from an URL you provide. Make sure files imported by the XSD schema file can also be resolved.
The configuration accepts a schema URL. This is a sample code snippet to determine a schema URL from a file in classpath:
URL schemaURL = this.getClass().getClassLoader().getResource("sensor.xsd"); // For NEsper .NET use C# ResourceManager class for loading resources
Here is a sample use of the runtime configuration API, please see Chapter 15, Configuration for further examples.
epService = EPServiceProviderManager.getDefaultProvider(); ConfigurationEventTypeXMLDOM sensorcfg = new ConfigurationEventTypeXMLDOM(); sensorcfg.setRootElementName("Sensor"); sensorcfg.setSchemaResource(schemaURL.toString()); epService.getEPAdministrator().getConfiguration() .addEventType("SensorEvent", sensorcfg);
You must provide a root element name. This name is used to look up the event type for the sendEvent(org.w3c.Node node)
method. An EventSender
is a useful alternative method
for sending events if the type lookup based on the root or document element name is not desired.
After adding the event type, you may create statements and send events. Next is a sample statement:
select ID, Observation.Command, Observation.ID, Observation.Tag[0].ID, Observation.Tag[1].ID from SensorEvent
As you can see from the example above, property expressions can query property values held in the XML document's elements and attributes.
There are multiple ways to obtain a XML DOM document instance from a XML string. The next code snippet shows how to obtain a XML DOM org.w3c.Document
instance:
InputSource source = new InputSource(new StringReader(xml)); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); Document doc = builderFactory.newDocumentBuilder().parse(source);
Send the org.w3c.Node
or Document
object into the engine for processing:
epService.getEPRuntime().sendEvent(doc);
By default, property expressions such as Observation.Tag[0].ID
are evaluated by a fast DOM-walker implementation provided by Esper. This DOM-walker implementation is not namespace-aware.
Should you require namespace-aware traversal of the DOM document, you must set the xpath-property-expr
configuration option to true (default is false). This flag causes Esper to generate namespace-aware XPath
expressions from each property expression instead of the DOM-walker, as described next. Setting the xpath-property-expr
option to true requires that you also configure namespace prefixes as described below.
When matching up the property names with the XSD schema information, the engine determines whether the attribute or element provides values. The algorithm checks attribute names first followed by element names. It takes the first match to the specified property name.
By setting the xpath-property-expr
option the engine rewrites each property expression as an XPath expression, effectively handing the evaluation over to the underlying XPath implementation
available from classpath. Most JVM have a built-in XPath implementation and there are also optimized, fast implementations such as Jaxen that can be used as well.
Set the xpath-property-expr
option if you need namespace-aware document traversal, such as when your schema mixes several namespaces and element names are overlapping.
The below table samples several property expressions and the XPath expression generated for each, without namespace prefixes to keep the example simple:
Table H.1. Property Expression to XPath Expression
Property Expression | Equivalent XPath |
---|---|
Observeration.ID | /Sensor/Observation/ID |
Observeration.Command | /Sensor/Observation/@Command |
Observeration.Tag[0].ID | /Sensor/Observation/Tag[position() = 1]/ID |
For mapped properties that are specified via the syntax name('key')
, the algorithm looks for an attribute by name id
and generates a XPath expression as mapped[@id='key']
.
Finally, here is an example that includes all different types of properties and their XPath expression equivalent in one property expression:
select nested.mapped('key').indexed[1].attribute from MyEvent
The equivalent XPath expression follows, this time including n0
as a sample namespace prefix:
/n0:rootelement/n0:nested/n0:mapped[@id='key']/n0:indexed[position() = 2]/@attribute
All elements that are unbound or have max occurs greater then 1 in the XSD schema are represented as indexed properties and require an index for resolution.
For example, the following is not a valid property expression in the sample Sensor document: Observeration.Tag.ID
. As no index is provided for Tag
, the property expression is not valid.
Repeated elements within a parent element in which the repeated element is a simple type also are represented as an array.
Consider the next XML document:
<item> <book sku="8800090"> <author>Isaac Asimov</author> <author>Robert A Heinlein</author> </book> </item>
Here, the result of the expression book.author
is an array of type String and the result of book.author[0]
is a String value.
Dynamic properties are not validated against the XSD schema information and their result value is always org.w3c.Node
. You may use a user-defined function to process dynamic properties returning Node
. As an alternative consider using an explicit property.
An example dynamic property is Origin?.ID
which will look for an element by name Origin
that contains an element or attribute node by name LocationCode
:
select Origin?.LocationCode from SensorEvent
When providing a XSD document, the default configuration allows to transpose property values that are themselves complex elements, as defined in the XSD schema, into a new stream. This behavior can be controlled via the flag auto-fragment
.
For example, consider the next query:
insert into ObservationStream select ID, Observation from SensorEvent
The Observation
as a property of the SensorEvent
gets itself inserted into a new stream by name ObservationStream
. The ObservationStream
thus consists of a string-typed ID
property and a complex-typed property named Observation
, as described in the schema.
A further statement can use this stream to query:
select Observation.Command, Observation.Tag[0].ID from ObservationStream
Before continuing the discussion, here is an alternative syntax using the wildcard-select, that is also useful:
insert into TagListStream select ID as sensorId, Observation.* from SensorEvent
The new TagListStream
has a string-typed ID
and Command
property as well as an array of Tag
properties that are complex types themselves as defined in the schema.
Next is a sample statement to query the new stream:
select sensorId, Command, Tag[0].ID from TagListStream
Please note the following limitations:
The XPath standard prescribes that XPath expressions against org.w3c.Node
are evaluated against the owner document of the Node
. Therefore XPath is not relative to the current node but absolute against each node's owner document. Since Esper does not create new document instances for transposed nodes, transposing properties is not possible when the xpath-property-expr
flag is set.
Complex elements that have both simple element values and complex child elements are not transposed. This is to ensure their property value is not hidden. Use an explicit XPath expression to transpose such properties.
Esper automatically registers a new event type for transposed properties. It generates the type name of the new XML event type from the XML event type name and the property names used in the expression. The synposis is type_name.property_name[.property_name...]. The type name can be looked up, for example for use with EventSender
or can be created in advance.
An EventSender
sends events into the engine for a given type, saving a type lookup based on element name.
This brief example sends an event via EventSender
:
EventSender sender = epRuntime.getEventSender("SensorEvent"); sender.sendEvent(node);
The XML DOM event sender checks the root element name before processing the event. Use the event-sender-validates-root
setting to disable validation. This forces the engine to process XML documents according to any predefined type without validation of the root element name.
The engine schema interrogation is based on the Xerces distribution packaged into Sun Java runtimes. Your application may not replace the JRE's Xerces version and use XML schemas, unless your application sets the DOM implementation registry as shown below before loading the engine configuration:
System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl");
Without a schema document a XML event may still be queried. However there are important differences in the metadata available without a schema document and therefore the property expression results. These differences are outlined below.
All property expressions against a XML type without schema are assumed valid. There is no validation of the property expression other than syntax validation. At runtime, property expressions return string-type values or null
if the expression did not
yield a matching element or attribute result.
When asked for property names or property metadata, a no-schema type returns empty array.
In all other aspects the type behaves the same as the schema-provided type described earlier.
Regardless of whether or not you provide a XSD schema for the XML event type, you can always fall back to configuring explicit properties that are backed by XPath expressions.
For further documentation on XPath, please consult the XPath standard or other online material. Consider using Jaxen or Apache Axiom, for example, to provide faster XPath evaluation then your Java VM built-in XPath provider may offer.
Shown below is an example configuration that adds an explicit property backed by a XPath expression and that defines namespace prefixes:
epService = EPServiceProviderManager.getDefaultProvider(); ConfigurationEventTypeXMLDOM sensorcfg = new ConfigurationEventTypeXMLDOM(); sensorcfg.addXPathProperty("countTags", "count(/ss:Sensor/ss:Observation/ss:Tag)", XPathConstants.NUMBER); sensorcfg.addNamespacePrefix("ss", "SensorSchema"); sensorcfg.setRootElementName("Sensor"); epService.getEPAdministrator().getConfiguration() .addEventType("SensorEvent", sensorcfg);
The countTags
property is now available for querying:
select countTags from SensorEvent
The XPath expression count(...)
is a XPath built-in function that counts the number of nodes, for the example document the result is 2
.
Esper can parse or cast the result of your XPath expression to the desired type. Your property configuration provides the type to cast to, like this:
sensorcfg.addXPathProperty("countTags", "count(/ss:Sensor/ss:Observation/ss:Tag)", XPathConstants.NUMBER, "int");
The type supplied to the property configuration must be one of the built-in types. Arrays of built-in type are also possible, requiring the XPathConstants.NODESET
type returned by your XPath expression, as follows:
sensorcfg.addXPathProperty("idarray", "//ss:Tag/ss:ID", XPathConstants.NODESET, "String[]");
The XPath expression //ss:Tag/ss:ID
returns all ID nodes under a Tag node, regardless of where in the node tree the element is located. For the example document the result is 2
array elements urn:epc:1:2.24.400
and urn:epc:1:2.24.40
.
An explicit property may return XPathConstants.NODE
or XPathConstants.NODESET
and can provide the event type name of a pre-configured event type for the property. The method name to add such properties is addXPathPropertyFragment
.
This code snippet adds two explicit properties and assigns an event type name for each property:
sensorcfg.addXPathPropertyFragment("tagOne", "//ss:Tag[position() = 1]", XPathConstants.NODE, "TagEvent"); sensorcfg.addXPathPropertyFragment("tagArray", "//ss:Tag", XPathConstants.NODESET, "TagEvent");
The configuration above references the TagEvent
event type. This type must also be configured. Prefix the root element name with "//" to cause the lookup to search the nested schema elements for the definition of the type:
ConfigurationEventTypeXMLDOM tagcfg = new ConfigurationEventTypeXMLDOM(); tagcfg.setRootElementName("//Tag"); tagcfg.setSchemaResource(schemaURL); epAdministrator.getConfiguration() .addEventType("TagEvent", tagcfg);
The tagOne
and tagArray
properties are now ready for selection and transposing to further streams:
insert into TagOneStream select tagOne.* from SensorEvent
Select from the new stream:
select ID from TagOneStream
An example with indexed properties is shown next:
insert into TagArrayStream select tagArray as mytags from SensorEvent
Select from the new stream:
select mytags[0].ID from TagArrayStream