• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

xml to xml transform

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! group,
I want to transform A xml file into B xml file
The A.xml is

and i want the B.xml as

I have written a XSL

But the out put is

can anybody help me in getting the desired out put..
regds.
prateek
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the below stylesheet for getting your desired output.
However do note that the stylesheet below can be reduced to one third of it's size by putting all the repetitive code into a common template and invoking it.
<xsl:template match="/">
<xsl:apply-templates select="source"/>
</xsl:template>
<xsl:template match="source">
<xsl:element name="{name(.)}">
<xsl:for-each select="@*">
<xsl:attribute name = "{name(.)}" >
</xsl:attribute>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:apply-templates select="name"/>
</xsl:element>
</xsl:template>
<xsl:template match="name">
<xsl:element name="{name(.)}">
<xsl:for-each select="@*">
<xsl:attribute name = "{name(.)}" >
</xsl:attribute>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:apply-templates select="name1"/>
</xsl:element>
</xsl:template>
<xsl:template match="name1">
<xsl:element name="{name(.)}">
<xsl:for-each select="@*">
<xsl:attribute name = "{name(.)}" >
</xsl:attribute>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:apply-templates select="name"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this works:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">;
<xsl utput method="xml" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"/>
</xsl:for-each>
<xsl:apply-templates select="*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
 
prateek narang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Capped Rose. At first I was thinking that my question is too lont that nobody is going to answer. Anyway my problem is solved. Thank you again.
Shashank M Tanksali, though u'r solution also work but if i want to apply it in more coplex xml file, it would be tough. Any way thanx for u'r help.
regds..
--prateek
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your name capped rose does not comply with the JavaRanch naming policy. We require the displayed names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please spare a moment and modify your profile to use a publicly displayed name that meets these requirements.
Thanks!
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic