Transforming a DCR into another DCR – part 1: the stylesheet

Posted: 11 November 2009 in FormsPublisher, Presentation Template, Teamsite, XSL presentation templates
Tags: , , , , , ,

From time to time, your data requirements will change. This cascades down to the data types you use. Adding new fields, removing others, changing values are all part of the evolution of your data so you should be prepared to transform it.

In this article, I will focus on migrating DCRs from one type to another. By way of example, I will use a fictitious intranet article type and something similar but different with portal news.

Intranet article Portal news
Article/type
Article/language
Article/region
Article/global
Article/category
Article/title Article/title
Article/date Article/date
Article/sortdate Article/sortdate
Article/content Article/content
Article/synopsis Article/synopsis
Article/image Article/image
Article/displayImage Article/showImage

The types are similar, as in real life your types will be similar: that’s why you’re changing them.

The plan is to wrtie an XSL stylesheet that wil help us transform DCR XML files of one type into DCR XML files of another type. This will be achieved through a java class that will scan the directory, perform the transformation and declare the resulting files as DCRs of the right type by setting some extended attributes on them. Here’s a sample source intranet article:

<?xml version="1.0" encoding="UTF-8"?>
<article>
 <title>lorem</title>
 <date>01 January 1970</date>
 <sortdate>01 January 1970</sortdate>
 <content><![CDATA[<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>]]></content>
 <synopsis><![CDATA[<p>Morbi feugiat nunc non urna auctor venenatis pulvinar nisi commodo.</p>]]></synopsis>
 <image>/resources/logo.gif</image>
 <displayImage>true</displayImage>
</article>

I create the XSL transformation template. I put it in templatedata/intranet/article/presentation. the file name is toPortalNews.xsl.

<?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"/>
    <xsl:template match="/article">
        <article>
        </article>
    </xsl:template>
</xsl:stylesheet>

It is best to deal with the fields in order. The missing values will be defaulted:

  • Article/language: en-gb
  • Article/region: emea
  • Article/global: false
  • Article/category: news
<?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"/>
    <xsl:template match="/article">
        <article>
            <language>en-gb</language>
            <region>emea</region>
            <global>false</global>
            <category>news</category>
        </article>
    </xsl:template>
</xsl:stylesheet>

For the other fields, I can select them and copy them directly as they are identical, using the xsl:copy command:

<?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"/>
 <xsl:template match="/article">
 <article>
 <language>en-gb</language>
 <region>emea</region>
 <global>false</global>
 <category>news</category>
 <xsl:apply-templates select="title|date|sortdate|content|synopsis|image" />
 </article>
 </xsl:template>

 <xsl:template match="*">
 <xsl:copy>
 <xsl:apply-templates select="@*"/>
 <xsl:apply-templates />
 </xsl:copy>
 </xsl:template>

 <xsl:template match="@*">
 <xsl:copy />
 </xsl:template>

</xsl:stylesheet>

For the last field which has the same value but a different name, I can use a simple xsl:value-of and encase that in the new field. this goes after the xsl:apply-template command in my example.

<showImage><xsl:value-of select="displayImage"/></showImage>

and the result is

<?xml version=”1.0″ encoding=”UTF-8″?>
<article>
<language>en-gb</language>
<region>emea</region>
<global>false</global>
<category>news</category>
<title>lorem</title>
<date>01 January 1970</date>
<sortdate>01 January 1970</sortdate>
<content>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/p&gt;</content>
<synopsis>&lt;p&gt;Morbi feugiat nunc non urna auctor venenatis pulvinar nisi commodo.&lt;/p&gt;</synopsis>
<image>/resources/logo.gif</image>
<showImage>true</showImage>
</article>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s