IBM Support

Creating UTF-8 Documents with XSLT

Technical Blog Post


Abstract

Creating UTF-8 Documents with XSLT

Body

Implementing XSLT with Special Characters/Umlauts

Are you onboarding new trading partners in EMEA or Asia? You need to create Business Data that contains Umlaut / Special Characters? Let me introduce you to an Example that makes usage of the XSLT Service in IBM Sterling B2B Integrator. It can be used to convert XML Style data into other Documents. In this Example we will use an XSLT to transform some Business Data into an HTML Reply Document that you can use to pass back information to an incoming web request.

  • Lets assume that you get some input data into your Business Process. It can be e.g. the result of an availability check in your ERP System. We will simulate this in this example by assigning some static data in the BP
  • This Business Data is then transformed into a Primary Document and stored in the BusinessProcess
  • Once we have that Primary Document we will use the XSL Transformation to convert it into an HTML Document

First we will discuss the Business Process:

  • First we assign a value to a data node
  • Then we create a Primary Document by converting that node into a standalone XML Document
  • Finally we take that Primary Document and process it with the XSLT Service
<process name="xsltGreetingTest">
	<sequence>
		<!-- create test input data -->
		<assign to="RootNode/INPUT/DATA">Hello World</assign>
		<!-- create Primary Document -->
		<assign to="." from="DOMToDoc(RootNode,'PrimaryDocument')" />
		<!-- process Primary Document with XSLT -->
		<operation name="AddGreetings">
			<participant name="XSLTService"/>
			<output message="XSLTServiceInputMessage">
				<assign to="xml_input_from">PrimaryDoc</assign>
				<assign to="xml_input_validation">NO</assign>
				<assign to="xslt_name">withGreetings</assign>
				<assign to="." from="*"/>
			</output>
			<input message="inmsg">
				<assign to="." from="*"/>
			</input>
	</sequence>			
</process>

The XSLT File

In this sample XSLT file there are several interesting points to focus on. Since it converts Data from Process Data into a HTLM Document we need to take care of formats and encodings in more than one place.

  • in the xsl:output directive you have to set method to html to make sure that the created output syntax is correct
  • you also set encoding to UTF-8 to make sure that the resulting Primary Document is actually filled with UTF-8 characters when doing the XSL Transformation
  • Inside the html Tag you set META Information to make sure that the Web Browser that reads this HTML file will be able to make the right assumptions about the content
  • Setting content to text/html makes sure that the webbrowser understands this data as displayable web page
  • charset=UTF-8 instructs the Web Browser to take the correct input filter when parsing the HTML
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
      <html>
        <head>
          <META content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
        </head>
        <body>
          <div>
            <b>
              <xsl:if test="string-length(normalize-space(INPUT/DATA)) > 0">
                <xsl:value-of select="INPUT/DATA"/><xsl:text> </xsl:text>  
              </xsl:if>                        
            </b>
          </div>
          <div>
            <p>Mit freundlichen Grüßen</p> 
            <p>こんにちは!</p>
          </div>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

The combination of the settings above makes sure that the resulting Primary Document will be stored in UTF-8 in the Sterling Integrator. This makes sure that the following steps in the BP can read the data back in and work with it as Unicode Characters. It also makes sure that the payload itself has processing information included for the Web Browser so that it knows how to deal with the data.

[{"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SS3JSW","label":"IBM Sterling B2B Integrator"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"","Edition":"","Line of Business":{"code":"LOB59","label":"Sustainability Software"}}]

UID

ibm11121835