The MarkLogic Java API allows you to create custom content transformations and apply them during operations such as document ingestion and retrieval. You can also apply transformations to search results. Transforms can be implemented using server-side JavaScript, XQuery, and XSLT. A transform can accept transform-specific parameters.
You can specify default transformations as well as operation-specific transformations. For example, setting the DefaultDocumentReadTransform
property of ServerConfigurationManager
to the name of a content transformation automatically applies the transformation to every document as it is read from the database. By default, there is no default read transform. Setting up default transforms requires rest-admin
privileges.
This chapter contains the following sections:
To install a transform on your server, do the following steps:
DatabaseClient
for connecting to the database. For example, if using digest authentication:DatabaseClient client = DatabaseClientFactory.newClient( host, port, new DigestAuthContext(username, password));
ServerConfigManager
to create the manager.TransformExtensionsManager transMgr = client.newServerConfigManager().newTransformExtensionsManager();
ExtensionMetadata
object. ExtensionMetadata metadata = new ExtensionMetadata(); metadata.setTitle("XML-TO-HTML XSLT Transform"); metadata.setDescription("This plugin transforms an XML document with a known vocabulary to HTML"); metadata.setProvider("MarkLogic"); metadata.setVersion("0.1");
FileInputStream transStream = new FileInputStream( "scripts"+File.separator+TRANSFORM_NAME+".xsl"); InputStreamHandle handle = new InputStreamHandle(transStream);
transMgr.writeXSLTransform(TRANSFORM_NAME, handle, metadata);
client.release();
Once you install a transform, you can apply it under the following circumstances:
This section describes how to use transforms and includes the following topics:
A read transform receives the document from the database as input and produces the document to be returned to the client application as output. Specify a read transform by including a ServerTransform
object in your call to DocumentManager.read
.
Use the following procedure to transform a document when reading it:
DatabaseClient
for connecting to the database. For example, if using digest authentication:DatabaseClient client = DatabaseClientFactory.newClient( host, port, new DigestAuthContext(username, password));
handleXML.setMimetype("text/xml"); //OR handleJSON.setMimetype("text/json");
ServerTransform
object. Specify the transform name and any parameter values expected by the transform.ServerTransform transform = new ServerTransform(TRANSFORM_NAME); transform.put("some-param", "value");
ServerTransform
object. The read handle will contain the transformed content.XMLDocMgr.read(theDocURI, handleXML, transform); //OR JSONDocMgr.read(theDocURI, handleJSON, transform);
client.release();
A write transform receives the document from the client application as input, and should produce the document to be written to the database as output. Specify a write transform by including a ServerTransform
object in your call to DocumentManager.write
.
Use the following procedure to transform a document when writing it:
DatabaseClient
for connecting to the database. For example, if using digest authentication:DatabaseClient client = DatabaseClientFactory.newClient( host, port, new DigestAuthContext(username, password));
TextDocumentManager
.TextDocumentManager writeMgr = client.newTextDocumentManager();
FileInputStream docStream = new FileInputStream("/path/to/my.txt"); InputStreamHandle writeHandle = new InputStreamHandle(docStream);
writeHandle.setMimetype("text/xml");
ServerTransform transform = new ServerTransform(TRANSFORM_NAME); transform.put("drop-font-tags", "yes");
String theDocURI = "/examples/mydoc.xml"; writeMgr.write(docId, writeHandle, transform);
client.release();
When you apply a transform to search results, the transform receives the search response data prepared by MarkLogic Server as input, and should produce the output to be returned to the client application. For example, if the response is in XML, the input is a document with a <search:response/>
root element.
For details, see Transforming Search Results.
When you apply a transform to the results of an alerting match, the transform receives the match results prepared by MarkLogic Server as input, and should produce the output to be returned to the client application. For example, if the response is in XML, the input is a document with a <rapi:rules>
root element.
For details, see Transforming Alert Match Results.
You can list all currently installed transform extensions by doing the following:
String result = transMgr.listTransforms( new StringHandle().withFormat(Format.XML)).get(); // format can be JSON as well new StringHandle().withFormat(Format.JSON)).get();
By default, calling listTransforms()
rebuilds the transform metadata to ensure the metadata is up to date. If you find this refresh makes discovery take too long, you can disable the refresh by setting the refresh
parameter to false:
String result = transMgr.listTransforms( new StringHandle().withFormat(Format.XML), false).get(); //Or new StringHandle().withFormat(Format.JSON), false).get();
Disabling the refresh can result in this request returning inaccurate information, but it does not affect the freshness or availability of the implementation of any transforms.
To delete a transform, effectively uninstalling it from the server do the following:
transMgr.deleteTransform(TRANSFORM_NAME);
To read the source code of an XQuery implemented transform into your application, do:
StringHandle textHandle = transMgr.readXQueryTransform(TRANSFORM_NAME, new StringHandle()); // can be any text handle
To read the source code of an XSLT implemented transform into your application, do:
XMLReadHandle xHandle = transMgr.readXSLTransform(TRANSFORM_NAME, new XMLReadhandle());
To read the source code of an Javascript implemented transform into your application, do:
JSONReadHandle jHandle = transMgr.readJavascriptTransform(TRANSFORM_NAME, new JSONReadHandle());
You can write transforms using server-side JavaScript, XQuery, or XSLT. The transform interface is shared across multiple MarkLogic client APIs, so you can use the same transforms with the Java Client API, Node.js Client API, and the REST Client API. For the interface definition, authoring guidelines, and example implementations, see Writing Transformations in the REST Application Developer's Guide.
Resource service extensions, transforms, row mappers and reducers, and other hooks cannot be implemented as JavaScript MJS modules.