many-to-many XML structure

oNLINE bUDDY

Usenet Archive
Joined
Jun 19, 2003
Messages
4
Reaction score
0
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>

Many people are the authors of a book.
So if we reverse the first xml we get:

<Author1>
<book1>
</book1>
<book2>
</book2>
</Author1>
<Author2>
<book2>
</book2>
</Author2>

How can I do that?

txs for your answer!
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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:eek: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>
 

oNLINE bUDDY

Usenet Archive
Joined
Jun 19, 2003
Messages
4
Reaction score
0
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:eek: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>
 

oNLINE bUDDY

Usenet Archive
Joined
Jun 19, 2003
Messages
4
Reaction score
0
<xsl:copy-of select="@*" /> will do some!

oNLINE bUDDY said:
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:eek: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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top