XML conversion using XSLT -



XML conversion using XSLT -

i trying convert xml using xslt. below construction of xml:

<?xml version="1.0" encoding="utf-8"?> <getinvoicelist> <request> <billstatuscode typecode="1">type description</billstatuscode> <ebillprocessstatuscode typecode="2">type description</ebillprocessstatuscode> </request> </getinvoicelist>

i tried xslt code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()"> <xsl:copy> <xsl:element name="{name()}"> <xsl:apply-templates select="node()"/> </xsl:element> <xsl:apply-templates select="@*"/> </xsl:copy> </xsl:template> <xsl:template match="@*"> <xsl:element name="{name()}"> <xsl:value-of select="."/> </xsl:element> </xsl:template> </xsl:stylesheet>

output:

<getinvoicelist> <getinvoicelist> <request> <request> <billstatuscode> <billstatuscode>type description</billstatuscode> <typecode>1</typecode> </billstatuscode> <ebillprocessstatuscode> <ebillprocessstatuscode>type description</ebillprocessstatuscode> <typecode>2</typecode> </ebillprocessstatuscode> </request> </request> </getinvoicelist> </getinvoicelist>

expected output:

<getinvoicelist> <request> <billstatuscode> <billstatuscode>type description</billstatuscode> <typecode>1</typecode> </billstatuscode> <ebillprocessstatuscode> <ebillprocessstatuscode>type description</ebillprocessstatuscode> <typecode>2</typecode> </ebillprocessstatuscode> </request> </getinvoicelist>

the code working on nodes why duplicates nodes got created. want work on nodes having text & attributes.

would appreciate help regarding this. thanks!

the problem in node() matching template; have both xsl:copy , xsl:element, both create same element, resulting in duplicate. applies nodes

<xsl:template match="node()"> <xsl:copy> <xsl:element name="{name()}">

what need alter match elements attributes

<xsl:template match="*[@*]"> <xsl:copy> <xsl:copy> <xsl:apply-templates select="node()"/> </xsl:copy> <xsl:apply-templates select="@*"/> </xsl:copy> </xsl:template>

then, have template matching node() in remove xsl:element element

<xsl:template match="node()"> <xsl:copy> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template>

try xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()"> <xsl:copy> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template> <xsl:template match="*[@*]"> <xsl:copy> <xsl:copy> <xsl:apply-templates select="node()"/> </xsl:copy> <xsl:apply-templates select="@*"/> </xsl:copy> </xsl:template> <xsl:template match="@*"> <xsl:element name="{name()}"> <xsl:value-of select="."/> </xsl:element> </xsl:template> </xsl:stylesheet>

xml xslt

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -