Page 1 of 1
XSLT help
Posted: Thu Sep 20, 2007 1:51 pm
by ravi
Hi,
am new to XSLT, i have the MQ message that message am able to read from queue, that message in the below format
<payload>aaa,111,ap,india</payload>
i need a help to convert this MQ message into the XML message using the XSLT
means expected output should be like this
<?xml version="1.0" encoding="UTF-8"?>
<empdetails><empname>abc</empname><empid>111</empid><empstate>ap</empstate><empcountry>india<empcountry></empdetails>
am totally new to this XSLT, it would be gr8 if some one provide me the XSLT for this
Thanks in advance
Posted: Thu Sep 20, 2007 10:47 pm
by jkmyoung
If you're using XSLT 2.0, this can easily be done with the tokenize function. Otherwise you'll have to use functions substring-before() and substring-after()
eg:
<empname><xsl:value-of select="substring-before(payload,',')"/></empname>
<empid><xsl:value-of select="substring-before(substring-after(payload ,',') ,',')"/></empid>
and so forth. You can use temporary variables to lessen the confusion.
http://www.w3schools.com/xpath/xpath_functions.asp
Posted: Fri Sep 21, 2007 12:48 pm
by ravi
jkmyoung wrote:If you're using XSLT 2.0, this can easily be done with the tokenize function. Otherwise you'll have to use functions substring-before() and substring-after()
eg:
<empname><xsl:value-of select="substring-before(payload,',')"/></empname>
<empid><xsl:value-of select="substring-before(substring-after(payload ,',') ,',')"/></empid>
and so forth. You can use temporary variables to lessen the confusion.
http://www.w3schools.com/xpath/xpath_functions.asp
Thanks for the reply,but the above logic will work for the only 3 fileds, but i have the 20 fileds
if the above logic works for the 20 fileds, could you please let me know how it's work's
for third filed i have used like this
<empcountry><xsl:value-of select="substring-before(substring-after(datastream/payload,','),',')"/></empcountry>
but after this am not getting how to do for the next field
please help me
Posted: Fri Sep 21, 2007 4:56 pm
by jkmyoung
Well if you did it this way, the 3rd field would be
substring-before(substring-after(substring-after(payload ,','),',') ,',')
which quickly gets ridiculous.
I suggest a recursive approach instead.
Global variable:
<xsl:variable name="numFields" select="20"/>
Inside template:
<xsl:call-template name="parse">
<xsl:with-param name="str" select="payload">
<xsl:with-param name="index" select="1">
</xsl:call-template>
After template:
<xsl:template name="parse">
<xsl:param name="str">
<xsl:param name="index">
<xsl:variable name="value">
<xsl:value-of select="substring-before($str, ',')"/>
<xsl:if test="$index = $numFields"><xsl:value-of select="$str"/></xsl:if>
</xsl:variable>
<xsl:variable name="field">
<xsl:choose>
<xsl:when test="$index = 1">empname</xsl:when>
<xsl:when test="$index = 2">empid</xsl:when>
<xsl:when test="$index = 3">empstate</xsl:when>
<xsl:when test="$index = 4">empcountry</xsl:when>
....
</xsl:choose>
</xsl:variable>
<xsl:element name="{$field}"><xsl:value-of select="$value"/></xsl:element>
<xsl:if test="$index < $numFields>
<xsl:call-template name="parse">
<xsl:with-param name="str" select="substring-after($str, ',')">
<xsl:with-param name="index" select="$index + 1">
</xsl:call-template>
</xsl:if>
</xsl:template>