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 12th, 2009, 01:50 PM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default Arrays in xslt

Apologies in advance - the title may not adequately describe the problem i'm attempting to solve.

I have the following javascript script

Code:
<script type="text/javascript">
        
 var text_st = new Array("Client 1", "Client2", "Client3", "Client4",                 "Client5", "Client6");

          var l = text_st.length;
          var rnd_no = Math.floor(l*Math.random());

          document.write('<span class="BodyText">');
          document.write('<a href="#">');
          document.write(text_st[rnd_no]);
          document.write('</a>');  
          document.write('</span>');  
        </script>
This script simply displays any of Client1 to Client6 every time the page is refreshed.

I am attempting to replace the values of Client1 to Client6 with xml.

SO far i have done the following...
Code:
<script type="text/javascript">
        
           var text_st = new Array("<xsl:for-each select='$dcr/spotlight'><xsl:value-of select='name'/>,</xsl:for-each>");       




          var l = text_st.length;
          var rnd_no = Math.floor(l*Math.random());

          document.write('<span class="InterflowBodyText">');
          document.write('<a href="#">');
          document.write(text_st[rnd_no]);
          document.write('</a>');  
          document.write('</span>');  
        </script>
This has returned all the values of $dcr/spotlight/name in the page rather than randomly displaying one value when the page is refreshed.

The other question is that I need to value in the href tag to be the corresponding url(of the page) of the Client name.

My xml is below....

Code:
<?xml version="1.0" encoding="UTF-8"  ?> 
  - <Properties Admin="true"  ComponentID="">
   - <Data>
   - <Datum ID="D001"  Name="D00"  Type="DCR">
   - <DCR Category="Customer"  Type="Customerspotlight">
   - <customerspotlight>
   - <spotlight>
     <name>client1</name> 

    <page>client1.page</page>  

   </spotlight>


  - <spotlight>
     <name>client2</name> 

    <page>client2.page</page>  

   </spotlight>


  - <spotlight>
     <name>client3</name> 

    <page>client3.page</page>  

   </spotlight>


  - <spotlight>
     <name>client4</name>  

    <page>client4.page</page>  

   </spotlight>


  - <spotlight>
     <name>client5</name> 

    <page>client5.page</page>  

   </spotlight>


  - <spotlight>
     <name>client6</name> 

    <page>client6.page</page>  

   </spotlight>


  - <spotlight>
     <name>client7</name> 

    <page>client7page</page>  

   </spotlight>


   </customerspotlight>


   </DCR>


   </Datum>
</Data>


    
   </Properties>
Thanks in advance

Last edited by rabs; June 12th, 2009 at 01:51 PM.. Reason: to edit code
 
Old June 12th, 2009, 02:57 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Can I reommend the following as a solution - much easier in XSLT:

var text_st = [];
<xsl:for-each select='$dcr/spotlight'>
text_st.push("<xsl:value-of select='name'/>");
</xsl:for-each>

which should generate:

var text_st = [];
text_st.push("Client1");
text_st.push("Client2");
text_st.push("Client3");
text_st.push("Client4");
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
rabs (June 12th, 2009)
 
Old June 12th, 2009, 03:13 PM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks, that worked.

Do you also have a solution for the following....

The other question is that I need to value in the href tag to be the corresponding url(of the page) of the Client name.
 
Old June 13th, 2009, 03:38 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Just create two arrays, one for the names an one for the href, then use them together.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old June 14th, 2009, 06:24 AM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks. I have created 2 arrays thus...

Code:
<script type="text/javascript">
              var text_st = [];
              var page_st =[];  
              <xsl:for-each select='$dcr/spotlight'>
              text_st.push("<xsl:value-of select='name'/>");
              page_st.push("<xsl:value-of select='page'/>");
              </xsl:for-each> 
              var l = text_st.length;
              var rnd_no = Math.floor(l*Math.random());

              document.write('<span class="InterflowBodyText">');
              document.write('<a href="page_st[rnd_no]">');
              document.write(text_st[rnd_no]);
              document.write('</a>');  
              document.write('</span>');  
            </script>
But I need to use the array page_st within the href i.e
document.write('<a href="page_st[rnd_no]">');. Right now this is rendered as a literal.

Thanks.
 
Old June 14th, 2009, 11:56 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That isn't really a XSLT question anymore, but a Javascript one, and a basic one at that.

I recommend you get yourself a good book on Javascript (something like Javascript - the good parts by Douglas Crockford).

Code:
document.write('<a href="' + page_st[rnd_no] + '">');
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
rabs (June 15th, 2009)
 
Old June 15th, 2009, 06:41 AM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

thanks. That worked. I'm quite rusty on the javascript side of things; thanks for the the tip.
 
Old June 18th, 2009, 01:41 PM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

I have one more question related to this.....

I need want to use the value of an image from xsl instead of the text as the link.

I'm sure I'm doing smthing wrong here.

The arrays of images is as follows.

Code:
logo_st.push("<xsl:attribute name='src'><xsl:value-of select='customerlogo'/></xsl:attribute>");

And I'm attempting to write to the browser thus

Code:
document.write('<a href="'+page_st[rnd_no]+'">');
              document.write('<img border="0" width="120" height="30">'); 
              document.write(logo_st[rnd_no]);
              document.write('</img>');
              document.write('</a>');

But I just get an image icon in the browser.

Thanks
 
Old June 18th, 2009, 06:09 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It is slightly hard seeing as I have no idea what your XML looks like nor what 'customerlogo' contains, but what you have posted is not valid XSLT (as I'm sure you are aware).

An <xsl:attribute> instruction must come inside either an output element or an <xsl:element> instruction.

The following is a guess at what might work:

logo_st.push("<xsl:value-of select='customerlogo'/>");

and

document.write('<a href="'+page_st[rnd_no]+'">');
document.write('<img border="0" width="120" height="30" src="');
document.write(logo_st[rnd_no]);
document.write('"/>');
document.write('</a>');
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
rabs (June 19th, 2009)
 
Old June 19th, 2009, 10:32 AM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks. That was what I needed.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
Multidemmesional Arrays OR arrays gmoney060 Classic ASP Basics 3 November 1st, 2004 03:42 PM





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