![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Transform xml to xml. >Thread Next - Re: Transform xml to xml. Re: Transform xml to xml.To: NULL Date: 5/1/2004 1:23:00 PM
Tracy wrote:
> I need to transform one xml to another xml. I found out that I only transformed the first level nodes out. The second, third etc node were not selected out. Can you help figure it out ? Thanks in advance!!
>
> The start xml file is :
> <?xml version="1.0" encoding="UTF-8" ?><books><book fullname="ItalyBook" country ="Italy" author = "Author Italy"> ItalyBook
> <book fullname = "USABook" country ="USA" author = "Author USA"> USABook </book></book><book fullname="JapanBook" country ="China" author = "Author Japan">JapanBook</book><book fullname="FranceBook" country ="China" author = "Author France">FranceBook</book></books>
Trying to understand what the XML looks like I have added some
formatting, I have now got
<?xml version="1.0" encoding="UTF-8" ?>
<books>
<book fullname="ItalyBook" country ="Italy" author = "Author Italy">
ItalyBook
<book fullname = "USABook" country ="USA" author = "Author USA">
USABook </book>
</book>
<book fullname="JapanBook" country ="China" author = "Author
Japan">JapanBook</book>
<book fullname="FranceBook" country ="China" author = "Author
France">FranceBook</book>
</books>
I am not sure I understand why the <book> elements can be nested but
maybe the result or the stylesheet let me understand that.
> The expected output xml:
> <?xml version="1.0" encoding="UTF-8" ?><books><book fullname="ItalyBook" country="Italy" author="Author Italy"> ItalyBook
> <book fullname="USABook" country="USA" author="Author USA">USABook
> </book></book><book fullname="JapanBook" country="China" author="Author Japan">JapanBook</book><book fullname="FranceBook" country="China" author="Author France">FranceBook</book></books>
Again that needs formatting to make it readable:
<?xml version="1.0" encoding="UTF-8" ?>
<books>
<book fullname="ItalyBook" country="Italy" author="Author Italy">
ItalyBook
<book fullname="USABook" country="USA" author="Author
USA">USABook</book>
</book>
<book fullname="JapanBook" country="China" author="Author
Japan">JapanBook</book>
<book fullname="FranceBook" country="China" author="Author
France">FranceBook</book>
</books>
but now I wonder what the difference between the source and the intended
result is. Is there one? I don't see it.
> My xslt file:
> xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'><xsl:template match = "/books"><TREENODES><xsl:for-each select = "book"><treenode><xsl:attribute name="Text"><xsl:value-of select="@fullname" /></xsl:attribute></treenode><xsl:for-each select ="book/book"><treenode><xsl:attribute name = "Text"><xsl:value-of select = "@fullname"/></xsl:attribute></treenode></xsl:for-each></xsl:for-each></TREENODES></xsl:template></xsl:stylesheet>
Again applying some formatting I get
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version='1.0'>
<xsl:template match = "/books">
<TREENODES>
<xsl:for-each select = "book">
<treenode>
<xsl:attribute name="Text"><xsl:value-of select="@fullname"
/></xsl:attribute>
</treenode>
<xsl:for-each select ="book/book">
<treenode><xsl:attribute name = "Text"><xsl:value-of select =
"@fullname"/></xsl:attribute>
</treenode>
</xsl:for-each>
</xsl:for-each>
</TREENODES>
</xsl:template>
</xsl:stylesheet>
and it is clear that you haven't provided the desired output as there
are literal result elements <treenode> in the stylesheet but in the
desired output.
I guess form the stylesheet that you want to transform the <books> list
into a <TREENODES> list and each <book> element into a <treenode>
element. That is done with the following stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version='1.0'>
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="books">
<TREENODES>
<xsl:apply-templates />
</TREENODES>
</xsl:template>
<xsl:template match="book">
<treenode text="{@fullname}">
<xsl:apply-templates select="*" />
</treenode>
</xsl:template>
</xsl:stylesheet>
which yields the result
<?xml version="1.0" encoding="UTF-8"?>
<TREENODES>
<treenode text="ItalyBook">
<treenode text="USABook"/>
</treenode>
<treenode text="JapanBook"/>
<treenode text="FranceBook"/>
</TREENODES>
I hope that is what you want, if not post back and tell us what the
result document is supposed to look like.
--
Martin Honnen
http://JavaScript.FAQTs.com/
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
