Groups | Blog | Home
all groups > dotnet xml > august 2004 >

dotnet xml : many-to-many XML structure


oNLINE bUDDY
8/7/2004 6:10:23 PM
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!



Dieu tout puissant
8/7/2004 10:44:56 PM
nope that wont work for what i want to do

[quoted text, click to view]

oNline bUDDY
8/7/2004 10:47:32 PM
nope that wont work for what i want to do

[quoted text, click to view]

Dimitre Novatchev
8/8/2004 10:48:57 AM
The answer is to use a grouping method -- e.g. the Muenchian method.

For a good description see:

http://www.jenitennison.com/xslt/grouping/

or

http://www.topxml.com/code/default.asp?p=3&id=v20010129150851


Cheers,


Dimitre Novatchev.


[quoted text, click to view]

Dimitre Novatchev
8/8/2004 6:07:17 PM
That solves exactly the problem you described -- in case you had something
else in mind you have yet to tell us about it.

Cheers,

Dimitre Novatchev.

[quoted text, click to view]

oNLINE bUDDY
8/8/2004 8:55:21 PM
ok got a (what looks like) good answer from someone


[quoted text, click to view]

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:output 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>


[quoted text, click to view]

AddThis Social Bookmark Button