c# - Append template from a XSLT into another XSLT -
i working in visual studio 2012 c#. have 2 xslt files. 1 has few templates. has nodes defined there. want build function in c# using pass template name. using name search in 1 xslt , if there template given name, copies on second xslt.
f("getmonth") should result following:
xslt1: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template name="getmonth"> <xsl:param name="month"/> <xsl:param name="putcall"/> <xsl:value-of select ="'a'"/> </xsl:template> xslt2: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <documentelement> // code written </documentelement> </xsl:template> </xsl:stylesheet> resultant xslt2: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template name="getmonth"> <xsl:param name="month"/> <xsl:param name="putcall"/> <xsl:value-of select ="'a'"/> </xsl:template> <xsl:template match="/"> <documentelement> // tags defined here </documentelement> </xsl:template> </xsl:stylesheet>
my attempt:
xmldocument xsldoc1 = new xmldocument(); xmldocument xsldoc2 = new xmldocument(); xsldoc1.load("xslt1.xslt"); xsldoc2.load("xslt2.xslt"); xmlnamespacemanager nsmg1r = new xmlnamespacemanager(xsldoc1.nametable); nsmgr1.addnamespace("xsl", "http://www.w3.org/1999/xsl/transform"); xmlnamespacemanager nsmgr2 = new xmlnamespacemanager(xsldoc2.nametable); nsmgr2.addnamespace("xsl", "http://www.w3.org/1999/xsl/transform"); xmlnodelist template = (xmlnodelist)xsldoc.selectnodes("/xsl:stylesheet/xsl:template[@name = templatename]", nsmgr); if(template != null) { // code should written here??? }
please suggest.
string templatename = "getmonth"; xmlnode template = xsldoc.selectsinglenode(string.format("/xsl:stylesheet/xsl:template[@name = '{0}']", templatename), nsmgr); if (template != null) { // append template last child of xsl:stylesheet xsldoc2.documentelement.appendchild(xsldoc2.importnode(template, true)); // alternative insert first child use // xsldoc2.documentelement.insertbefore(xsldoc2.importnode(template, true), xsldoc2.documentelement.firstchild); // save xsldoc2.save("xslt2.xslt"); }
Comments
Post a Comment