oh txs very much! I can understand more the XSLT how it works. but lets say you have more attributes to transfert. how do you also transfert those?
Here I added titles for books and email adresses for the authors:
<?xml version="1.0" encoding="UTF-8"?>
<booklist>
<book id="1">
<Title="Star Wars"/> <-New
<Author>Kay</Author>
<email=""/> <-New
</book>
<book id="2">
<Title="Anaconda"/> <-New
<Author>Kay</Author>
<email=""/> <-New
<Author>Gosling</Author>
<email=""/> <-New
</book>
</booklist>
<?xml version="1.0" encoding="UTF-8"?>
<authorlist>
<Author>Kay<booklist>
<email=""/> <-New
<book id="1"/>
<Title="Star Wars"/> <-New
<book id="2"/>
<Title="Anaconda"/> <-New
</booklist>
</Author>
<Author>Gosling<booklist>
<email=""/> <-New
<book id="2"/>
<Title="Anaconda"/> <-New
</booklist>
</Author>
</authorlist>
<xsl:template match="/">
<authorlist>
<xsl:for-each select="//Author[not(. = following::Author)]"> <xsl:copy> <xsl:value-of select="." /> <booklist> <xsl:apply-templates select="//book[Author = current]" /> <xsl:apply-templates select="//email[Author = current]" /> <------- ???? </booklist> </xsl:copy> </xsl:for-each> </authorlist> </xsl:template>
Martin Honnen said:
oNLINE said:
How can you reverse a many-to-many XML structure between 2 tags?
Lets say we have a books/author XML file.
A book can have many authors.
<book1>
<Author1>
</Author1>
</book1>
<book2>
<Author1>
</Author1>
<Author2>
</Author2>
</book1>
Are you really using a unique tag name for every book and every author? That is very bad XML design in my view, you should have one <book> and <Author> elements and then use attributes and/or child elements to indentify the books or the authors. Thus an example could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<booklist>
<book id="1">
<Author>Kay</Author>
</book>
<book id="2">
<Author>Kay</Author>
<Author>Gosling</Author>
</book>
</booklist>
Then you can transform that to
<?xml version="1.0" encoding="UTF-8"?>
<authorlist>
<Author>Kay<booklist>
<book id="1"/>
<book id="2"/>
</booklist>
</Author>
<Author>Gosling<booklist>
<book id="2"/>
</booklist>
</Author>
</authorlist>
with a stylesheet alike
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl

utput method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<authorlist>
<xsl:for-each select="//Author[not(. = following::Author)]"> <xsl:copy> <xsl:value-of select="." /> <booklist> <xsl:apply-templates select="//book[Author = current]" /> </booklist> </xsl:copy> </xsl:for-each> </authorlist> </xsl:template>
<xsl:template match="book">
<xsl:copy>
<xsl:copy-of select="@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>