Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 23rd, 2006, 03:54 AM
Authorized User
 
Join Date: May 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default show element button

I am trying to create a HTML documen from XML using XSLT to create a situation where each time a certain element occurs, a button is displayed which can be used to show hidden content.

So for example in the XML below, the content of the example element can be displayed if a button is clicked.

<article xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://docbook.org/ns/docbook
schema/docbook.xsd">
    <title>something</title>
    <section sensing="1" intuitive="2">
        <title>First</title>
        <example show="off" label="1">
            <title>first example</title>
            <para>example text</para>
        </example>
        <para>para First 1</para>
        <para>para First 2</para>
    </section>
    <section sensing="2" intuitive="1">
        <title>Second</title>
        <example show="off" label="2">
            <title>second example</title>
            <para>example text</para>
        </example>
        <para>para Second 1</para>
        <para>para Second 2</para>
    </section>
</article>

I have a situation where Javascript in an HTML document is used to switch stylesheets associated with that document.

 This is so that the XML can be sorted according to different criteria. It includes a refresh() function which re-loads the styelsheets.

It also includes functions which change the attributes of certain XML elements. Using the refresh() function, this can be used to show or hide these elements depending on the value of their attributes.

The way I do it at the moment ALL the elements of a certain name have their attributes changed.

function changeAtt(ell)
{

var el; /*variable to refer to element name*/
var list; /* variable which will hold list of elements*/

el = ell;

/*get all elements in dataXML according to the value of el*/

list = dataXML.documentElement.getElementsByTagName(el);

/*loop through the elements and set all attributes
* named "show" to the new value of "att"*/

for(var i = 0; i < list.length; i++)
{
list[i].setAttribute("show", "on");

}

refresh();
}


If I include a button in a XSLT template - something like this

<xsl:template match="ns:example">


<button id ="btn2" onclick="changeAtt('example', '0') ; return false" >show examples</button>


<xsl:if test="@show = 'on'">

</xsl:if>

</xsl:template>

And change the function to something like this

function changeAtt(ell, number)
{

var el; /*variable to refer to element name*/
var list; /* variable which will hold list of elements*/

el = ell;
no = number;

/*get all elements in dataXML according to the value of el*/

list = dataXML.documentElement.getElementsByTagName(el);

/*loop through the elements and set all attributes
* named "show" to the new value of "att"*/



list[no].setAttribute("show", "on");
refresh();
}


Then I can specify that a single element has its attribute changed. But of course, this way,each time there is a "example" element, the button that occurs there changes the attribute of the same element - in this case the first example element.

How can I set it up so that the button only changes the attribute of the "current" element?



 
Old July 23rd, 2006, 05:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I would use id attributes on the elements in the XML whose attributes need to be changed, and change your Javascript code to accept this id value as a parameter.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to show a button have hidden property ??? nobitavn94 ASP.NET 1.0 and 1.1 Basics 2 November 19th, 2006 09:39 AM
How to show gif image on mouseover event of button arshad mahmood ASP.NET 1.0 and 1.1 Basics 2 September 14th, 2006 12:15 AM
how to show a button when save is done noor ASP.NET 1.0 and 1.1 Basics 4 May 9th, 2005 12:49 AM
Quiz how to show button IF they get 70% or more? edkeyte Javascript How-To 4 January 22nd, 2004 08:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.