Adams Bros Blog

7Feb/1042

What is JAXB and JAXB Example Code

Posted by Trenton

I thought it may be nice for those googlers that want to know about JAXB, to have a quick working example to use.  So, that is the purpose of this post.

22Nov/094

XSL Break or Wrap String on Word Boundary

Posted by Trenton

I've searched all over the Internet for this, and was unable to find anything reasonable.  I found an example somewhere, of how to break a string at a specific location, but it breaks whether there is a word there or not.  So, either you have to re-compose the XML elements without a space, and hope every system you interact with does the same thing as you,  or re-compose them with a space, and a word may then be broken up in the end result.

In my example below, I break a string on a word boundary, outputting to an XML element called "NoteMessage" from the PESC standard.  This is dependant on Java, but you could use any language that has a useful lastIndexOf function.  In the case of Java, with zero based string index, we need to compensate for the one based string index that XSL has.  So, we add one to the result of the lastIndexOf() call.

<!--
Example...
java -cp target/dependency/xml-util-0.1.3-SNAPSHOT.jar:/usr/share/xalan/lib/xalan.jar \
org.apache.xalan.xslt.Process -XSL target/classes/xsl/notemessage.xsl \
 -PARAM testString "This is a test to break a string into multiple note messages, \
automatically, without programming."
-->
<xsl:stylesheet version="1.0"
                xmlns:String="http://xml.apache.org/xalan/java/java.lang.String"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="${xml.indent}" method="xml" omit-xml-declaration="yes"/>
  <xsl:param name="testString"/>
  <xsl:variable name="break-at" select="'76'"/>

  <xsl:template match="/">
    <xsl:call-template name="note-message">
      <xsl:with-param name="string" select="$testString"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="note-message">
    <xsl:param name="string"/>
    <xsl:choose>
      <xsl:when test="string-length($string) <= $break-at">
        <xsl:element name="NoteMessage">
        <xsl:value-of select="$string"/>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <!-- call method to find word boundary index -->
        <xsl:variable name="truncString"
                      select="String:new(substring($string, 1, $break-at))"/>
        <xsl:variable name="lastSpaceIndex"
                      select="String:lastIndexOf($truncString, ' ') + 1"/>
        <xsl:element name="NoteMessage" namespace="">
          <xsl:value-of select="substring(string($truncString), 1, $lastSpaceIndex)"/>
        </xsl:element>
        <xsl:call-template name="note-message">
          <xsl:with-param name="string"
                          select="substring($string, $lastSpaceIndex + 1)"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
Filed under: Java, Programming, XSL 4 Comments