Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old September 26th, 2006, 10:09 AM
Authorized User
 
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default Document() ... parameterized ?

Dear All,

I have been experimenting with switching the data sources (i.e. switching between XMLs) and have managed to get my XSL to incorporate more than one XML file. That's great :-)

Now I want to parameterize the whole thing but am not sure weather I am attempting an illegal operation. Maybe somone can check my code. This is a snippet of the problem section ...

<xsl:choose>
 <xsl:when test="$source">
      <table>
      <xsl:value-of select="$source" />
      <xsl:apply-templates select="document($source)" mode="disp">
      </xsl:apply-templates>
     </table>
 </xsl:when>
</xsl:choose>

Here, I use a little block of JavaScript to rewrite the page. This code works fine with many other filtering and sort operations and seems to also be working OK here because my debugging line ...

<xsl:value-of select="$source" />

... successfully displays the item selected.

The variable that I have declared ...

<xsl:param name="source" select="'file2.xml'" />

... also seems to work fine because when I hope up the whole thing, the correct data is displayed.

So therefore I am a bit confused: my debug shows that my javascript has successfully changed the content of $source and the data is displayed when the form is opened (i.e. displayed according to the default value of $source) but when I switch the value, the data refuses to change.

Maybe I have a syntax error? Or maybe it is just not possible to change a Document() declaration 'on-the-fly'? I'm not sure.

Anyway, I hope that someone can help me out with this one because it would be great if I could get the XML sources to switch on command :-)

Many thanks,
Alan Searle

  #2 (permalink)  
Old September 26th, 2006, 10:29 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I'm a bit confused about your process, where the XSLT and JavaScript occur.
Can you show a bit more of the process etc?

--

Joe (Microsoft MVP - XML)
  #3 (permalink)  
Old September 26th, 2006, 12:31 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Supplying a variable, or any other expression, as an argument to the document() function is perfectly OK. It's just like any other function. The one thing that needs a little care is that the treatment of relative URIs is different depending on whether you pass a string or a node. But you're not clear what's happening. I assume it's failing, because otherwise you wouldn't be writing here - but how is it failing? If it can't find the document, then you'll need to tell us more about where the document and the stylesheet are located.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #4 (permalink)  
Old September 27th, 2006, 03:09 AM
Authorized User
 
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

> I'm a bit confused about your process, where the XSLT
> and JavaScript > occur.
> Can you show a bit more of the process etc?

Hi Joe, I did more investigation and diagnosed the problem a bit more: I find it very quirky.

Anyway, I'm posting the reply to Michael on this same thread.

Thanks,
Alan.

  #5 (permalink)  
Old September 27th, 2006, 03:10 AM
Authorized User
 
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mhkay
 Supplying a variable, or any other expression, as an argument to the document() function is perfectly OK. It's just like any other function. The one thing that needs a little care is that the treatment of relative URIs is different depending on whether you pass a string or a node. But you're not clear what's happening. I assume it's failing, because otherwise you wouldn't be writing here - but how is it failing? If it can't find the document, then you'll need to tell us more about where the document and the stylesheet are located.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #6 (permalink)  
Old September 27th, 2006, 03:16 AM
Authorized User
 
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

> Supplying a variable, or any other expression, as an argument to
> the document() function is perfectly OK. It's just like any other
> function.

I did more testing and see this now.

> but how is it failing? If it can't find the document, then you'll
> need to tell us more about where the document and the stylesheet
> are located.

That's the strange thing: The code finds the external document and displays the content (excellent!) but is not selectable.

I have done more diagnostics and here are my results:

Quirky thing ...

Guys: I have found out why I couldn't apply my parameter and, yes, Michael, as you say there is no problem sending and argument to Document().

Here are excerpts from my code. The A:, B:, C: labels indicate the variations which I have tried.

A: and B: work but C: only partly. Here is the code ...

<form name="formfilt" id="formfilt">
  <label for="selsrce">Source</label><br />
  <select size="5" name="selsrce" id="selsrce">

    A: <xsl:apply-templates select="person" mode="fl" />

    B: <option value="Pete">Pete</option>
       <option value="Bob">Bob</option>

    C: <xsl:apply-templates select="document('people.xml')" mode="fl" /> </select>
  <input type="button" value="ausführen" onClick="proc(document.formfilt.selsrce.value)"/>
</form>

[...]

<xsl:template match="people/person" mode="fl">
  <option value="{.}"><xsl:value-of select="."/></option>
</xsl:template>

Here is the explanation ...

I want to use this picklist (<select>) to provide a selectable list to the users and find that ...

A: I can generate a selectable list of people from the base XML (i.e. the XML from where the XSL is called):

<?xml-stylesheet href="data.xsl" type="text/xsl"?>
<?xml version="1.0"?>
<people>
  <person>Pete</person>
  <person>Bob</person>
</people>

B: I can select from a list hard-coded into the XSL.

Both of these options (A: and B:) are selectable and my javascript can process (and display) the result OK.

