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 June 30th, 2006, 01:24 PM
Registered User
 
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding Classes OnMouseOver

Perhaps someone can help me with this dilema?

I have the following format:

<root>
<element>
<info>
<name>Example</name>
</info>
<relationships>
<child id="Example">A Child</child>
<child id="Example">Another Child</child>
</relationships>
</element>

<element>
<info>
<name>A Child</name>
</info>
</element>

<element>
<info>
<name>Another Child</name>
</info>
</element>

</root>

I have these elements spread out into a table with different <td>'s.

I want it so that when I put my mouse over "Example," it does some kind of style adjustment to "Example," "Another Child", and "A Child" (i.e. all of the elements under its relationship node, and itself)... for example changing the background color of them or such.

I *do* have the opportunity to re-label the child ID if that makes things easier, however, I felt that would be the easiest way to link the two together.

Thank you in advance!
 
Old June 30th, 2006, 06:57 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT is used to transform XML into HTML. If you don't know what HTML you want to generate, then you have an HTML problem rather than an XSLT problem, so it would be better to ask on a different forum.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 7th, 2006, 01:00 PM
Registered User
 
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are absolutely right.

What about this?

What if the XSLT script scans all <child> elements and adds a class equal to the ID to each value of the <child> elements

so in the html revsion of "Another Child" and "A Child" would both contain a class="Example" along with other classes they already contain.

This would be a pure XSLT problem, and with this solution, I can code the rest.

Thanks!
 
Old July 7th, 2006, 02:54 PM
Registered User
 
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried putting this into <td>A Child</td>:

          <xsl:for-each select="root/element/relationships">
            <xsl:if test="child='{$nam}'"> // Where $nam = name of element (A Child)
              <xsl:attribute name="class"><xsl:value-of select="@id"></xsl:value-of></xsl:attribute>
            </xsl:if>
          </xsl:for-each>


Had no luck, however. I think this is the way to go, but obviously I'm missing something..
 
Old July 7th, 2006, 04:03 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your last two posts don't make it clear whether you still have a problem, and if so, what the problem is.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 7th, 2006, 04:35 PM
Registered User
 
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah pretend i'm starting all over (I still have a problem)

My problem is that with this XML:

<root>
<element>
<info>
<name>Example</name>
</info>
<relationships>
<child id="Example">A Child</child>
<child id="Example">Another Child</child>
</relationships>
</element>

<element>
<info>
<name>A Child</name>
</info>
</element>

<element>
<info>
<name>Another Child</name>
</info>
</element>

</root>

I have it translated into <TD>s of a table for example <table><tr><td>Example</td><td>Another Child</td><td><A Child></td></tr></table>

I want the XSLT script to scan all <child> elements and add a class equal to the ID to each value of the <child> elements

so in the html output, the TD "Another Child" and TD "A Child" would both contain a class="Example" along with other classes they already contain.

I tried doing it like this

<td>A Child
  <xsl:for-each select="root/element/relationships">
    <xsl:if test="child='{$nam}'"> // Where $nam = name of element (A Child)
      <xsl:attribute name="class"><xsl:value-of select="@id"></xsl:value-of></xsl:attribute>
    </xsl:if>
   </xsl:for-each>
</td>

but it did not work. I could have sworn that would have worked, but it didn't. Do you have any other ideas, or maybe a correction to what I tried?

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

You've shown a snippet of your XSLT code, but you've crucially left out any information about its context. You haven't shown what the context node is for this piece of XSLT, and you haven't shown how the variable $nam is initialized.

There are four things that are obviously wrong with your code, apart from any errors that I can't detect because of the missing context. Firstly, you never use curly braces inside an XPath expression, you only use them to surround an XPath expression in an ordinary attribute. So change

child='{$nam}'

to

child=$nam

Secondly, you can't create an attribute for an element after creating its children (child elements or child text nodes). So you can't output "A Child" until after the xsl:attribute.

Thirdly, you are iterating over "relationships" elements, but there is only one. I suspect you want to iterate over "child" elements. So change for-each select="root/element/relationships" to select="root/element/relationships/child". (Then in the xsl:if, child will be the context node, so change "child" to ".")

Finally, you are referring to @id when the context node is the "relationships" element, but @id appears on the "child" element. This error will go away when you fix the previous problem

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 11th, 2006, 12:36 PM
Registered User
 
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

$nam is a <xsl:variable> declaration:

 <xsl:variable name="nam" select="name"></xsl:variable>

Why doesn't this work? :

<td>
  <xsl:for-each select="root/element/relationships/child">
    <xsl:if test=".=$nam">
      <xsl:attribute name="class"><xsl:value-of select="@id"></xsl:value-of></xsl:attribute>
    </xsl:if>
   </xsl:for-each>

A Child

</td>
 
Old July 11th, 2006, 12:54 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The very fact that you're showing me the variable declaration of $nam, initialized to select="name" (which means select="child::name") suggests that you haven't understood the importance of context. That variable declaration means nothing unless one knows what the context node is at the time it is evaluated.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 11th, 2006, 01:03 PM
Registered User
 
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ahh.. understood. Are there ways of setting permanent variables for this purpose? i.e. the variable is stored for the context node at the present time and can be referenced later in the program?

For example setting a variable in a portion at the beginning of the XSLT script for a given context node, and then referencing it later when the context has changed, but not change the value...

Thanks for all your help by the way.





Similar Threads
Thread Thread Starter Forum Replies Last Post
onmouseover question??? RinoDM Javascript 3 May 30th, 2008 03:01 AM
onmouseover sstuber BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 April 29th, 2008 12:30 PM
onmouseover gilgalbiblewheel HTML Code Clinic 4 February 4th, 2005 08:05 AM





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