Transforming to a simple text file

We've already done most of the work involved in setting up the environment to transform the same DXL output file we used in the last example for XML output. In this case, we've copied the original Domino agent TransformXMLtoXMLfile and renamed the new copy TransformXMLtoTextFile. In the new agent we've specified a new XSL stylesheet to use for the transformation and a new output file name. Refer to 4.3, "Transforming from XML to XML" on page 212 to get an overview of the environment and the Java agent that is used to generate this example. The new output file name is called c:\\temp\\textoutput.txt. The only real work is creating a new stylesheet to squirt out text instead of XML. The new stylesheet is called c:/temp/DXLtoFishText.xsl. Here is the stylesheet in it's entirety:

<?xml version="1.0"?> <xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/">

Fish IDs: <xsl:apply-templates select = "/document/item[@name='FishID']"/>

Fish Names: <xsl:apply-templates select =

Subcategories: <xsl:apply-templates select =

"/document/item[@name='Subcategory']"/>

Fish Descriptions: <xsl:apply-templates select =

"/document/item[@name='FishDescription']"/>

</xsl:template>

<xsl:template match="item[@name='FishID']">

<xsl:value-of select = "."/> </xsl:template>

<xsl:template match="item[@name='FishName']">

<xsl:value-of select = "."/> </xsl:template>

<xsl:template match="item[@name='Subcategory']">

<xsl:value-of select = "."/> </xsl:template>

<xsl:template match="item[@name='FishDescription']">

Chapter 4. Transforming XML data in Domino 229

<xsl:value-of select = "."/> </xsl:template>

</xsl:stylesheet>

In order to make a successful transformation from the DXL document to text, a few things have to be kept in mind. First of all, make sure that your output method is set to text:

<xsl:output method="text"/>

In this case, as with the previous XML to XML example, we wanted to strip away any extra Domino systems fields and document properties and focus on the fish content in the original DXL data. To do this we had to make some select statements:

<xsl:template match="/">

Fish IDs: <xsl:apply-templates select = "/document/item[@name='FishID']"/>

Fish Names: <xsl:apply-templates select =

Subcategories: <xsl:apply-templates select =

"/document/item[@name='Subcategory']"/>

Fish Descriptions: <xsl:apply-templates select =

"/document/item[@name='FishDescription']"/>

</xsl:template>

In this case we just want the FishID, FishName, Subcategory and FishDescription fields specifically from the root node in the DOM. This meant that all other elements will be ignored. Because the DXL output has written these Domino fields (and most standard output besides document properties) to elements called item, we need to retrieve the name attribute from each element to choose the elements we want. This is done using the @ symbol followed by the attribute name that we want to return the value of. We also add some text to the beginning of each line, which will be appended before writing the values of the XSL match templates:

<xsl:template match="item[@name=,FishID']">

<xsl:value-of select = "."/> </xsl:template>

<xsl:template match="item[@name='FishName']">

<xsl:value-of select = "."/> </xsl:template>

<xsl:template match="item[@name='Subcategory']">

<xsl:value-of select = "."/> </xsl:template>

230 XML Powered by Domino

<xsl:template match="item[@name='FishDescription,]">

<xsl:value-of select = "."/> </xsl:template>

This code simply returns all the values in the original DXL file that have a name attribute that matches the FishID, FishName, Subcategory, and FishDescription fields. This takes us to the end result, which is the output text itself, located in c:\temp\textoutput.txt:

Fish IDs: 640641642643644645646647648649650651652653 Fish Names: Shark

Subcategories: BlacktipBronze WhalerEastern SchoolGrey NurseGummyHammerheadotherPencilSpurdogThickskinTigerWhiskeryWhite PointerWobbegong Fish Descriptions:

SharkSharkSharkSharkSharkSharkSharkSharkSharkSharkSharkSharkSharkShark

0 0

Post a comment

  • Receive news updates via email from this site