many-to-many XML structure

O

oNLINE bUDDY

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!
 
M

Martin Honnen

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>
 
O

oNLINE bUDDY

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="(e-mail address removed)"/> <-New
</book>
<book id="2">
<Title="Anaconda"/> <-New
<Author>Kay</Author>
<email="(e-mail address removed)"/> <-New
<Author>Gosling</Author>
<email="(e-mail address removed)"/> <-New
</book>
</booklist>

<?xml version="1.0" encoding="UTF-8"?>
<authorlist>
<Author>Kay<booklist>
<email="(e-mail address removed)"/> <-New
<book id="1"/>
<Title="Star Wars"/> <-New
<book id="2"/>
<Title="Anaconda"/> <-New
</booklist>
</Author>
<Author>Gosling<booklist>
<email="(e-mail address removed)"/> <-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>
 
O

oNLINE bUDDY

<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="(e-mail address removed)"/> <-New
</book>
<book id="2">
<Title="Anaconda"/> <-New
<Author>Kay</Author>
<email="(e-mail address removed)"/> <-New
<Author>Gosling</Author>
<email="(e-mail address removed)"/> <-New
</book>
</booklist>

<?xml version="1.0" encoding="UTF-8"?>
<authorlist>
<Author>Kay<booklist>
<email="(e-mail address removed)"/> <-New
<book id="1"/>
<Title="Star Wars"/> <-New
<book id="2"/>
<Title="Anaconda"/> <-New
</booklist>
</Author>
<Author>Gosling<booklist>
<email="(e-mail address removed)"/> <-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
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top