Hi,
We are migration data from older to newer version, we are using XSLT to transform the source XML to build latest XML.
Some conditions have to be evaluated during transformation, I have listed 2 of them below:
1. Replace the old users with new users
2. SKIP building a <Folder> tag if the created date is prior to 2007.
Below is my
Source XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<XML>
<Folder id="id74" name="Folder 1">
<Data id="id281">
<Values value="2004-09-07" title="date_created"/>
<Values value="sraju670" title="user_created"/>
</Data>
</Folder>
<Folder id="id741" name="Folder 2">
<Data id="id282">
<Values value="2007-09-07" title="date_created"/>
<Values value="sudheer" title="user_created"/>
</Data>
</Folder>
<Folder id="id742" name="Folder 3">
<Data id="id283">
<Values value="2006-09-07" title="date_created"/>
<Values value="raju" title="user_created"/>
</Data>
</Folder>
<Folder id="id748" name="Folder 4">
<Data id="id284">
<Values value="2009-09-07" title="date_created"/>
<Values value="naga" title="user_created"/>
</Data>
</Folder>
</XML>
In this example Folder1 & Folder3 shouldn't be exported as they are created before 2007.
The user "naga" for Folder4 is a old user (in-active), so his name has to be replaced with "admin".
I need to write a XSL which would fulfill both these conditions and build a output XML file, something like this:
OUTPUT XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<XML>
<Folder id="id741" name="Folder 2">
<Data id="id282">
<Values value="2007-09-07" title="date_created"/>
<Values value="sudheer" title="user_created"/>
</Data>
</Folder>
<Folder id="id748" name="Folder 4">
<Data id="id284">
<Values value="2009-09-07" title="date_created"/>
<Values value="admin" title="user_created"/>
</Data>
</Folder>
</XML>
Below is what I have tried so far:
1. Looping through Folder, Data, Values using <xsL:for-each>
2. Getting @title value,
A. If @title is 'date_created' then check if the date is later to 2007.
B. If @title is 'user_created' then check if user is 'naga' (or other users from another XML file) and then replace with admin.
3. Try building <Folder> tag based on date_created condition, BUT IT IS FAILING.
Below is my overall code:
Code:
<xsl:template name="rootTemplate">
<xsl:for-each select="XML/Folder">
<xsl:for-each select="Data">
<xsl:for-each select="Values">
<xsl:variable name="title" select="@title"/>
<xsl:variable name="attribute_type" select="@title"/>
<xsl:if test="$attribute_type = 'owning_user'">
<xsl:message> owning_user : <xsl:value-of select="@value" /></xsl:message>
</xsl:if>
<xsl:if test="$attribute_type = 'last_mod_date'">
<xsl:variable name="last_modified_date_value" select="@value"/>
<xsl:message> last_mod_date : <xsl:value-of select="$last_modified_date_value" /></xsl:message>
<xsl:variable name="last_mod_date_formatted" select="xs:integer(translate(substring($last_modified_date_value,1,10),'-',''))"/>
<xsl:message> last_mod_date_formatted: <xsl:value-of select="$last_mod_date_formatted"/></xsl:message>
<xsl:choose>
<xsl:when test="$last_mod_date_formatted >= 20070101">
<xsl:message> Input Date : <xsl:value-of select="$last_mod_date_formatted"/> is greater than or equal to 20070101, so MIGRATE</xsl:message>
<xsl:element name="Folder">
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:element name="Data">
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<xsl:element name="Values">
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:message> Input Date : <xsl:value-of select="$last_mod_date_formatted"/> is less than or equal to 20070101, so SKIP </xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Any help is highly appreciated.