Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

Ayan

unread,
Oct 30, 2009, 1:55:01 AM10/30/09
to
Hi Friends

Greetings!

I have an XML file which contains difference between two xml like this:

<?xml version="1.0" encoding="UTF-8" ?>
<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
------------------------------------------------------------


Now, the tags "Item", "comment" etc are not fixed. It depends on the XML we
are comparing. So need to

track on the basis of <!-- Added Element(s) -->, <!-- Deleted Element(s)
-->, <!-- Changed

Element:new --> etc. and need to render the table printing the values and
attributes in the tags.

Please ask me anything you like.
Thanks a ton in advance

Ayan

Martin Honnen

unread,
Oct 30, 2009, 7:10:46 AM10/30/09
to

Ayan wrote:


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/

Ayan

unread,
Nov 4, 2009, 12:49:01 AM11/4/09
to
Thanks a lot Martin. This is just what I needed. This will be of much help!!

Cheers
Ayan

0 new messages