many-to-many XML structure

Discussion in 'XML' started by oNLINE bUDDY, Aug 7, 2004.

  1. oNLINE bUDDY

    oNLINE bUDDY Guest

    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!
    oNLINE bUDDY, Aug 7, 2004
    #1
    1. Advertising

  2. oNLINE bUDDY wrote:

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


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 8, 2004
    #2
    1. Advertising

  3. oNLINE bUDDY

    oNLINE bUDDY Guest

    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" <> wrote in message
    news:41162482$...
    >
    >
    > oNLINE bUDDY wrote:
    >
    > > 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>
    >
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
    >
    oNLINE bUDDY, Aug 9, 2004
    #3
  4. oNLINE bUDDY

    oNLINE bUDDY Guest

    <xsl:copy-of select="@*" /> will do some!


    "oNLINE bUDDY" <> wrote in message
    news:2oARc.15536$...
    >
    > 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" <> wrote in message
    > news:41162482$...
    > >
    > >
    > > oNLINE bUDDY wrote:
    > >
    > > > 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>
    > >
    > >
    > > --
    > >
    > > Martin Honnen
    > > http://JavaScript.FAQTs.com/
    > >

    >
    >
    oNLINE bUDDY, Aug 9, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    721
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,176
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    579
  4. Replies:
    9
    Views:
    25,204
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    749
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page