i have code xml output as
<?xml version="1.0" encoding="windows-1252" standalone="yes"?> <products> <product> <name>abc</name> <id>1</id> </product> <product> <name>klm</name> <id>2</id> </product> </products>
i want same xml displayed in following format well:
<?xml version="1.0" encoding="windows-1252" standalone="yes"?> <products> <product> <name> <value>abc</value> <unit></unit> </name> <id> <value>1</value> <unit></unit> </id> <product> <name> <value>klm</value> <unit></unit> </name> <id> <value>2</value> <unit></unit> </id> </product>
how can using xslt?
i using spring environment. xml tags product variable. these vary kind of products. code generate xml is
jaxbcontext jc; try { jc = jaxbcontext.newinstance(cla); marshaller m; m = jc.createmarshaller(); m.marshal(obj, out); } catch (jaxbexception e) { e.printstacktrace(); }
edit: product may have weight attribue, there tag of
<weight>10lbs<weight>
this tage break
<weight> <value>10</value> <unit>lbs</unit> </weight>
here sample stylesheet [edited reflect new requirement weight element]:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <xsl:template match="products"> <xsl:copy> <product> <xsl:apply-templates select="product/*"/> </product> </xsl:copy> </xsl:template> <xsl:template match="product/*"> <xsl:copy> <value> <xsl:value-of select="."/> </value> <unit></unit> </xsl:copy> </xsl:template> <xsl:template match="product/weight" priority="3"> <xsl:copy> <value> <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/> </value> <unit> <xsl:value-of select="translate(., '0123456789', '')"/> </unit> </xsl:copy> </xsl:template> </xsl:stylesheet>
when apply sample input
<products> <product> <name>abc</name> <id>1</id> </product> <product> <name>klm</name> <id>2</id> </product> <product> <name>foo</name> <id>3</id> <weight>10lbs</weight> </product> </products>
i
<products> <product> <name> <value>abc</value> <unit/> </name> <id> <value>1</value> <unit/> </id> <name> <value>klm</value> <unit/> </name> <id> <value>2</value> <unit/> </id> <name> <value>foo</value> <unit/> </name> <id> <value>3</value> <unit/> </id> <weight> <value>10</value> <unit>lbs</unit> </weight> </product> </products>
based on alejandro's comment might have misread wanted output , don't want merge products, in case use
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="product/*"> <xsl:copy> <value> <xsl:value-of select="."/> </value> <unit></unit> </xsl:copy> </xsl:template> <xsl:template match="product/weight" priority="3"> <xsl:copy> <value> <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/> </value> <unit> <xsl:value-of select="translate(., '0123456789', '')"/> </unit> </xsl:copy> </xsl:template> </xsl:stylesheet>
then get
<products> <product> <name> <value>abc</value> <unit/> </name> <id> <value>1</value> <unit/> </id> </product> <product> <name> <value>klm</value> <unit/> </name> <id> <value>2</value> <unit/> </id> </product> <product> <name> <value>foo</value> <unit/> </name> <id> <value>3</value> <unit/> </id> <weight> <value>10</value> <unit>lbs</unit> </weight> </product> </products>
Comments
Post a Comment