XSLT help
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 4
- Joined: Thu Sep 20, 2007 1:35 pm
XSLT help
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
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
-
- Posts: 89
- Joined: Mon Mar 06, 2006 10:13 pm
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
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
-
- Posts: 4
- Joined: Thu Sep 20, 2007 1:35 pm
Thanks for the reply,but the above logic will work for the only 3 fileds, but i have the 20 filedsjkmyoung 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
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
-
- Posts: 89
- Joined: Mon Mar 06, 2006 10:13 pm
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>
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>
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service