Amy01
asked on
XML and XSL
Dear experts,
I'm developing a XML web tool to allow the user to change, add, or delete xml elements and attributes. The following test.xsl can only show the information of one element "command" ( mytree.xml ). It doesn't work if there are more elements of "command" ( mytree2.xml ). I also want to know how to display the details of the element and its' attriubites into the form input fields in the right frame when the user clicks on any node on the left frame. Please advice me with code. Thanks!
****************
test.xsl
****************
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--containers-->
<xsl:template match="*[*]">
<xsl:variable name="id" select="command" />
<div>
<img src="images/collapse.gif" onclick="toggleMenu('{$id} ');" name="img{$id}" style="cursor:hand;"/>
<span class="item">
<xsl:value-of select="name(.)"/>: [<xsl:value-of select="@name" />]
</span>
<div style="display:none;" id="container{$id}" class="container">
<xsl:apply-templates select="*" />
</div>
</div>
</xsl:template>
<!--leaf objects-->
<xsl:template match="*[not(*)]">
<div>
<img src="images/leaf.gif"/>
<span class="item">
<xsl:value-of select="name(..)"/>::<xsl: value-of select="name(.)"/>: [<xsl:value-of select="@name"/>]
</span>
</div>
</xsl:template>
</xsl:stylesheet>
I'm developing a XML web tool to allow the user to change, add, or delete xml elements and attributes. The following test.xsl can only show the information of one element "command" ( mytree.xml ). It doesn't work if there are more elements of "command" ( mytree2.xml ). I also want to know how to display the details of the element and its' attriubites into the form input fields in the right frame when the user clicks on any node on the left frame. Please advice me with code. Thanks!
****************
test.xsl
****************
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--containers-->
<xsl:template match="*[*]">
<xsl:variable name="id" select="command" />
<div>
<img src="images/collapse.gif" onclick="toggleMenu('{$id}
<span class="item">
<xsl:value-of select="name(.)"/>: [<xsl:value-of select="@name" />]
</span>
<div style="display:none;" id="container{$id}" class="container">
<xsl:apply-templates select="*" />
</div>
</div>
</xsl:template>
<!--leaf objects-->
<xsl:template match="*[not(*)]">
<div>
<img src="images/leaf.gif"/>
<span class="item">
<xsl:value-of select="name(..)"/>::<xsl:
</span>
</div>
</xsl:template>
</xsl:stylesheet>
ASKER
*******************
modifyXML.html
*******************
<HTML>
<HEAD>
<TITLE>Modify XML Document</TITLE>
</HEAD>
<BODY>
<IFRAME ALIGN=LEFT FRAMEBORDER=1 HEIGHT=300 WIDTH=75% MARGINHEIGHT=0 MARGINWIDTH=0 NAME=XMLTree SCROLLING=yes SRC="./folderTreeFrame.jsp ">
</BODY>
</HTML>
********************
XMLTreeFrame.html
********************
<html>
<head>
<title>XMLTree</title>
</head>
<FRAMESET cols="200,*">
<FRAME src="./treeTest.html" name="treeframe" >
<FRAME SRC="./folderTreeBlankFram e.jsp" name="basefrm">
</FRAMESET>
</HTML>
**************
mytree.xml
**************
<?xml version="1.0"?>
<project name="XML Generator Application">
<command name="View">
<codeBase>xmlgenerator.com mands.View </codeBase >
<target name="view">/viewXMLDocume nt.jsp</ta rget>
<target name="modify">/modifyXMLDo cument.jsp </target>
<requestparameter get="true" set="false" name="userFile">java.lang. String</re questparam eter>
<requestparameter get="true" set="false" name="action">java.lang.St ring</requ estparamet er>
<requestparameter get="true" set="false" name="xmlDocument">java.la ng.String< /requestpa rameter>
<requestparameter get="true" set="true" name="xmlFile">java.lang.S tring</req uestparame ter>
</command>
</project>
****************
mytree2.html
****************
<?xml version="1.0"?>
<project name="XML Generator Application">
<command name="View">
<codeBase>xmlgenerator.com mands.View </codeBase >
<target name="view">/viewXMLDocume nt.jsp</ta rget>
<target name="modify">/modifyXMLDo cument.jsp </target>
<requestparameter get="true" set="false" name="userFile">java.lang. String</re questparam eter>
<requestparameter get="true" set="false" name="action">java.lang.St ring</requ estparamet er>
<requestparameter get="true" set="false" name="xmlDocument">java.la ng.String< /requestpa rameter>
<requestparameter get="true" set="true" name="xmlFile">java.lang.S tring</req uestparame ter>
<requestparameter get="false" set="true" name="msg">java.lang.Strin g</request parameter>
<requestparameter get="false" set="true" name="viewList">java.util. Vector</re questparam eter>
<requestparameter get="false" set="true" name="nodes">java.util.Col lection</r equestpara meter>
</command>
<command name="Modify">
<codeBase>xmlgenerator.com mands.View </codeBase >
<target name="view">/viewXMLDocume nt.jsp</ta rget>
<target name="modify">/modifyXMLDo cument.jsp </target>
<requestparameter get="true" set="false" name="userFile">java.lang. String</re questparam eter>
<requestparameter get="true" set="false" name="action">java.lang.St ring</requ estparamet er>
<requestparameter get="true" set="false" name="xmlDocument">java.la ng.String< /requestpa rameter>
<sessionattribute get="true" set="true" name="id">java.lang.String </requestp arameter>
</command>
</project>
modifyXML.html
*******************
<HTML>
<HEAD>
<TITLE>Modify XML Document</TITLE>
</HEAD>
<BODY>
<IFRAME ALIGN=LEFT FRAMEBORDER=1 HEIGHT=300 WIDTH=75% MARGINHEIGHT=0 MARGINWIDTH=0 NAME=XMLTree SCROLLING=yes SRC="./folderTreeFrame.jsp
</BODY>
</HTML>
********************
XMLTreeFrame.html
********************
<html>
<head>
<title>XMLTree</title>
</head>
<FRAMESET cols="200,*">
<FRAME src="./treeTest.html" name="treeframe" >
<FRAME SRC="./folderTreeBlankFram
</FRAMESET>
</HTML>
**************
mytree.xml
**************
<?xml version="1.0"?>
<project name="XML Generator Application">
<command name="View">
<codeBase>xmlgenerator.com
<target name="view">/viewXMLDocume
<target name="modify">/modifyXMLDo
<requestparameter get="true" set="false" name="userFile">java.lang.
<requestparameter get="true" set="false" name="action">java.lang.St
<requestparameter get="true" set="false" name="xmlDocument">java.la
<requestparameter get="true" set="true" name="xmlFile">java.lang.S
</command>
</project>
****************
mytree2.html
****************
<?xml version="1.0"?>
<project name="XML Generator Application">
<command name="View">
<codeBase>xmlgenerator.com
<target name="view">/viewXMLDocume
<target name="modify">/modifyXMLDo
<requestparameter get="true" set="false" name="userFile">java.lang.
<requestparameter get="true" set="false" name="action">java.lang.St
<requestparameter get="true" set="false" name="xmlDocument">java.la
<requestparameter get="true" set="true" name="xmlFile">java.lang.S
<requestparameter get="false" set="true" name="msg">java.lang.Strin
<requestparameter get="false" set="true" name="viewList">java.util.
<requestparameter get="false" set="true" name="nodes">java.util.Col
</command>
<command name="Modify">
<codeBase>xmlgenerator.com
<target name="view">/viewXMLDocume
<target name="modify">/modifyXMLDo
<requestparameter get="true" set="false" name="userFile">java.lang.
<requestparameter get="true" set="false" name="action">java.lang.St
<requestparameter get="true" set="false" name="xmlDocument">java.la
<sessionattribute get="true" set="true" name="id">java.lang.String
</command>
</project>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It also need catch the element "project"
<?xml version="1.0"?>
<project name="XML Generator Application">
<command name="View">
<codeBase>xmlgenerator.com mands.View </codeBase >
<target name="view">/viewXMLDocume nt.jsp</ta rget>
<target name="modify">/modifyXMLDo cument.jsp </target>
<requestparameter get="true" set="false" name="userFile">java.lang. String</re questparam eter>
<requestparameter get="true" set="false" name="action">java.lang.St ring</requ estparamet er>
<requestparameter get="true" set="false" name="xmlDocument">java.la ng.String< /requestpa rameter>
<requestparameter get="true" set="true" name="xmlFile">java.lang.S tring</req uestparame ter>
<requestparameter get="false" set="true" name="msg">java.lang.Strin g</request parameter>
<requestparameter get="false" set="true" name="viewList">java.util. Vector</re questparam eter>
<requestparameter get="false" set="true" name="nodes">java.util.Col lection</r equestpara meter>
</command>
<command name="Modify">
<codeBase>xmlgenerator.com mands.View </codeBase >
<target name="view">/viewXMLDocume nt.jsp</ta rget>
<target name="modify">/modifyXMLDo cument.jsp </target>
<requestparameter get="true" set="false" name="userFile">java.lang. String</re questparam eter>
<requestparameter get="true" set="false" name="action">java.lang.St ring</requ estparamet er>
<requestparameter get="true" set="false" name="xmlDocument">java.la ng.String< /requestpa rameter>
<sessionattribute get="true" set="true" name="id">java.lang.String </requestp arameter>
</command>
</project>
<?xml version="1.0"?>
<project name="XML Generator Application">
<command name="View">
<codeBase>xmlgenerator.com
<target name="view">/viewXMLDocume
<target name="modify">/modifyXMLDo
<requestparameter get="true" set="false" name="userFile">java.lang.
<requestparameter get="true" set="false" name="action">java.lang.St
<requestparameter get="true" set="false" name="xmlDocument">java.la
<requestparameter get="true" set="true" name="xmlFile">java.lang.S
<requestparameter get="false" set="true" name="msg">java.lang.Strin
<requestparameter get="false" set="true" name="viewList">java.util.
<requestparameter get="false" set="true" name="nodes">java.util.Col
</command>
<command name="Modify">
<codeBase>xmlgenerator.com
<target name="view">/viewXMLDocume
<target name="modify">/modifyXMLDo
<requestparameter get="true" set="false" name="userFile">java.lang.
<requestparameter get="true" set="false" name="action">java.lang.St
<requestparameter get="true" set="false" name="xmlDocument">java.la
<sessionattribute get="true" set="true" name="id">java.lang.String
</command>
</project>
Yes, I appreciate that, but can you tell me why the "catch alls" or if you don't understand me, why the *[*]? What is it meant to match exactly? (Rat is probably not in full form today!)
ASKER
*[*] matches any element on the tree except leaf elements.
ASKER
Here is my solution:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--containers-->
<xsl:template match="*[*]">
<xsl:variable name="id" select="*" />
<div>
<img src="images/collapse.gif" onclick="toggleMenu('{$id} ');" name="img{$id}" style="cursor:hand;"/>
<span class="item">
<xsl:value-of select="name(.)"/>: [<xsl:value-of select="@name" />]
</span>
<div style="display:none;" id="container{$id}" class="container">
<xsl:apply-templates select="*" />
</div>
</div>
</xsl:template>
<!--leaf objects-->
<xsl:template match="*[not(*)]">
<xsl:variable name="id" />
<div>
<img src="images/leaf.gif"/>
<span class="item">
<xsl:value-of select="name(..)"/>::<xsl: value-of select="name(.)"/>: [<xsl:value-of select="@name" />]
</span>
</div>
</xsl:template>
</xsl:stylesheet>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--containers-->
<xsl:template match="*[*]">
<xsl:variable name="id" select="*" />
<div>
<img src="images/collapse.gif" onclick="toggleMenu('{$id}
<span class="item">
<xsl:value-of select="name(.)"/>: [<xsl:value-of select="@name" />]
</span>
<div style="display:none;" id="container{$id}" class="container">
<xsl:apply-templates select="*" />
</div>
</div>
</xsl:template>
<!--leaf objects-->
<xsl:template match="*[not(*)]">
<xsl:variable name="id" />
<div>
<img src="images/leaf.gif"/>
<span class="item">
<xsl:value-of select="name(..)"/>::<xsl:
</span>
</div>
</xsl:template>
</xsl:stylesheet>
Interesting...
In the leaf template the <xsl:variable name="id" />
is unused and redundant.
I must admit I do like the *[not(*)] construct! It appeals to Rat's failble for the obscure!
In the leaf template the <xsl:variable name="id" />
is unused and redundant.
I must admit I do like the *[not(*)] construct! It appeals to Rat's failble for the obscure!
ASKER
treeTest.html
**************
<html>
<head>
<title>Tree Menu Through MSIE Transformation</title>
<script language="javascript">
<!--
var imgExpand = new Image();
imgExpand.src = "images/expand.gif";
var imgCollapse = new Image();
imgCollapse.src = "images/collapse.gif";
function toggleMenu(szID) {
var szImage = "img" + szID;
var img = document.images ? document.images[szImage] : null;
var szContainer = "container" + szID;
var container = document.all ? document.all[szContainer] : document.getElementById ?
document.getElementById(sz
if (! img || ! container) return;
if (container.style.display == "none") {
img.src = imgExpand.src;
container.style.display = "block";
} else {
img.src = imgCollapse.src;
container.style.display = "none";
}
}
window.onload = init;
function init() {
// use the += operator which appends
// using the = replaces the innerHTML and removes the reference to the data islands
// that would generate errors
document.body.innerHTML += oXML.transformNode(oXSLT);
}
//-->
</script>
<style type="text/css">
<!--
body {font-family:Verdana;lette
a {text-decoration:none;colo
a:hover {text-decoration:underline
div.root, div.container, div.item {padding-left:10px;}
//-->
</style>
</head>
<body>
<XML id="oXML" src="mytree.xml"></XML>
<XML id="oXSLT" src="test.xsl"></XML>
</body>
</html>