RSS Forums RSS
Please support our XML, XSLT and XPATH advertiser: Programming Forums
Views: 2131 | Replies: 5 | Solved
Reply
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

XSLT - inserting new elements

  #1  
May 3rd, 2007
I'm struggling with XSLT. I need to copy an entire xml file to a new xml file. The input and output should be identical except for the addition of some extra tags around certain elements.

Consider the two versions:

  1. <?xml version="1.0"?>
  2. <document>
  3. <header>
  4. <general>
  5. <version>1.14.2</version>
  6. <form>/XX/ATL_ZI_D007_PURCHASE</form>
  7. <language>EN</language>
  8. <device>PRINTER</device>
  9. </general>
  10. <archive mode="1" mode-modify-enabled="yes"/>
  11. </header>
  12. <data xml:space="preserve">
  13. <window name="MAIN_WINDOW" type="main" page="FIRST" page-id="001">
  14. <table name="DATA" pattern="0001">
  15. <thead/>
  16. <tbody>
  17. <tr ltype="TABLE_POS">
  18. <tc cell="1" />
  19. </tr>
  20. </tbody>
  21. </table>
  22. </window>
  23. </data>
  24. </document>

  1. <?xml version="1.0"?>
  2. <document>
  3. <header>
  4. <general>
  5. <version>1.14.2</version>
  6. <form>/XX/ATL_ZI_D007_PURCHASE</form>
  7. <language>EN</language>
  8. <device>PRINTER</device>
  9. </general>
  10. <archive mode="1" mode-modify-enabled="yes"/>
  11. </header>
  12. <data xml:space="preserve">
  13. <window name="MAIN_WINDOW" type="main" page="FIRST" page-id="001">
  14. <table name="DATA" pattern="0001">
  15. <thead/>
  16. <tbody>
  17. <extra>
  18. <tr ltype="TABLE_POS">
  19. <tc cell="1" />
  20. </tr>
  21. </extra>
  22. </tbody>
  23. </table>
  24. </window>
  25. </data>
  26. </document>

Notice the "extra" tags around the "tr" element. Essentially I want to copy the file, but re-write the "tr" element by surrounding it with additional tags. That's the simplified version.

In actuality, imagine there are multiple "tr" elements, and I wanted the "extra" tags around every group of three:

  1. <table name="DATA" pattern="0001">
  2. <thead/>
  3. <tbody>
  4. <extra>
  5. <tr ltype="TABLE_POS">
  6. <tc cell="1" />
  7. </tr>
  8. <tr ltype="TABLE_POS">
  9. <tc cell="1" />
  10. </tr>
  11. <tr ltype="TABLE_POS">
  12. <tc cell="1" />
  13. </tr>
  14. </extra>
  15. <extra>
  16. <tr ltype="TABLE_POS">
  17. <tc cell="1" />
  18. </tr>
  19. <tr ltype="TABLE_POS">
  20. <tc cell="1" />
  21. </tr>
  22. <tr ltype="TABLE_POS">
  23. <tc cell="1" />
  24. </tr>
  25. </extra>
  26. <extra>
  27. <tr ltype="TABLE_POS">
  28. <tc cell="1" />
  29. </tr>
  30. <tr ltype="TABLE_POS">
  31. <tc cell="1" />
  32. </tr>
  33. <tr ltype="TABLE_POS">
  34. <tc cell="1" />
  35. </tr>
  36. </extra>
  37. </tbody>
  38. </table>
  39.  

I appreciate any assistance or references.
Last edited by tgreer : May 3rd, 2007 at 7:48 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: XSLT - inserting new elements

  #2  
May 3rd, 2007
If that's a bit too much to tackle, then an explanation of the basic technique to modify a single child element would at least get me started. If I had this for example:

  1. <?xml version="1.0"?>
  2. <sandwich>
  3. <bread type="rye" />
  4. <ingredient>Peanut Butter</ingredient>
  5. <ingredient>Jelly</ingredient>
  6. </sandwich>

And I want to copy it, but wanted to transform the bread element to: <bread>Wheat</bread>, how would I do that?
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: XSLT - inserting new elements

  #3  
May 4th, 2007
Ok, figured out this much:

  1. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="xml" indent="yes" />
  3.  
  4. <xsl:template match="node()|@*">
  5. <xsl:copy>
  6. <xsl:apply-templates select="@*|node()"/>
  7. </xsl:copy>
  8. </xsl:template>
  9.  
  10. <xsl:template match="tr">
  11. <extra>
  12. <xsl:copy>
  13. <xsl:apply-templates select="@*" />
  14. <xsl:apply-templates select="node()"/>
  15. </xsl:copy>
  16. </extra>
  17. </xsl:template>
  18.  
  19. </xsl:stylesheet>

That's a huge step in the right direction, but of course it puts the "extra" tags around every "tr". I want to group every three "tr" elements. Anyone?
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: XSLT - inserting new elements

  #4  
May 4th, 2007
Am I confusing everyone? Or do we not have any XML pros as members? Let me restate as simply but as completely as possible.

Given the following XML input file:

  1. <?xml version="1.0"?>
  2. <document>
  3. <table>
  4. <tbody>
  5. <tr>1</tr>
  6. <tr>2</tr>
  7. <tr>3</tr>
  8. <tr>4</tr>
  9. <tr>5</tr>
  10. <tr>6</tr>
  11. <tr>7</tr>
  12. <tr>8</tr>
  13. <tr>9</tr>
  14. <tr>10</tr>
  15. <tr>11</tr>
  16. </tbody>
  17. </table>
  18. </document>

I want to produce the following result file:

  1. <?xml version="1.0"?>
  2. <document>
  3. <table>
  4. <tbody>
  5. <extra>
  6. <tr>1</tr>
  7. <tr>2</tr>
  8. <tr>3</tr>
  9. </extra>
  10. <extra>
  11. <tr>4</tr>
  12. <tr>5</tr>
  13. <tr>6</tr>
  14. </extra>
  15. <extra>
  16. <tr>7</tr>
  17. <tr>8</tr>
  18. <tr>9</tr>
  19. </extra>
  20. <extra>
  21. <tr>10</tr>
  22. <tr>11</tr>
  23. </extra>
  24. </tbody>
  25. </table>
  26. </document>

So far I have the following XSLT. It's overly complex, it's meant to provide multiple points to insert the necessary logic. It produces:

  1. <?xml version="1.0"?>
  2. <document>
  3. <table>
  4. <tbody>
  5. <extra>
  6. <tr>1</tr>
  7. <tr>2</tr>
  8. <tr>3</tr>
  9. <tr>4</tr>
  10. <tr>5</tr>
  11. <tr>6</tr>
  12. <tr>7</tr>
  13. <tr>8</tr>
  14. <tr>9</tr>
  15. <tr>10</tr>
  16. <tr>11</tr>
  17. </extra>
  18. </tbody>
  19. </table>
  20. </document>

XSLT Program:
  1. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2.  
  3. <xsl:output method="xml" indent="yes" />
  4.  
  5. <xsl:template match="node()|@*">
  6. <xsl:copy>
  7. <xsl:apply-templates select="@*|node()"/>
  8. </xsl:copy>
  9. </xsl:template>
  10.  
  11. <xsl:template match="tbody">
  12. <xsl:copy>
  13. <xsl:apply-templates select="@*" />
  14. <!-- somehow I need to do this for every THREE "tr" elements -->
  15. <extra>
  16. <xsl:call-template name="trGroup" />
  17. </extra>
  18. <!-- -->
  19. </xsl:copy>
  20. </xsl:template>
  21.  
  22.  
  23. <xsl:template name="trGroup">
  24.  
  25. <xsl:copy>
  26. <xsl:apply-templates select="@*" />
  27. <xsl:apply-templates select="node()"/>
  28. </xsl:copy>
  29.  
  30. </xsl:template>
  31.  
  32.  
  33. </xsl:stylesheet>
Reply With Quote  
Join Date: Dec 2004
Posts: 1,592
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 35
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: XSLT - inserting new elements

  #5  
May 4th, 2007
I solved this with a combination of two recursive templates and creative use of the "copy-of" function.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: XSLT - inserting new elements

  #6  
May 12th, 2007
sorry I didn't spot this in time, been rather busy with other things.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
All times are GMT -4. The time now is 12:15 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC