Click to See Complete Forum and Search --> : HELP! XML Newbie on XSLT Transformation!!


salnick4
05-27-2009, 08:59 AM
I hope that someone can assist me here...I need to know how to transform a simple xml tree menu into the format that can be seen on either HTML or JP page.

Here is what I have:

1) I have an xml tree menu. Snippet of the code is below:
<menu>

<name>BIMI Menu</name>

<entry>
<name>Home</name>

<url>index.htm</url>

</entry>
<menu>
<name>HBCA</name>
<menu>
<name>Actuate</name>
<menu>
<name>Direct Weekly</name>
<entry>
<name>DR03w - Direct Weekly</name>
<url>#</url>
</entry>
</menu>
<menu>
<name>Intraweek Acquisition Weekly</name>
<entry>
<name>Intraweek Acquisition Weekly</name>
<url>#</url>
</entry>
</menu>
<menu>
<name>OAO (OSA) Weekly</name>
<entry>
<name>DR01w - New Accounts</name>
<url>#</url>
</entry>
<entry>
<name>DR01w - OSA Weekly</name>
<url>#</url>
</entry>
<entry>
<name>DR01w - Promo Code</name>
<url>#</url>
</entry>
</menu>
<menu>
<name>Weekly Commercial Checking</name>
<entry>
<name>Commercial Checking Report - MNY N</name>
<url>#</url>
</entry>
</menu>
</menu>
<menu>
<name>Cognos</name>
<entry>
<name>Product Analysis Monthly Dashboard</name>
<url>#</url>
</entry>
</menu>
</menu>
2) I need to transform the xml using xslt. The xslt code below also contains an imbedded javascript function:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lxslt="http://xml.apache.org/xslt"
xmlns:result="http://www.example.com/results" extension-element-prefixes="result" version="1.0">

<lxslt:component prefix="result" elements="rules" functions="getResult">
<lxslt:script lang="javascript">
function ExpanderClicked()
{
//Get the element that was clicked
var ctlExpander = event.srcElement;
var ctlSelectedEntry = ctlExpander.parentElement;
//Get all the DIV elements that are direct descendants
var colChild = ctlSelectedEntry.children.tags("DIV");
if(colChild.length > 0)
{
var strCSS;
//Get the hidden element that indicates whether or not entry is expanded
var ctlHidden = ctlSelectedEntry.all("hidIsExpanded");

if(ctlHidden.value == "1")
{
//Entry was expanded and is being contracted
ctlExpander.innerHTML = "+&nbsp;";
ctlHidden.value = "0";
strCSS = "NotVisible";
}
else
{
//Entry is being expanded
ctlExpander.innerHTML = "-&nbsp;";
ctlHidden.value = "1";
strCSS = "IsVisible";
}
//Show all the DIV elements that are direct children
for(var intCounter = 0; intCounter < colChild.length; intCounter++)
{
colChild[intCounter].className = strCSS;
}
}
}
</lxslt:script>
</lxslt:component>
<xsl:template match="/">
<xsl:for-each select="//menu/entry">
<xsl:call-template name="SubMenu">
<xsl:with-param name="strCSS">Parent IsVisible</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="SubMenu">
<xsl:param name="strCSS" />

<xsl:variable name="strURL" select="url" />

<div class="{$strCSS}">
<xsl:choose>
<xsl:when test="count(entry) > 0">
<!-- Element has children, it can be expanded -->
<input type="hidden" id="hidIsExpanded" value="0" />
<label id="lblExpand" class="Expander" onclick="ExpanderClicked()">+
</label>
</xsl:when>
<xsl:otherwise>
<label class="Expander"> </label>
</xsl:otherwise>
</xsl:choose>

<a href="{$strURL}"><xsl:value-of select="name" /></a>
<xsl:for-each select="entry">
<xsl:call-template name="SubMenu">
<xsl:with-param name="strCSS">NotVisible</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</div>
</xsl:template>

</xsl:stylesheet>

So I have tried to run these, but have not been successful...please advise what I am doing wrong.

Thank you.

jkmyoung
05-28-2009, 01:48 PM
What is a jp page?
How are you trying to run these? javascript? xml processing instruction?
I think creating a div with class="Parent IsVisible" will probably not work.

salnick4
05-28-2009, 03:53 PM
Thank you for replying!

I was able to resolve my original issue...but it leads me to another...

Maybe you can assist?

I have a tree menu within my xml, and I need to make sure that the <name> tags are treated as lables and my <link> tags are links...

Here is a snippet of my xml:
<name>RM Data Analysis</name>
<entry>
<link>RM191 RM Portfolio - Personal Customer Details By District</link>
<url>rm191.html</url>
</entry>
So here is what I have in my xls:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:for-each select="//menu/entry">
<xsl:call-template name="SubMenu">
<xsl:with-param name="strCSS">Parent IsVisible</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="SubMenu">
<xsl:param name="strCSS" />

<xsl:variable name="strURL" select="url" />
<xsl:variable name="strLABEL" select="name" />

<div class="{$strCSS}">
<xsl:choose>
<xsl:when test="count(entry) &gt; 0">
<!-- Element has children, it can be expanded -->
<input type="hidden" id="hidIsExpanded" value="0" />
<img src="images/fe.gif" id="lblExpand" onclick="ExpanderClicked()" />
</xsl:when>
<xsl:otherwise>
<img src="images/i.gif" class="Expander" />
</xsl:otherwise>
</xsl:choose>

<a href="{$strURL}" target="_blank" class="pnpNavigatorLink"><xsl:value-of select="link" /></a>
<xsl:for-each select="entry">
<xsl:call-template name="SubMenu">
<xsl:with-param name="strCSS">NotVisible</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</div>
</xsl:template>

</xsl:stylesheet>
As you can see I have the line of code that will identify the <link> tags for href...but when I run this, the <name> tags are blank...

Any ideas???

jkmyoung
05-29-2009, 02:21 PM
<xsl:variable name="strLABEL" select="name" />
this assumes the name label is a child in the current element. Instead, you need
preceding-sibling::name
or ../name