Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Transform xml to xml.

From: Martin Honnen <mahotrash@-----.-->
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/



transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent