Need help with XML structure change

Questions about XML that are not covered by the other forums should go here.
trojanloy
Posts: 1
Joined: Sat Aug 13, 2005 10:18 am
Location: South Bend, IN
Contact:

Need help with XML structure change

Post by trojanloy »

Hi,

I am a self-confessed XML newbie, and I suspect my question is probably very simple, but I don't even know what to search for, as I don't know the right terms and such. So I'm hoping someone here can help me.

I recently downloaded oXygen and am trying to figure out how to perform a specific function with it. What I need to do is quite simple. This is the current structure of my XML document:

Code: Select all


    <thread id="90815878">
<comment>
<datetime>2003-03-17T01:48:14-05:00</datetime>
<name>Andrew</name>
<email>uscrepublican@hotmail.com</email>
<uri></uri>
<ip>24.24.241.28</ip>
<text><![CDATA[Man, the West is tough. What were they thinking?!? Arizona, Kansas, Duke, and Illinois--any other year and that could be your Final Four. Illinois and Duke both won their conference tourneys, and Arizona is ranked #1. Duke is always tough. Sheesh.]]></text>
</comment>
<comment>
<datetime>2003-03-17T01:54:36-05:00</datetime>
<name>Andrew</name>
<email>uscrepublican@hotmail.com</email>
<uri></uri>
<ip>24.24.241.28</ip>
<text><![CDATA[Wow, picking the semifinal matchups in the East is turning out to be quite difficult. Each matchup I show could go either way.]]></text>
</comment>
</thread>
I need to make it so that the "thread ID" number appears on EVERY comment, not just at the beginning of each comment thread. So for example, it could look like this:

Code: Select all


    <thread id="90815878">
<comment>
<threadid>90815878</threadid>
<datetime>2003-03-17T01:48:14-05:00</datetime>
<name>Andrew</name>
<email>uscrepublican@hotmail.com</email>
<uri></uri>
<ip>24.24.241.28</ip>
<text><![CDATA[Man, the West is tough. What were they thinking?!? Arizona, Kansas, Duke, and Illinois--any other year and that could be your Final Four. Illinois and Duke both won their conference tourneys, and Arizona is ranked #1. Duke is always tough. Sheesh.]]></text>
</comment>
<comment>
<threadid>90815878</threadid>
<datetime>2003-03-17T01:54:36-05:00</datetime>
<name>Andrew</name>
<email>uscrepublican@hotmail.com</email>
<uri></uri>
<ip>24.24.241.28</ip>
<text><![CDATA[Wow, picking the semifinal matchups in the East is turning out to be quite difficult. Each matchup I show could go either way.]]></text>
</comment>
</thread>
Or something along those lines. Bottom line, I just need to find an automated way of sticking that thread id number inside the "comment" attribute. If someone can show me the basics, I can probably figure out the specifics... I just need a starting point.

In case you're wondering, this conversion is necessary because I am exporting to a MySQL database and I need the ID number in every comment in order to sychronise with the existing entries in the database.

Any help would be much appreciated! Thanks!

--Brendan Loy
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Brendan,

For this kind of problems the general solution is to start with a copy template and then add specific matches on the nodes that you want to change. A possible stylesheet that does the change you requested is below:

Code: Select all


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="comment">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<threadid><xsl:value-of select="../@id"/></threadid>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George
Post Reply