Hello All,
I am currently working on an
application whose problem defination is
as follows :-
PROBLEM DEFINATION
Given an xml file as input produce the following as output
(coded in HTML) :-
1) A tree structure representing element names, in a
hierarchical order,
for all elements of the inputted XML file in the left frame of
a frameset.
2) On clicking each sibling of root element , produce 2
buttons on the
right frame
Caption on button 1 => Insert new element instance of
element type selected
on left frame
Function of button 1 => On pressing button 1, user should
be presented
with a text box to enter element content for a new instance
of the element type selected on left frame.This new element
instance is to be
added to the input XML file.
Caption on button 2 => Retrieve element content of element
selected
on left frame
Function of button 2 => On pressing button 2, user should
be presented
with data corresponding to the element
selected in the left frame.
let me give an eg.
For the following sample XML file as INPUT :-
<?xml version="1.0"?>
<Root>
<Child1>Data1</Child1>
<Child2>Data2</Child2>
</Root>
The browser OUTPUT should be
* Button for Insert new element
Root
instance of type
Child1 selected
on left frame
Child2
* Button for Retrieve
element
content
of element selected on
left
frame
On selecting either Child1 or Child2 in the left frame, I
would get the
2 buttons on the right frame as described above.
Now I can either insert a new element instance or retrieve
contents
of a element selected in the left frame by clicking the
relevant buttons.
APPROACH
1) I am currently planning to use XSL to
* write patterns
- whose rules will "match" the element
hierarchy of the
source XML file
- whose templates will contain HTML code to
produce
the tree struture on the left frame and the 2 buttons on
the right frame.
My XSL file would look something like
1. <xsl:stylesheet>
2. <xsl:define-macro
name="AnyChild">
3. <xsl:for-each
select="./*[only-of-any()]">
/*The following to
be included here
- HTML
code for writing child node name on left frame
-
OnClick events for buttons which will invoke the
script functions for adding new element of type selected
on left frame or retrieving ele. content of ele. selected
on left frame,
depending upon button pressed.
These functions will use DOM API
calls.
*/
5. </xsl:for-each>
6.
<xsl:for-each select="./*[not-only-of-any()]">.
/*The following to
be included here
- HTML
code for writing child node name on left frame
-
OnClick events for buttons which will invoke the
script functions for adding new element of type selected
on left frame or retrieving ele. content of ele. selected
on left frame,
depending upon button pressed.
These functions will use DOM API
calls.
*/
8. <xsl:invoke
macro="AnyChild"/>
9. </xsl:for-each>
10. </xsl:define-macro>
11. <xsl:template
match="id(Root)">
12. /*The following to be
included here
- HTML
code for building frameset
-
SCRIPTS for writing functions for
- Inserting new element instance of element type selected
on
left frame
- Retrieving element content of element selected
on
left frame
-
These functions will use calls from the DOM API to retrieve
ele. content or insert new elements
*/
13. <xsl:process-children/>
14. </xsl:template>
15. <xsl:template
match="id(Root)/*">
/*The following to
be included here
- HTML
code for writing matched
element node name on left frame
*/
17. <xsl:invoke macro="AnyChild"/>
18.
</xsl:template>
19. </xsl:stylesheet>
A brief explanation of the XSL file.
* Lines 11-14 => This is the template rule for the
identifying the
element which will have an ID type attribute with value =
"root".
The template of this rule will be responsible for construcing
the frameset
and also including definations of
scripting functions for inserting elements / retrieving
element content.
These functions will include DOM API calls for
insertion/deletion.
* Lines 15-18 => This is the template rule for identifying
the
immediate children of the root element.
The template will write the matched element name on
the left frame.At Line 17, the macro named
"AnyChild" is called.
* Lines 2-10 => This includes the defination for the macro
"AnyChild".
- Between Line 3 - 5 => The instruction at
this point will find
all those siblings of the current element node which have no
siblings.
On finding such elements the instruction will write
the selected node names on the left frame in proper
hierarchical order.
- Between Line 6 - 8 => The instruction at
this point will find
all those siblings of the current element node that have
siblings.
On finding such elements their node names will be written
on the left frame in proper hierarchical order.
For each element node in the selected list
the macro "AnyChild" is called recursively again (At
Line 8) till there are
siblings left which have furthur siblings.
CONFUSION
The problem defination states (indirectly though) that I need
to traverse
the input source XML file to produce an output tree structure
of it.
In my approach, I am using XSL template rules to traverse the
src. XML tree, by using the match /select syntax.
This could very easily be done using DOM API calls as well
instead of using XSL rules.
But if I were to use a DOM implementation instead of XSL , how
would
I the generate the HTML output on traversing parts of the
tree?
DOM gives no mechanism to do that!
If I were to stick to XSL itself, well then how would I
include
scripts within XSL?
So, my confusion lies in whether to use an XSL implementation
or a DOM implementation.
I have infact listed down a few points which favour/oppose DOM
and XSL.
Favouring DOM
* Since I am iterating through the source XML tree to produce
output
(in the eg. above) which can be viewed through a browser
(as HTML code now, XSL flow objects once browsers
incorporate
XSL engines to understand styling objects),
DOM gives a very clean API for tree iteration.
* Using DOM I can also insert new elements/retrieve elements
into my src.
tree very easily, if the user wishes to add a new element
through
the buttons on the right frame.
Against DOM
* I need to output a tree structure
which is visible on a browser
(an HTML browser now, an XML
browser once things stabilize).
DOM does not deal with formatting characteristics , so
how do I produce
my output (HTML output now, XSL style flow objects once
browsers incorporate
XSL engines to understand styling objects) using a DOM
implementation?
In other words I will need to work up a solution to output the
"formatting code".
Favouring
XSL
* In XSL , we have a readymade
framework which
- through the pattern
language with it's match/select mechanism can emulate
iterating through the source XML tree. eg.
- through the formatting object
vocabulary , gives us a way to produce browser
displayable output, without
me having to cook up any solution to display output.
(unlike a DOM implementation where I would have
to).
Against XSL
* What if I wish to add a new
element to my src. XML tree?
How
would I achieve that through XSL?
If it is through the use of scripts within XSL then
- Scripting within XSL is still being
finalised
- Assuming scripts would be incorporated ,
would they be capable
of supporting insertion/deletion of
elements to src. XML tree like DOM does?
Keeping all these arguements in mind
QUESTIONS
* Which of the two approches shall I use for my application ,
DOM or XSL?
* Is it adviceable to go in for a combination of
both as I intend doing now?
In this case I would code an XSL
stylesheet in which I somehow
(through HTML <SCRIPT> maybe)
would embed a DOM implementation
within the templates of rules to help insert new elements/retrieve
element
content of selected
elements.
This way
- XSL would be used to transform
src. XML tree to result HTML tree
- DOM embedded within the
result tree would be used to for inserting
new
elements/retrieving element content to/from the result tree.
I'd be grateful if someone could kindly
suggest which amongst the
2 approaches would be ideal for my scenario.
Thanks in advance for any help,
AMIT