473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

many-to-many XML structure

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!


Jul 20 '05 #1
3 1930


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

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

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="ka*@hotmail.com"/> <-New
</book>
<book id="2">
<Title="Anaconda"/> <-New
<Author>Kay</Author>
<email="ka*@hotmail.com"/> <-New
<Author>Gosling</Author>
<email="go*****@hotmail.com"/> <-New
</book>
</booklist>

<?xml version="1.0" encoding="UTF-8"?>
<authorlist>
<Author>Kay<booklist>
<email="ka*@hotmail.com"/> <-New
<book id="1"/>
<Title="Star Wars"/> <-New
<book id="2"/>
<Title="Anaconda"/> <-New
</booklist>
</Author>
<Author>Gosling<booklist>
<email="go*****@hotmail.com"/> <-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" <ma*******@yahoo.de> wrote in message
news:41********@olaf.komtel.net...


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

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #3
<xsl:copy-of select="@*" /> will do some!
"oNLINE bUDDY" <sd*****@hotmail.com> wrote in message
news:2o********************@weber.videotron.net...

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="ka*@hotmail.com"/> <-New
</book>
<book id="2">
<Title="Anaconda"/> <-New
<Author>Kay</Author>
<email="ka*@hotmail.com"/> <-New
<Author>Gosling</Author>
<email="go*****@hotmail.com"/> <-New
</book>
</booklist>

<?xml version="1.0" encoding="UTF-8"?>
<authorlist>
<Author>Kay<booklist>
<email="ka*@hotmail.com"/> <-New
<book id="1"/>
<Title="Star Wars"/> <-New
<book id="2"/>
<Title="Anaconda"/> <-New
</booklist>
</Author>
<Author>Gosling<booklist>
<email="go*****@hotmail.com"/> <-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" <ma*******@yahoo.de> wrote in message
news:41********@olaf.komtel.net...


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

Martin Honnen
http://JavaScript.FAQTs.com/


Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
4
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.