Click to See Complete Forum and Search --> : XML to XML Transformation


muj0i
12-19-2007, 10:06 PM
Hello...

I'm newbie with xml transformation and XSLT. Could there be anyone who can give me a simple example of transforming an xml to another xml.

I see samples from the internet but it transforms an xml to an html. I have read some tutorials online too but i still find it difficult to create an xml file that can do the transformation. please help :(

ex. old xml
<Item>
<Stocks>
<Apple>00</Apple>
<Banana>89</Banana>
<Bread>12</Bread>
<Juice>11</Juice>
</Stocks>
</Item>

new xml
<ItemsInStock>
<Banana>eightyNine</Banana>
<Bread>12more</Bread>
<Juice>11</Juice>
</ItemsInStock>

I know it looks weird. I just dont know how to produce an xml with new tags not existing from the previous xml file. Also, how to include exisitng xml tags as well. Sorry for my weird sample. Hope someone can help. tnx

jkmyoung
12-20-2007, 10:11 AM
<xsl:template match="Item">
<ItemsInStock>
<xsl:apply-templates select="Stocks/*[. != 0]"/>
</ItemsInStock>
</xsl:template>
<xsl:template match="Juice">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="Bread">
<Bread>
<xsl:value-of select="."/><xsl:text>more</xsl:text>
</Bread>
</xsl:template>
<xsl:template match="Apple">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="Banana">
<Banana>
???create complex template
</Banana>
</xsl:template>

For banana you'd have to create a template which converts numbers into text, and then call it. Either that or create an external function.

muj0i
12-24-2007, 12:50 AM
thanks! =p
This is a big help :p
i'll try this one