943,884 Members | Top Members by Rank

Ad:
May 3rd, 2007
0

XSLT - inserting new elements

Expand Post »
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:

xml Syntax (Toggle Plain Text)
  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>

xml Syntax (Toggle Plain Text)
  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:

xml Syntax (Toggle Plain Text)
  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>

I appreciate any assistance or references.
Last edited by tgreer; May 3rd, 2007 at 7:48 pm.
Similar Threads
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 3rd, 2007
0

Re: XSLT - inserting new elements

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:

xml Syntax (Toggle Plain Text)
  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?
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 4th, 2007
0

Re: XSLT - inserting new elements

Ok, figured out this much:

xml Syntax (Toggle Plain Text)
  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?
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 4th, 2007
0

Re: XSLT - inserting new elements

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:

xml Syntax (Toggle Plain Text)
  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:

xml Syntax (Toggle Plain Text)
  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:

xml Syntax (Toggle Plain Text)
  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:
xml Syntax (Toggle Plain Text)
  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>
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 4th, 2007
1

Re: XSLT - inserting new elements

I solved this with a combination of two recursive templates and creative use of the "copy-of" function.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
May 12th, 2007
0

Re: XSLT - inserting new elements

sorry I didn't spot this in time, been rather busy with other things.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: XForms Select1 itemset model instance
Next Thread in XML, XSLT and XPATH Forum Timeline: starting XMl





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC