Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 14th, 2010, 09:42 AM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using XSLT to transform one XML format to other

Hi,

I am using XSLT for Xforming one XML format to other. Both the formats are given below:

Source Format:
<ns0:UserInput>
<ns0:attr name="UserCode"> <ns0:value>user</ns0:value> </ns0:attr>
<ns0:attr name="Pasword"> <ns0:value>user</ns0:value> </ns0:attr>
</ns0:UserInput>


Target Format:
<ns1:CreateUser>
<ns1:UserCode> user </ns1:UserCode>
<ns1:Pasword> user </ns1:Pasword>
</ns1:CreateUser>

basically if we see the element name in target format(UserCode) is nothing but the attr name in source format(UserCode). I don't want to hardcode and apply this logic in XSLT. how can this be done using xslt?

Thanks
Siva
Reply With Quote
  #2 (permalink)  
Old June 14th, 2010, 09:55 AM
Friend of Wrox
Points: 6,438, Level: 34
Points: 6,438, Level: 34 Points: 6,438, Level: 34 Points: 6,438, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,196
Thanks: 0
Thanked 237 Times in 236 Posts
Default

You could do it along these lines:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ns0="http://example.com/ns0"
  xmlns:ns1="http://example.com/ns1"
  exclude-result-prefixes="ns0"
  version="1.0">
 
  <xsl:template match="ns0:UserInput">
     <ns1:CreateUser>
       <xsl:apply-templates/>
     </ns1:CreateUser>
  </xsl:template>

  <xsl:template match="ns0:UserInput/ns0:attr">
     <xsl:element name="ns1:{@name}">
        <xsl:apply-templates/>
     </xsl:element>
   </xsl:template>

</xsl:stylesheet>
You will have to adjust the namespaces to ones actually used in your input and output formats.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
tsmets (July 12th, 2010)
  #3 (permalink)  
Old June 14th, 2010, 10:35 AM
Registered User
 
Join Date: Jun 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using XSLT to transform one XML format to other

Hi Martin,

I think i have misled your answer because of my little experience with XSLT,not sure whether i can make use of this in my scenario.. I am trying to put my actual scenario. Actually i have source and target xsds and have to use xslt to convert from one xsd formatted XML to another xsd formatted XML.

Source XSD:
<xsd:complexType name="CreateUserAttr">
<xsd:sequence>
<xsd:element name="value" type="DsmlValue"/>
</xsd:sequence>
<xsd:attribute name="name" type="CreateUserDescValue" use="required"/>
</xsd:complexType>

<xsd:complexType name="UserInput">
<xsd:sequence>
<xsd:element name="attr" type="CreateUserAttr"/>
</xsd:sequence>
</xsd:complexType>

<xsd:element name="UserInput" type="UserInput"/>


Target XSD:
<xsd:complexType name="Input">
<xsd:sequence>
<xsd:element name="UserCode" type="xsd:string"/>
<xsd:element name="Password" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="CreateUser" type="Input"/>

So to do this i am creating the xstl like below. And in if scenario i dont want to hard code it to 'UserCode' or 'Password' and wants to use use any in built function, so that i will be able to extend my xsds in future without worrrying about xslt Xformation. Please let me know how can i get the UserCode element name in if condition below. I was unable to refer any target element in xslt if condition.

<xsl:template match="/">
<ns1:CreateUser>
<xsl:for-each select="/ns0:UserInput/ns0:attr">
<xsl:if test="@name = 'UserCode'">
<ns1:UserCode>
<xsl:value-of select="ns0:value"/>
</ns1:UserCode>
</xsl:if>
</xsl:for-each>
</ns1:UserCreateInput>
</xsl:template>

Thanks
Siva
Reply With Quote
  #4 (permalink)  
Old June 14th, 2010, 10:45 AM
Friend of Wrox
Points: 6,438, Level: 34
Points: 6,438, Level: 34 Points: 6,438, Level: 34 Points: 6,438, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,196
Thanks: 0
Thanked 237 Times in 236 Posts
Default

I am afraid I don't really understand what you write in your second post. You say you don't want to "hard code" UserCode or Password. The stylesheet I posted does not hard code them so why can't you use it?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
XML/XSLT Transform Error in ASP kwilliams Classic ASP XML 2 October 9th, 2007 03:26 AM
How do I transform XSLT without the <?xml?> tag? nadavvin XSLT 4 June 10th, 2007 10:25 AM
Can 1 xslt transform an xml doc into 2 text files Raju Sarode XSLT 7 November 3rd, 2006 04:10 PM
Transform XML with XSLT in JavaScript Mozilla Spankmaster79 Javascript 3 January 18th, 2006 07:31 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 09:18 PM



All times are GMT -4. The time now is 01:14 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.