0

I am new to XSLT. In one of my task I have to convert XML file into a text file with specific format. I am wondering is there a easy/quick way to get it done.

My XML looks like below

<?xml version="1.0" encoding="UTF-8"?>
<QTKTRes xmlns="http://www.xxx.com/app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2013-03-19T15:40:58-05:00" transLanguage="EN" baseLanguage="EN" messageID="1363722058486545315" appVersion="5 1 20110725-1550" rsStart="0" rsCount="1" rsTotal="1">
<TKTSet>
<TKT>
  <COST>0.0</COST>
  <HRS>0.0</HRS>
  <CHANGEDATE>2013-02-19T14:59:51-05:00</CHANGEDATE>
  <TKTID>101</TKTID>
  <TKTSPEC>
    <ATTRID>PMSCR</ATTRID>
    <REID>101</REID>
    <VALUEN>RDPS</VALUE>
  </TKTSPEC>
  <TKTSPEC>
    <ATTRID>PMSCQ</ATTRID>
    <REID>101</REID>
    <VALUET>RDPQ</VALUE>
  </TKTSPEC>
<TKT>
<TKTSet>
</QTKTRes>

`

My Desired Output is needed as below :-

COST=0.0&
HRS=0.0&
CHANGEDATE=2013-02-19T14:59:51-05:00&
TKTID=101&
TKTSPEC.1.ATTRID=PMSCR&
TKTSPEC.1.REID=101&
TKTSPEC.1.VALUEN=RDPS&
TKTSPEC.2.ATTRID=PMSCR&
TKTSPEC.2.REID=101&
TKTSPEC.2.VALUET=RDPQ

Is there a tool I can make use of or I need to manually write XSLT stylesheet . I am new to this area. Your valuable answers are welcome.

1 Answer 1

1

If you just want to list the child elements of TKT with their node name followed by =, their value, and & on all rows but the last, this should work:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:app="http://www.xxx.com/app">
  <xsl:output method="text" />
  <xsl:strip-space elements="*" />

  <xsl:template match="app:TKT//*">
    <xsl:param name="prefix" />
    <xsl:param name="inLast" select="true()" />

    <xsl:value-of select="concat($prefix, local-name(), '=', normalize-space())"/>
    <xsl:if test="not($inLast) or position() != last()">
      <xsl:text>&amp;&#xA;</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="app:TKT//*[*]">
    <xsl:param name="inLast" select="true()" />
    <xsl:variable name="num">
      <xsl:number />
    </xsl:variable>

    <xsl:apply-templates>
      <xsl:with-param name="prefix" 
                      select="concat(local-name(), '.', $num, '.')" />
      <xsl:with-param name="inLast" 
                      select="$inLast and position() = last()" />
    </xsl:apply-templates>
  </xsl:template>
</xsl:stylesheet>

When run on your sample input, this produces:

COST=0.0&
HRS=0.0&
CHANGEDATE=2013-02-19T14:59:51-05:00&
TKTID=101&
TKTSPEC.1.ATTRID=PMSCR&
TKTSPEC.1.REID=101&
TKTSPEC.1.VALUE=RDPS&
TKTSPEC.2.ATTRID=PMSCQ&
TKTSPEC.2.REID=101&
TKTSPEC.2.VALUE=RDPQ
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again for your respone.. I am trying to alter the XSLT to fit an another scenario. Consider my input as updated now with TKTSPEC values , I am trying to come up with an output based on your xslt . Is this possible to achieve?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.