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 8th, 2006, 08:24 PM
Registered User
 
Join Date: Jul 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default need help in generating static ID

Hi,

My XSLT and XML files are below. What I'm hoping to achieve is that when I click the hyperlink, its corresponding section should appear and then disappear on the next click.

XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="profile_style.xsl"?>

<CUSTOMER>
    <FAQ>
        <QUESTION id="1">1044 Error Message</QUESTION>
        <GUIDE>This is the guide to answer to error 1044.</GUIDE>
        <ANSWER>This is the answer to error 1044.</ANSWER>
    </FAQ>

    <FAQ>
        <QUESTION id="2">1023 Error Message</QUESTION>
        <GUIDE><b>This is the guide to answer to error 1023.</b></GUIDE>
        <ANSWER>This is the answer to error 1023.</ANSWER>
    </FAQ>

</CUSTOMER>


And here is my XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0"/>
<xsl:template match="/">
      <html>
      <head>
    <script language="javascript">
    {
        function showWindow(x){
            if(document.getElementById(x).style.display == "none"){
                document.getElementById(x).style.display = "block"
            }else{
                document.getElementById(x).style.display = "none"
            }
        }
    }
    </script>
      </head>
      <body>
          <xsl:for-each select="CUSTOMER/FAQ">
        <xsl:variable name="grID" select="generate-id()"/>
        <script type="text/javascript">
        {
            var getID= '<xsl:copy-of select="$grID"/>'
             document.write('<a onclick='showWindow(getID)' href="#"><xsl:value-of select="$grID" /></a>')
             document.write('<div id="' + getID + '" style="display: block">HELLO WORLD</div>')
        }
        </script>
        <br />
             </xsl:for-each>
        </body>
      </html>
</xsl:template>
</xsl:stylesheet>

Hope you can help me out :)

~ Marvine

 
Old July 9th, 2006, 03:25 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try to create an HTML page by hand that does what you want. Then try to write the XSLT to generate this HTML. If you can't write the HTML, then you have an HTML problem not an XSLT problem (which means you're on the wrong forum). If you can write the HTML, then post it here as the desired output of the transformation.

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

Sorry I think my description was a little vague. Let's look at the line:

<xsl:variable name="grID" select="generate-id()"/>
<script type="text/javascript">
{
  var getID= '<xsl:copy-of select="$grID"/>'
  document.write('<a onclick='showWindow(getID)' href="#"><xsl:value-of select="$grID" /></a>')

When you open the XML file, you will have two links. If you click on one of them, the corresponding <p> will appear. When you click the second link, it hides/displays the <p> section. However, if you click the first link, it hides/displays the second link rather than the first <p> section. This is because of the current generate-id().

I have tried using <xml:number/> to use it as a counter but the result is still the same.

Now, is there a way to make this work properly? HTML is not the problem but the "counter" is. Imagine I have a hundred of <FAQ> and it only shows/hides the last item.

Please let me know.


 
Old July 10th, 2006, 02:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please tell me what HTML your code is generating, and what HTML you would like it to generate. Then I might be able to help you.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 10th, 2006, 06:29 PM
Registered User
 
Join Date: Jul 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply. Here is what the output should look like and do. When you click "This is line 1", HELLO WORLD should show/hide under it. When you click "This is line 2", HELLO WORLD should show/hide under the second one.

<html>
<head>
    <script language="javascript">
    {
        function showWindow(x){
            if(document.getElementById('window' + x).style.display == "none"){
                document.getElementById('window' + x).style.display = "block"
            }else{
                document.getElementById('window' + x).style.display = "none"
            }
        }
    }
    </script>
    </head>

    <body>
        <a onclick='showWindow(1)' href="#">This is line 1.</a>
        <div id="window1" style="display: none">HELLO WORLD</div>
        <br>
        <a onclick='showWindow(2)' href="#">This is line 1.</a>
        <div id="window2" style="display: none">HELLO WORLD</div>
        <br />
    </body>
</html>


In my files, window1 and window2 are generated with the help of generate-id() function. However, the output of my XML and XSL is different. When you click either the first/second line, the HELLO WORLD would always hide/show under the second line.

I hope this helps a little.

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

Instead of having your XSLT generate Javascript which when executed does calls like document.write() to create elements in the HTML page, why not simply have your XSLT generate the HTML elements directly? It's too late at night to do the detail, but you want to replace

<script type="text/javascript">
        {
            var getID= '<xsl:copy-of select="$grID"/>'
             document.write('<a onclick='showWindow(getID)' href="#"><xsl:value-of select="$grID" /></a>')
             document.write('<div id="' + getID + '" style="display: block">HELLO WORLD</div>')
        }
        </script>

with something like

<a onclick="showWindow({position()})" href="#">This is line <xsl:value-of select="position()"/>.</a>
        <div id="{generate-id()}" style="display: none">HELLO WORLD</div>
        <br>



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

Thanks buddy. It works!!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
Generating Email Id vijaypalada ASP.NET 1.0 and 1.1 Basics 1 January 20th, 2007 05:49 PM
'this.ID = id;' in class construction holf BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 October 6th, 2006 10:58 AM
PLZ HELP IN GENERATING MY OWN SESSION ID anand_asv .NET Framework 2.0 0 March 3rd, 2005 03:49 AM
why not index.asp?id=1 can be www.myweb.com/?id=1 BurhanKhan Classic ASP Professional 11 September 6th, 2004 02:06 PM





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