View Single Post
  #1 (permalink)  
Old September 5th, 2006, 02:52 AM
pvasudevan pvasudevan is offline
Authorized User
 
Join Date: Nov 2003
Location: , , .
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transforming data in required XML Format

Hi, I'm trying to transform the data from a text file were data is in a tab-delimited format to an xml format, which is as given below:
<UserAccessDS>
    <Access>
        <ww />
        <un />
        <rl />
    </Access>
</UserAccessDS>

The data in the text file is as given below:
1065,steo,PARIS_SPEND_DV_TPP
1099,steo,JAPAN_SPEND_IOP_TPP
1015,steo,PARIS_FUTURE_LOK_TPP

I'm expecting the output xml to be as following:
<UserAccessDS>
    <Access>
        <ww>1065</ww>
        <un>steo</un>
        <rl>PARIS_SPEND_DV_TPP</rl>
    </Access>
    <Access>
        <ww>1099</ww>
        <un>steo</un>
        <rl>JAPAN_SPEND_IOP_TPP</rl>
    </Access>
    <Access>
        <ww>1015</ww>
        <un>steo</un>
        <rl>PARIS_FUTURE_LOK_TPP</rl>
    </Access>
</UserAccessDS>

I'm using an xslt stylesheet to transform it to the required xml format using saxon transformer. Following are the parameters that identifies the root element, new line element and innermost value element:
  <xsl:param name="root" select="'UserAccessDS'"/>
  <xsl:param name="line" select="'Access'"/>
  <xsl:param name="entry" select="'ww'"/>

The innermost template that reads the innermost value from the text file delimited by a comma and writes it as the innermost element in the xml format is as follows:
<xsl:template name="tValues">
    <xsl:param name="value" select="''"/>
    <xsl:analyze-string select="$value" regex=",|\t">
      <xsl:matching-substring/>
      <xsl:non-matching-substring>
        <xsl:element name="{$entry}">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>

Currently my xml output is formatted as follows:
<UserAccessDS>
    <Access>
        <ww>1065</ww>
        <ww>steo</ww>
        <ww>PARIS_SPEND_DV_TPP</ww>
    </Access>
    <Access>
        <ww>1099</ww>
        <ww>steo</ww>
        <ww>JAPAN_SPEND_IOP_TPP</ww>
    </Access>
    <Access>
        <ww>1015</ww>
        <ww>steo</ww>
        <ww>PARIS_FUTURE_LOK_TPP</ww>
    </Access>
</UserAccessDS>

As you can see from the above format, the innermost element has the opening and closing tags as <ww>data</ww>. What I want is that each element value to be seperately placed using a different tag name. i.e. for each value between the comma, should be written inside a new tag name. So wanted to know if I could achive this, by modifying the initial parameter as:
  <xsl:param name="entry" select="'ww','un','rl'"/>
And then in the template, I could add a <xsl:for-each select="$entry"> and then map is appropriately.

Can anyone give me a tip on how I could achieve this.

Thanks,
Praveen

Reply With Quote