Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I'm new to XML/ XSLT. My task is to generate xml in the below structure and send it restful server through ASP.NET application. Could you help me to produce the below structure using XSLT in VS2010 please? Thanks in advance.
Required output XML:
XML
<?xml version="1.0" encoding="UTF8"?>
<MyTalkTalkMessage xmlns="http://www.Testwebsite.com/xx/dunlop" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" 
               xmlns:gt="http://www.Testwebsite.com/schemas/freetalk/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               
               xsi:schemaLocation="http://www.Testwebsite.com/xx/dunlop http://xmlws.Testwebsite.com/v10/schema/Etalk_mychapter.xsd">
  <dunlopVersion>1.0</dunlopVersion>
  <dunHeader>
    <dunMessageDetails>
      <dunClass>ClassName</dunClass>     
    </dunMessageDetails>    
  </dunHeader>  
  <Body>
    <MyTestSubmit xmlns="http://xmlws.Testwebsite.com/MyHeader" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                    xsi:schemaLocation="http://xmlws.Testwebsite.com/Header http://xmlws.Testwebsite.com/v123/schema/wsforms/MyTestFormSubmit.xsd"
                    xmlns:bs="http://xmlws.Testwebsite.com">      
      <myDateSig>19990102</myDateSig>
      <Form>
        <myPets xmlns="http://xmlws.Testwebsite.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                      xsi:schemaLocation="http://xmlws.Testwebsite.com http://xmlws.Testwebsite.com/v123/schema/wsforms/myPetreturn342.xsd">                  
          <Pets>
            <petCompCode>1234</petCompCode>
          </Pets>
          <PetVETAddress>
            <VetPetAddress>Address</VetPetAddress>                      
          </PetVETAddress>          
        </myPets>
      </Form>
    </MyTestSubmit>
  </Body>
</MyTalkTalkMessage>

In my xslt, I have included all the namespaces but when transformed the xslt, the xml has been generated without
HTML
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance 
in
HTML
<mytestsubmit>  & <mypets>
sections of my output xml
My xslt:
XML
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="no"/>  
  <xsl:template match="/">
    <xsl:apply-templates select="/Test" />
  </xsl:template>
  <xsl:template match="Test">
	<MyTalkTalkMessage xmlns="http://www.Testwebsite.com/xx/dunlop" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" 
               xmlns:gt="http://www.Testwebsite.com/schemas/freetalk/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               
               xsi:schemaLocation="http://www.Testwebsite.com/xx/dunlop http://xmlws.Testwebsite.com/v10/schema/Etalk_mychapter.xsd">
	
	<dunlopVersion>1.0</dunlopVersion>
  	<dunHeader>
    		<dunMessageDetails>
      			<dunClass><xsl:value-of select="Test/dunClass"/></dunClass>      			
    		</dunMessageDetails>    		
  	</dunHeader>  	    
      <Body>
        <MyTestSubmit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
			xmlns:bs="http://xmlws.Testwebsite.com"
			xsi:schemaLocation="http://xmlws.Testwebsite.com/Header http://xmlws.Testwebsite.com/v123/schema/wsforms/MyTestFormSubmit.xsd"			xmlns="http://xmlws.Testwebsite.com/MyHeader">		
      		<myDateSig>
			<xsl:variable name="dts" select="Test/SigDate"/>
            		<xsl:value-of select="concat(substring($dts, 1, 4), '-',
                      		substring($dts, 6, 2), '-', substring($dts, 9, 2))"/>
		</myDateSig>          
          <Form>
            <myPets xmlns="http://xmlws.Testwebsite.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                      xsi:schemaLocation="http://xmlws.Testwebsite.com http://xmlws.Testwebsite.com/v123/schema/wsforms/myPetreturn342.xsd">
          <Pets>
            <petCompCode><xsl:value-of select="Test/CompCode"/></petCompCode>
          </Pets>
          <PetVETAddress>
            <VetPetAddress><xsl:value-of select="Test/Add1"/></VetPetAddress>                   
          </PetVETAddress>
        </myPets>
      </Form>
    </MyTestSubmit>
  </Body>
</MyTalkTalkMessage>
</xsl:template>
</xsl:stylesheet>


C# code:
C#
{
            XslCompiledTransform myXslTrans = new XslCompiledTransform();            myXslTrans.Load(HttpContext.Current.Server.MapPath("~/Test/InputXSLT.xslt"));            myXslTrans.Transform(HttpContext.Current.Server.MapPath("~/Test/OutputXML.xml"), myXMLPath + "\\" + xmlFileName);
        }

My output XML
XML
<?xml version="1.0" encoding="UTF-8"?>
<MyTalkTalkMessage xmlns="http://www.Testwebsite.com/xx/dunlop" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" 
               xmlns:gt="http://www.Testwebsite.com/schemas/freetalk/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               
               xsi:schemaLocation="http://www.Testwebsite.com/xx/dunlop http://xmlws.Testwebsite.com/v10/schema/Etalk_mychapter.xsd">
  <dunlopVersion>1.0</dunlopVersion>
  <dunHeader>
    <dunMessageDetails>
      <dunClass>ClassName</dunClass>     
    </dunMessageDetails>    
  </dunHeader>  
  <Body>
    <MyTestSubmit xmlns="http://xmlws.Testwebsite.com/MyHeader" 
                  xsi:schemaLocation="http://xmlws.Testwebsite.com/Header http://xmlws.Testwebsite.com/v123/schema/wsforms/MyTestFormSubmit.xsd"
                  xmlns:bs="http://xmlws.Testwebsite.com">
      <myDateSig>19990102</myDateSig>     
      <Form>
        <myPets xmlns="http://xmlws.Testwebsite.com"  
                xsi:schemaLocation="http://xmlws.Testwebsite.com http://xmlws.Testwebsite.com/v123/schema/wsforms/myPetreturn342.xsd">
          <Pets>
            <petCompCode>1234</petCompCode>
          </Pets>
          <PetVETAddress>
            <VetPetAddress>Address</VetPetAddress>                      
          </PetVETAddress>          
        </myPets>
      </Form>
    </MyTestSubmit>
  </Body>
</MyTalkTalkMessage>


Could you advise me how to generate a required output xml please? TIA.
Posted
Updated 3-May-14 3:49am
v5
Comments
_Asif_ 2-May-14 3:54am    
What have you tried so far?
MaryJames 3-May-14 9:51am    
Thank you for your swift reply. Updated my question. Please advise me. TIA
karthik Udhayakumar 3-May-14 15:21pm    
Required output xml? or output data?not clear bro..pls use widget:)
MaryJames 5-May-14 11:46am    
I'm experencing problem generating XML structure with multiple namespaces. I'm fine with data. After transform my XSLT, the xml generated without: xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance.
MaryJames 6-May-14 3:08am    
Please help me to resolve the problem. TIA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900