How to transform a XML using XSLT only modifying a node that's required? -
i'm trying transform xml replace node name another, xslt framed messing transformed xml. might small issue might have overlooked, stuck long time.
input xml have:
<?xml version="1.0" encoding="utf-8"?> <datacollection> <queryconst language="dbsql"> <queryused> select emp.firstname, emp.lastname (select firstname, lastname employees employeeid < 10) emp </queryused> </queryconst> <record> <old> <employees> <firstname>james</firstname> <lastname>gosling</lastname> </employees> </old> </record> <record> <old> <employees> <firstname>rod</firstname> <lastname>johnson</lastname> </employees> </old> </record> </datacollection>
the xslt used follows:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" /> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*" /> </xsl:copy> </xsl:template> <xsl:template match="datacollection/queryconst/@language"> <xsl:attribute name="language">dbview</xsl:attribute> </xsl:template> <xsl:template match="datacollection/record/old/employees"> <xsl:element name="table"> <xsl:copy> <xsl:apply-templates select="node()|@*" /> </xsl:copy> </xsl:element> </xsl:template> </xsl:stylesheet>
the output xml want follows:
<?xml version="1.0" encoding="utf-8"?> <datacollection> <queryconst language="dbview"> <queryused> select emp.firstname, emp.lastname (select firstname, lastname employees employeeid < 10) emp </queryused> </queryconst> <record> <old> <table> <firstname>james</firstname> <lastname>gosling</lastname> </table> </old> </record> <record> <old> <table> <firstname>rod</firstname> <lastname>johnson</lastname> </table> </old> </record> </datacollection>
any / tips / suggestions on above appreciated.
you need remove xsl:copy
template matching employees
. also, there's no need use xsl:element
when element name known. try simply:
<xsl:template match="datacollection/record/old/employees"> <table> <xsl:apply-templates/> </table> </xsl:template>
note match
pattern not select
expression. don't need use path unless have other employees
nodes elsewhere in input xml. given example,
<xsl:template match="employees">
would quite sufficient.
Comments
Post a Comment