Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Creating tabular HTML output based on comment in XML (using XSLT)

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 10/30/2009 12:11:00 PM
Ayan wrote:

> <orders>
> <OrdersTransaction>
> <Item>
> <!--  Changed Element:old --> 
> <Description>Marker</Description> 
> <!--  Changed Element:new --> 
> <Description>Mark er</Description> 
> </Item>
> </OrdersTransaction>
> <OrdersTransaction>
> <Item>
> <!--  Changed Element:old --> 
> <Description>Eraser</Description> 
> <!--  Changed Element:new --> 
> <Description>eraser</Description> 
> </Item>
> <Item>
> <!--  Added Element(s) --> 
> <comment>additional comments</comment> 
> </Item>
> <Item>
> <!--  Deleted Element(s) --> 
> <comment>Pen</comment> 
> </Item>
> <Item>
> <!--  Changed Element:old --> 
> <Description>Highlighter</Description> 
> <!--  Changed Element:new --> 
> <Description>highlighter</Description> 
> </Item>
> </OrdersTransaction>
> </orders>
> 
> 
> "Deleted Element(s)" means, it was present in First XML, but not anymore in 
> Second XML.
> "Added Element(s)" means, it was present in Second XML, but not in First XML.
> "Changed Element:old" means it was like this in First XML but "Changed 
> Element:new" shows the changed 
> 
> version of it in Second XML.
> 
> Now, these deleted, added or changed components can occur at any position in 
> the XML file. What I mean 
> 
> to say is, the tags can be deep inside. So there is no chance to know 
> beforehand. Only we can check the 
> 
> comments to know which part in the XML holds what.
> 
> My question:
> 
> I need to show HTML output in a simple table like this:
> 
> ------------------------------------------------------------
> First XML		| Second XML			
> ------------------------------------------------------------
> 		|
> 		|	Added Element(s):
> 		|	Item>comment
> 		|	value: additional comments
> ------------------------------------------------------------
> 		|
> Deleted Element(s):	|
> Item>comment	|
> value: Pen		|
> ------------------------------------------------------------
> Changed Element:old	|	Changed Element:new
> Item>Description	|	Item>Description
> value: Highlighter	|	value: highlighter
> ------------------------------------------------------------


Here is a sample stylesheet that should give you an idea:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

   <xsl:output method="html" indent="yes"/>

   <xsl:template match="/">
     <html lang="en">
       <head>
         <title>Example</title>
       </head>
       <body>
         <table>
           <thead>
             <tr>
               <th>First XML</th>
               <th>Second XML</th>
             </tr>
           </thead>
           <tbody>
             <xsl:apply-templates/>
           </tbody>
         </table>
       </body>
     </html>
   </xsl:template>

   <xsl:template match="comment()[. = '  Changed Element:old ']">
     <tr>
       <td>
         <xsl:text>Changed element:old</xsl:text>
         <br/>
         <xsl:text>Item></xsl:text>
         <xsl:value-of select="name(following-sibling::*[1])"/>
         <br/>
         <xsl:text>value: </xsl:text>
         <xsl:value-of select="following-sibling::*[1]"/>
       </td>
       <td>
         <xsl:text>Changed element:new</xsl:text>
         <br/>
         <xsl:text>Item></xsl:text>
         <xsl:value-of select="name(following-sibling::comment()[. = ' 
Changed Element:new '][1]/following-sibling::*[1])"/>
         <br/>
         <xsl:text>value: </xsl:text>
         <xsl:value-of select="following-sibling::comment()[. = ' 
Changed Element:new '][1]/following-sibling::*[1]"/>
       </td>
     </tr>
   </xsl:template>

   <xsl:template match="comment()[. = '  Deleted Element(s) ']">
     <tr>
       <td>
         <xsl:text>Deleted element(s):</xsl:text>
         <br/>
         <xsl:text>Item></xsl:text>
         <xsl:value-of select="name(following-sibling::*[1])"/>
         <br/>
         <xsl:text>value: </xsl:text>
         <xsl:value-of select="following-sibling::*[1]"/>
       </td>
       <td></td>
     </tr>
   </xsl:template>

   <xsl:template match="comment()[. = '  Added Element(s) ']">
     <tr>
       <td></td>
       <td>
         <xsl:text>Added element(s):</xsl:text>
         <br/>
         <xsl:text>Item></xsl:text>
         <xsl:value-of select="name(following-sibling::*[1])"/>
         <br/>
         <xsl:text>value: </xsl:text>
         <xsl:value-of select="following-sibling::*[1]"/>
       </td>
     </tr>
   </xsl:template>

   <xsl:template match="text()"/>

</xsl:stylesheet>

Its output, when run against the XML sample you posted, is as follows:

<html lang="en">
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

       <title>Example</title>
    </head>
    <body>
       <table>
          <thead>
             <tr>
                <th>First XML</th>
                <th>Second XML</th>
             </tr>
          </thead>
          <tbody>
             <tr>
                <td>Changed element:old<br>Item&gt;Description<br>value: 
Marker
                </td>
                <td>Changed element:new<br>Item&gt;Description<br>value: 
Mark er
                </td>
             </tr>
             <tr>
                <td>Changed element:old<br>Item&gt;Description<br>value: 
Eraser
                </td>
                <td>Changed element:new<br>Item&gt;Description<br>value: 
eraser
                </td>
             </tr>
             <tr>
                <td></td>
                <td>Added element(s):<br>Item&gt;comment<br>value: 
additional comments
                </td>
             </tr>
             <tr>
                <td>Deleted element(s):<br>Item&gt;comment<br>value: Pen
                </td>
                <td></td>
             </tr>
             <tr>
                <td>Changed element:old<br>Item&gt;Description<br>value: 
Highlighter
                </td>
                <td>Changed element:new<br>Item&gt;Description<br>value: 
highlighter
                </td>
             </tr>
          </tbody>
       </table>
    </body>
</html>


-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent