New XML creation using XSLT -
i have input xml message trying tranform different xml altogether values taken input. can please , guide me sample xslt?
input xml:
<req_in> <reqid_in>reqidin001</reqid_in> <reqtype_in>azz002</reqtype_in> <vmid_in>vmidinv003</vmid_in> <psmid_in>psmidin004</psmid_in> <sidhiq_in>sssiiidddd54005</sidhiq_in> <sip_in>sip_in_10.20.30.40</sip_in> <mpass_in>full details of buyer</mpass_in> <currency_in>usd</currency_in> <amt_in>167.78</amt_in> <midets_in> <itcode_in>a</itcode_in> <itdesc_in>aadesc</itdesc_in> <ituntprice_in>555</ituntprice_in> <itquan_in>41</itquan_in> </midets_in> <midets_in> <itcode_in>b</itcode_in> <itdesc_in>bbbdesc</itdesc_in> <ituntprice_in>44343</ituntprice_in> <itquan_in>32</itquan_in> </midets_in> <midets_in> <itcode_in>c</itcode_in> <itdesc_in>abcdesc</itdesc_in> <ituntprice_in>8979</ituntprice_in> <itquan_in>32</itquan_in> </midets_in> <conbdetails_in> <cname_in>firstname lastname</cname_in> <conaddress1>conbdetails addressline1 </conaddress1> <conaddress2>conbdetails addressline2</conaddress2> <conaddress3>conbdetails addressline3</conaddress3> <city_in>conbdetails_city</city_in> <pcode_in>conbdetails_pin</pcode_in> <c_in>conbdetails us</c_in> </conbdetails_in> <mshipdets_in> <fullname>fname lname</fullname> <meraddress1>mship addressline1</meraddress1> <meraddress2>mship addressline2</meraddress2> <meraddress3>mship addressline3</meraddress3> <city_in>mship city</city_in> <pcode_in>mship pin</pcode_in> <c_in>mship us</c_in> </mshipdets_in> <vmetransactionidentifier> <vmeuniqueid> a9001</vmeuniqueid> <authenticationmethod>01</authenticationmethod> <authenticationreasoncode>1c</authenticationreasoncode> </vmetransactionidentifier> </req_in>
output xml:
<req_in_new> <psmid_in_new>value of psmid_in element(psmidin004)</psmid_in_new> <reqid_in_new>value of reqid_in element (reqidin001)</reqid_in_new> <spt_new>value of reqid_in element (reqidin001)</spt_new> <conbdetails_in_new> <cname_in_new>firstname lastname</cname_in_new> <street1>conbdetails addressline1 </street1> <street2>conbdetails addressline2 </street2> <street3>conbdetails addressline3</street3> <city>conbdetails_city</city> <postalcode>conbdetails_pin</postalcode> <country>conbdetails_us</country> <ipaddress>value of sip_in element (sip_in_10.20.30.40)</ipaddress> </conbdetails_in_new> <mpass_in_new>full details of buyer </mpass_in_new> <mshipdets_in_new> <fullname_new>fname lname</fullname_new> <meraddress1_new>mship addressline1</meraddress1_new> <meraddress2_new>mship addressline2</meraddress2_new> <meraddress3_new>mship addressline3</meraddress3_new> <city_in_new>mship city</city_in_new> <pcode_in_new>mship pin</pcode_in_new> <c_in_new>mship us</c_in_new> </mshipdets_in_new> <midets_in_new id="0"> <itcode_in_new>a</itcode_in_new> <itdesc_in_new>aadesc</itdesc_in_new> <ituntprice_in_new>555</ituntprice_in_new> <itquan_in_new>41</itquan_in_new> </midets_in_new> <midets_in_new id="1"> <itcode_in_new>b</itcode_in_new> <itdesc_in_new>bbbdesc</itdesc_in_new> <ituntprice_in_new>44343</ituntprice_in_new> <itquan_in_new>32</itquan_in_new> </midets_in_new> <midets_in_new id="2"> <itcode_in_new>c</itcode_in_new> <itdesc_in_new>abcdesc</itdesc_in_new> <ituntprice_in_new>8979</ituntprice_in_new> <itquan_in_new>41</itquan_in_new> </midets_in_new> <totalamt> <amt_in_new>167.78</amt_in_new> <currency_in_new>usd</currency_in_new> </totalamt> </req_in_new>
this transform ask. template root element picks out elements source in order appear in output, general-purpose template copies each element, adding _new
name.
a number of other templates processes elements handled differently rest.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:template match="/req_in"> <req_in_new> <xsl:apply-templates select="psmid_in"/> <xsl:apply-templates select="reqid_in"/> <xsl:apply-templates select="conbdetails_in"/> <xsl:apply-templates select="mpass_in"/> <xsl:apply-templates select="mshipdets_in"/> <xsl:apply-templates select="midets_in"/> <totalamt> <amt_in_new> <xsl:value-of select="amt_in"/> </amt_in_new> <currency_in_new> <xsl:value-of select="currency_in"/> </currency_in_new> </totalamt> </req_in_new> </xsl:template> <xsl:template match="*"> <xsl:element name="{concat(name(), '_new')}"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="reqid_in"> <xsl:element name="{concat(name(), '_new')}"> <xsl:apply-templates/> </xsl:element> <spt_new> <xsl:apply-templates/> </spt_new> </xsl:template> <xsl:template match="conbdetails_in"> <xsl:element name="{concat(name(), '_new')}"> <xsl:apply-templates/> <ipaddress> <xsl:value-of select="preceding-sibling::sip_in"/> </ipaddress> </xsl:element> </xsl:template> <xsl:template match="conbdetails_in/*[starts-with(name(),'conaddress')]"> <xsl:element name="{concat('street', substring-after(name(), 'conaddress'))}"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="conbdetails_in/city_in"> <city> <xsl:apply-templates/> </city> </xsl:template> <xsl:template match="conbdetails_in/pcode_in"> <postalcode> <xsl:apply-templates/> </postalcode> </xsl:template> <xsl:template match="conbdetails_in/c_in"> <country> <xsl:apply-templates></xsl:apply-templates> </country> </xsl:template> <xsl:template match="midets_in"> <xsl:element name="{concat(name(), '_new')}"> <xsl:attribute name="id"> <xsl:value-of select="position()-1"/> </xsl:attribute> <xsl:apply-templates/> </xsl:element> </xsl:template> </xsl:stylesheet>
output
<req_in_new> <psmid_in_new>psmidin004</psmid_in_new> <reqid_in_new>reqidin001</reqid_in_new> <spt_new>reqidin001</spt_new> <conbdetails_in_new> <cname_in_new>firstname lastname</cname_in_new> <street1>conbdetails addressline1 </street1> <street2>conbdetails addressline2</street2> <street3>conbdetails addressline3</street3> <city>conbdetails_city</city> <postalcode>conbdetails_pin</postalcode> <country>conbdetails us</country> <ipaddress>sip_in_10.20.30.40</ipaddress> </conbdetails_in_new> <mpass_in_new>full details of buyer</mpass_in_new> <mshipdets_in_new> <fullname_new>fname lname</fullname_new> <meraddress1_new>mship addressline1</meraddress1_new> <meraddress2_new>mship addressline2</meraddress2_new> <meraddress3_new>mship addressline3</meraddress3_new> <city_in_new>mship city</city_in_new> <pcode_in_new>mship pin</pcode_in_new> <c_in_new>mship us</c_in_new> </mshipdets_in_new> <midets_in_new id="0"> <itcode_in_new>a</itcode_in_new> <itdesc_in_new>aadesc</itdesc_in_new> <ituntprice_in_new>555</ituntprice_in_new> <itquan_in_new>41</itquan_in_new> </midets_in_new> <midets_in_new id="1"> <itcode_in_new>b</itcode_in_new> <itdesc_in_new>bbbdesc</itdesc_in_new> <ituntprice_in_new>44343</ituntprice_in_new> <itquan_in_new>32</itquan_in_new> </midets_in_new> <midets_in_new id="2"> <itcode_in_new>c</itcode_in_new> <itdesc_in_new>abcdesc</itdesc_in_new> <ituntprice_in_new>8979</ituntprice_in_new> <itquan_in_new>32</itquan_in_new> </midets_in_new> <totalamt> <amt_in_new>167.78</amt_in_new> <currency_in_new>usd</currency_in_new> </totalamt> </req_in_new>
Comments
Post a Comment