Metadata as Parameters to Stylesheets

How to pass parameters into a stylesheet when you’re using an adapter

I’m never really been all that interested in data manipulation; one of my stock phrases is it’s just data1. From an integration perspective I might never understand the business semantics around each field in your data file. The critical part of our work is to make sure that your data gets out of System A, and gets to System B in the right format. For me, the interesting part is always understanding the esoteric limitations or complexities around the communications protocol rather than the data formatting itself. This probably goes back to messing around with protocol analyzers and gender benders (not that kind; the kind that you fit to an RS232 port) when I first started.

Anyway, the adapter has an EDI transformation engine which can turn XML into its appropriate EDI representation; one of requirements of EDI is that you need to have a document number for each document. Most of the time, this needs to be generated by us as the original data doesn’t have it. Previously someone; they shall remain nameless (you know who you are); wrote a simplistic java class that wasn’t threadsafe, relied on lots of java behaviour that was prone to errors, to increment a number in a hashmap and this ended up being used in a number of mappings which are now being reviewed. The extensions provided by Xalan that enable behaviour this within a stylesheet is yet another reason why the adapter hasn’t quite moved away from Xalan yet.

Since 2.5.0 (a very long time ago now) the XML Transform service has had the ability to receive all the message’s metadata as parameters to the stylesheet. Using this feature along with one of the concrete implementations of AbstractJdbcSequenceNumberService it’s a simple thing to handle sequence number generation.

First of all we need to have an XML Transform that requires parameters; this is probably the most complex transform that you’ll ever see me write. Honestly, it’s just data.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <xsl:param name="sequence_number"/>

  <xsl:template match="/">
    <Root>
      <SequenceNumber><xsl:value-of select="$sequence_number"/></SequenceNumber>
      <xsl:copy-of select="." />
    </Root>
  </xsl:template>
</xsl:stylesheet>

Then we need to configure our adapter to

  1. Generate a sequence number
  2. Execute the transform with metadata passed in as parameters

In this example, I have for simplicity, used StaticIdentitySequenceNumberService to generate the sequence number. You could also opt to use MetadataIdentitySequenceNumberService instead.

  <service xsi:type="java:com.adaptris.core.services.jdbc.StaticIdentitySequenceNumberService">
    <connection xsi:type="java:com.adaptris.core.jdbc.JdbcConnection">
      <driver-imp>com.mysql.jdbc.Driver</driver-imp>
      <connect-url>jdbc:mysql://localhost:3306/mydatabase</connect-url>
    </connection>
    <always-replace-metadata>true</always-replace-metadata>
    <metadata-key>sequence_number</metadata-key>
    <identity>MyStaticIdentity</identity>
    <number-format>000000000</number-format>
  </service>
  <service xsi:type="java:com.adaptris.core.transform.XmlTransformService">
    <url>../transforms/transform-with-param.xsl</url>
    <use-metadata-as-stylesheet-parameters>true</use-metadata-as-stylesheet-parameters>
  </service>

If you have used the stock StaticIdentitySequenceNumberService then you will find that the table contains the next number in the sequence. That’s intentional and by design.

  1. According to company lore there are quite a number of features and behaviours that are ascribed to my personality; like all things that other people say about you, I can’t quite see it myself. Still I have found myself saying It’s just data on more than one occasion. ↩︎



© 2017. All rights reserved.

Powered by Hydejack v6.6.1