However, with version C:, where the list is stored in an external XML (i.e. people.xml), I find that I can display the list in the <select> box but, aarrrggghhh, this is not selectable (i.e. my javascript doesn't react).

I have changed nothing in my code except switch the source to the external file (file is named differently).

I am really confused about this: Why should it be that the contents of the external file can be displayed but that this stops the selection? Indeed, I even tried running it with the external file declared but the contents of the list hard-coded ...

<xsl:template match="people/person" mode="fl">
  <option value="Pete">Pete</option>
</xsl:template>

... and this also didn't work.

This makes me think that the fact that I cam connecting external files is messing up my ability to select.

I'm going to try a few other variations and will report back again.


  #7 (permalink)  
Old September 27th, 2006, 03:36 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This is entirely an HTML problem rather than XML. What you need to do is show the HTML that is generated and diagnose the problem from there.
Some browsers don't react well to script added after the page has initially loaded, you can sometimes get around this by having the script element marked as DEFER. In your case I don't see why the JavaScript needs changing though if you're just populating a select via XML.

--

Joe (Microsoft MVP - XML)
  #8 (permalink)  
Old September 27th, 2006, 03:45 AM
Authorized User
 
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think I understand the problem now: my javascript can accept and process entries found in the main XML file (i.e. the XML file calling the XSL) but if (anywhere in the XSL) I declare an external source (document($myparam) or document(myfile.xml)), then the content of the external file will be displayed but the script stops working. I assume that data from external XMLs can only be retrieved once?

Or is there a method to 'requery' the whole thing.

Anyway, here is my complete code. It displays the list of files OK and displays the content of the default declared file (file2.xml) but will not flip to display the contents of file1.

If I remove the declaration 'document($srce)' (and all associated code) then the selection works fine but of course there is no data to display.

I do hope you can help me with this one?

-- filelist.xml --
<?xml-stylesheet href="data.xsl" type="text/xsl"?>
<?xml version="1.0" encoding="UTF-8"?>
<files>
  <file>file1.xml</file>
  <file>file2.xml</file>
</files>

-- file1.xml --
<?xml version="1.0" encoding="UTF-8"?>
<dataroot>
  <item>1A</item>
  <item>1B</item>
  <item>1C</item>
</dataroot>

-- file2.xml --
<?xml version="1.0" encoding="UTF-8"?>
<dataroot>
  <item>2A</item>
  <item>2B</item>
  <item>2C</item>
</dataroot>

-- data.xsl --
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="srce" select="'file2.xml'" />

<xsl:template match="/">

<html>
<head>

<script language="javascript" type="text/javascript">

function procss(srce){
try{
  var s = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
  var x = document.XMLDocument;
  if (x == null){
   x = navigator.XMLDocument;
   s.loadXML(navigator.XSLDocument.xml);
  }else{
   s.loadXML(document.XSLDocument.xml);
  }
  var tem = new ActiveXObject("MSXML2.XSLTemplate");
  tem.stylesheet = s;
  var proc = tem.createProcessor();
  proc.addParameter("srce", srce);
  proc.input = x;
  proc.transform();
  var str = proc.output;

  var newDoc = document.open("text/html");
  newDoc.write(str);
  navigator.XMLDocument = x;
  navigator.XSLDocument = s;
  newDoc.close();
 }
 catch(exception){
 }
}

</script>
</head>

<body>

   <form name="formfilt" id="formfilt">

     <label for="selsrce">Source</label><br />

     <select size="5" name="selsrce" id="selsrce">
       <xsl:apply-templates select="files" mode="fl">
       <xsl:sort order="descending"/>
       </xsl:apply-templates>
     </select>

    <input type="button" value="ausführen" onClick="procss(document.formfilt.selsrce.value)"/>
   </form>

<xsl:choose>
 <xsl:when test="$srce">

      <xsl:value-of select="$srce" />
      <table>
      <xsl:apply-templates select="document($srce)" mode="disp">
      </xsl:apply-templates>
     </table>

 </xsl:when>
</xsl:choose>

</body>
</html>

</xsl:template>

<xsl:template match="/dataroot/item" mode="disp">
  <tr><td>
  <xsl:value-of select="."/>
  </td></tr>
</xsl:template>

<xsl:template match="files/file" mode="fl">
  <option value="{.}"><xsl:value-of select="."/></option>
</xsl:template>

</xsl:stylesheet>


  #9 (permalink)  
Old September 27th, 2006, 04:03 AM
Authorized User
 
Join Date: Sep 2006
Location: Cologne, , Germany.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe,

> This is entirely an HTML problem rather than XML. What you need
> to do is show the HTML that is generated and diagnose the problem
> from there.
> Some browsers don't react well to script added after the page has
> initially loaded, you can sometimes get around this by having the #
> script element marked as DEFER. In your case I don't see why the
> JavaScript needs changing though if you're just populating a select
> via XML.

OK, I'll look into these points. Maybe the DEFER option will help me.

What would really help me at this point would be a little example where the XML file is switched by user action. This could be simply two buttons which switch (preferably using javascript) the contents of the XSL display from 'file1' to 'file2'.

If I could get my hands on this example, then it would open a lot of doors to me: I would be able to cut down my XML files into 'bite-size-chunks' and then the user could click and select the XML file that he/she wants to see.

I hope this is possible?

Many thanks.

Regards,
Alan.

  #10 (permalink)  
Old September 27th, 2006, 05:26 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm not sure what you mean by "selectable". Do you mean, selectable by the user when the resulting HTML is displayed in a browser? In that case, you're generating the wrong HTML and we need to know what wrong HTML you are generating. You need to study the "source" of the generated HTML, it's not enough to see how it behaves in a browser.

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
parameterized if-then not working hoqenishy XSLT 1 April 17th, 2008 01:25 PM
Parameterized Property surendraparashar C# 2005 4 October 17th, 2007 11:33 PM
Parameterized Query asters VB.NET 2002/2003 Basics 5 July 9th, 2007 12:08 PM
Parameterized SQLDataAdapter boyshey VB.NET 2002/2003 Basics 1 April 14th, 2006 07:08 AM
Parameterized OracleDataCommand narendra_patil BOOK: Beginning ASP.NET 1.0 0 April 21st, 2005 05:17 AM





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