469,343 Members | 5,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

Converting XML in this format

I have an XML like this.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <Criteria>
  3. <Messages Code="SSS" Message_Text="Congrats!" /> 
  4. <Messages Code="JJJ" Message_Text="Bad Luck"/> 
  5. </Criteria>
  6.  
Can someone help me in transforming into the following
using XSL. I want to use XSL and transform this into
XML like below.

Expand|Select|Wrap|Line Numbers
  1. <Criteria>
  2. <Messages>
  3. <Message code="APF">Congrats!</Message> 
  4. <Message code="JJJ">Bad Luck!</Message>
  5. </Messages>
  6. </Criteria>
  7.  
I used the below,but am the output in in text when i run this using the browser.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!-- Edited with XML Spy v2007 (http://www.altova.com) -->
  3. <xsl:stylesheet version="1.0"
  4. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5. <xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes'/>
  6.  
  7. <xsl:template match="/">
  8.     <Criteria>
  9.            <Messages>
  10.             <xsl:apply-templates/>
  11.         </Messages>
  12.         </Criteria>
  13. </xsl:template>
  14.  
  15. <xsl:template match="Messages">
  16.               <Message code="{@Code}"><xsl:value-of select="@Message_Text"/></Message>
  17. </xsl:template>
  18.  
  19. </xsl:stylesheet>
  20.  
  21.  
Apr 13 '07 #1
3 1227
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN....

Try to update XSL this way:
[html]<xsl:template match="Messages">
&lt;Message code=&quot;<xsl:value-of select="@Code"/>&quot;&gt;<xsl:value-of select="@Message_Text"/>&lt;/Message&gt;
</xsl:template>
[/html]
Apr 13 '07 #2
Hi,
Thanks a lot for your reply.
I am using the below as mentioned :

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?xml version="1.0" encoding="ISO-8859-1"?>
  3. <!-- Edited with XML Spy v2007 (http://www.altova.com) -->
  4. <xsl:stylesheet version="1.0"
  5. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  6. <xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes'/>
  7.  
  8. <xsl:template match="/">
  9.     <Criteria>
  10.         <Messages>
  11.             <xsl:apply-templates/>
  12.         </Messages>
  13.      </Criteria>
  14. </xsl:template>
  15.  
  16. <xsl:template match="Messages">
  17.               &lt;Message code=&quot;<xsl:value-of select="@Code"/>&quot;&gt;<xsl:value-of select="@Message_Text"/>&lt;/Message&gt;
  18. </xsl:template>
  19.  
  20. </xsl:stylesheet>
  21.  
  22.  
  23.  
And the Output now shows (in IE) as

Expand|Select|Wrap|Line Numbers
  1. <Message code="SSS">Congrats!</Message> <Message code="JJJ">Bad Luck</Message> 
  2.  
I would also like to have Criteria as the Root Element.

Help really appreciated.I am quite new to XSLT.
Apr 13 '07 #3
dorinbogdan
839 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. ....
  2. <xsl:template match="/">
  3.     &lt;Criteria&gt;
  4.         &lt;Messages&gt;
  5.             <xsl:apply-templates/>
  6.         &lt;/Messages&gt;
  7.     &lt;/Criteria&gt;
  8. </xsl:template>
  9. .......
  10.  
Apr 13 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Jonny Au | last post: by
5 posts views Thread by c_shah | last post: by
3 posts views Thread by Jef Driesen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.