XPath 1.0 Usage

XPath expressions can be defined within the context of a runtime component, e.g. within a schema fragment definition, or can be used programmatically within your applications. For more information regarding schema fragments and schema properties and how they are used within the context of alakai, please see the user guide. If you are unfamiliar with the XPath 1.0 language, we recommend reading the w3schools tutorial.

Defining Message Fragment Expressions

The following example defines an XPath expression which is used to extract instances of schema property "myProperty1" from instances of "myElement1" at runtime. Note the value of the 'language' attribute, which is used to indicate that XPath 1.0 is the query language used to define the expression:

<deployment xmlns="http://bluestemsoftware.org/specification/eoa/1.0/deployment">
  <providers>
    <xs:schema id="S1" targetNamespace="http://foo" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://foo">
      <xs:annotation xmlns:eoa="http://bluestemsoftware.org/specification/eoa/1.0/component/schema/ext">
        <xs:appinfo source="http://bluestemsoftware.org/specification/eoa/1.0">
          <eoa:property name="myProperty1" type="tns:mySimpleType1" />
        </xs:appinfo>
      </xs:annotation>
      <xs:simpleType name="mySimpleType1">
        <xs:restriction base="xs:int">
          <xs:minInclusive value="1" />
          <xs:maxInclusive value="9" />
        </xs:restriction>
      </xs:simpleType>
    </xs:schema>
    <xs:schema id="S2" targetNamespace="http://foo" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://foo">
      <xs:include schemaLocation="#http://foo%23eoa.schema(S1)"/>
      <xs:element name="myElement1">
        <xs:annotation xmlns:eoa="http://bluestemsoftware.org/specification/eoa/1.0/component/schema/ext">
          <xs:appinfo source="http://bluestemsoftware.org/specification/eoa/1.0">
            <eoa:fragment property="tns:myProperty1">
              <eoa:query language="http://www.w3.org/TR/xpath">@myAttribute</eoa:query>
            </eoa:fragment>
          </xs:appinfo>
        </xs:annotation>
        <xs:complexType>
          <xs:attribute name="myAttribute" type="tns:mySimpleType1" />
        </xs:complexType>
      </xs:element>
    </xs:schema>
  </providers>
</deployment>

Using Expressions Within Your Applications

An XPath 1.0 expression can also be created and used programmatically within your applications as demonstrated within the following example. Note that expressions should be created within an init method if they are to be re-used, i.e. to avoid the overhead of repetitive compilation.

...

String text = "\\<birthdate xmlns=\"http://foo\"\\>\\<year\\>1970\\</year\\>\\</birthdate\\>";
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(text.getBytes("UTF-8")));

ExpressionFactory factory = SystemContext.getContext().getSystem().getExpressionFactory(TYPE);
XPathExpression xpath = ((XPathExpressionFactory)factory).createExpression("/ns:birthdate/ns:year");
xpath.addNamespace("ns", "http://foo");
Node birthdateNode = xpath.evaluateNode(document);
assert(birthdateNode.getNodeName().equals("year"));
String birthdateString = xpath.evaluateString(document);
assert(birthdateString.equals("1970"));
Double birthdateDouble = (Double)xpath.evaluateNumber(document);
assertEquals(birthdateDouble, new Double("1970"));

...