XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 27th, 2004, 05:14 AM
1ei1a 1ei1a is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 32 1ei1a User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 13 sec
Reputation Power: 5
javascript/xslt/xml/ Xpath issues!

Hi folks,

Could anyone help out with the following problem please.

I want to use javascript to retrieve the querystring off a url, and then, using that querystring value, I want to set an xsl:variable (or xslarameter) to select the correct node in my xml document.

For example:
my xml document contains a list of news items, sorted by a primary key:
<news>
<item>
<primarykey>1</primarykey>
<title>Title One</title>
<body>body of article</body>
</item>
<item>
<primarykey>2</primarykey>
<title>Title Two</title>
<body>body of article</body>
</item>
.
.
.
</news>

My url looks like this:
http://www.somesite.com?newsItem=2

My javascript should strip the url and retrieve the querystring, specifically it must retrieve '2' as this must later be used by Xpath and xsl to select and display the correct node from my xml document.

At the moment I don't know how to pass the javascript variable (in this case '2') to the xsl. How is this accomplished??

Also, I don't understand what the difference between xslaram and xsl:varable is? As I want to use the abovementioned javascript querystring variable in variious places, I guess I should be using xslaram?

Many thanks for any help!
Leila

Reply With Quote
  #2  
Old July 28th, 2004, 02:34 AM
kid23 kid23 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 62 kid23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I'm afraid I have bad news for you.. you're not the first one to ask for that kind of interaction between javascript & xslt. I was looking forward to achieving something similar in the past, but I never could.

Here's why it can't work:

you agree with me that XML + XSLT = HTML, right ?

Now, in a standard HTML file, when would your javascript be executed ? At run-time.

The thing is that during the transformation, you javascript is just a piece of text, and IS NOT PROCESSED by xslt, so there's no way. The transformation happens before the execution of the page.

Sorry for the bad news, but at least you know

Reply With Quote
  #3  
Old July 28th, 2004, 07:00 AM
1ei1a 1ei1a is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 32 1ei1a User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 13 sec
Reputation Power: 5
Quote:
Originally Posted by kid23
I'm afraid I have bad news for you.. you're not the first one to ask for that kind of interaction between javascript & xslt. I was looking forward to achieving something similar in the past, but I never could.

Here's why it can't work:

you agree with me that XML + XSLT = HTML, right ?

Now, in a standard HTML file, when would your javascript be executed ? At run-time.

The thing is that during the transformation, you javascript is just a piece of text, and IS NOT PROCESSED by xslt, so there's no way. The transformation happens before the execution of the page.

Sorry for the bad news, but at least you know


Hi there,

Many thanks for your useful comments.

What would be the best way to solve this problem then? How did you manage to get around these constaints?

Reply With Quote
  #4  
Old July 28th, 2004, 03:56 PM
kid23 kid23 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 62 kid23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I see two ways of doing it:

1) You transform the whole file, and put everything into DIV tags which are hidden, and by javascript, you get your value from the URL and set the appropriate DIV tag to visible.

Of course, if you have to select from a large amount of possible values, then this is not the most acceptable version.. I'd only recommend it if you had say 4-5 values to choose from.

2) This second alternative is probably not the most flexible, but it works for sure, as I'm using it on my site. It works with 3 files:

- A very small XML file containing your key, and a basic structure to build your page
- The XSL file
- Another XML file which acts as your database

Practically, your users are going to click on a link to "newsItem2.xml", which contains your key and the main XML tag that will be transformed by XSL.
The xsl:template will then use the document() function to query the right data from the XML database.

The only big problem I have with this technique is that you have to have a intermediate file for every possible key value.

I've been looking for a way to avoid using these files, which I think could be achieved with xpointers (my URL's would look like "mydatabase.xml#xpointer(//mydata/key25)" ) but unfortunately, this doesn't seem to be supported correctly by any browser.

A quick example:

xml file containing the key:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet href="yourfile.xsl" type="text/xsl"?>
<AnyTag>
  <SelectedKey>
    <KeyValue>2</KeyValue>
  </SelectedKey>
</AnyTag>


xsl template:
Code:
<xsl:template match="SelectedKey">
  <xsl:variable name="mykey">
    <xsl:value-of select="KeyValue" />
  </xsl:variable>

  <xsl:apply-templates select="document('yourdb.xml')//item[primarykey=$mykey]" />
</xsl:template>


As I said, this works, and is acceptable if you have a few predefined key values to play with, but if you have many of them, or need much greater flexibility, I'm afraid we'll both have to wait until better support for xpointers is provided

Hope this helps.. as unfortunately, the only way to work around your original problem is to completely rethink the logic, which is what I've done here.

Reply With Quote
  #5  
Old July 30th, 2004, 05:26 AM
1ei1a 1ei1a is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 32 1ei1a User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 13 sec
Reputation Power: 5
Thumbs up

Hi there,

Thank you ever so much for taking the tme to give me your solutions. Your first idea about using divs gave me the solution I needed to solve my problem. THANKS

Leila

Reply With Quote
  #6  
Old August 18th, 2004, 11:38 AM
rguilbault rguilbault is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 30 rguilbault User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 18 sec
Reputation Power: 5
Another take

A method I used for a similar problem was to break the information into separate xml files organized in directories, such as:

20040817/
+--> 1.xml
+--> 2.xml
+--> 3.xml

20040818/
+--> 1.xml
+--> 2.xml

etc.

my urls look as such:

sample.htm?dir=20040817&article=1

In the document, I can peice together the (relative) url with javascript as such:

Code:
if (location.search) {
  var aSearch = location.search.slice(1).split("&");

  for (var i = 0; i < aSearch.length; i++) {
    var ind = aSearch[i].split("=");
    aParams[ind[0].toLowerCase()] = ind[1];
  }
}

var uri = aParams["dir"]+"/"+aParams["article"]+".xml";
...


I'm using the browser's xslt processor objects to load the contents into the page...although I suppose I could skip that by redirecting to the xml file (with an embedded xml-stylesheet directive)...hmm...

Advantages:
Scalable: Just add any additional files you want to a directory and they're ready to access
Efficiency: The xml files are smaller and you force the user to download only the content they care about, rather than downloading a large file and selecting 1 node out of it. This also reduces the complexity of selecting the proper node by the XSLT processor (though I suppose one could certainly argue this would be negligible in most cases).

Disadvantages:
Flexibility: This is a relatively flat design, because it assumes only 1 level of directory and limits to subdirectories of the current node (this could be addressed with a more robust javascript solution)

In addition, if you wanted to display more than just 1 'article', it would be more painful with everything separated in their own files than if they were all contained in a single file. For my purposes, this was not an issue, as I would only care to view 1 'article' at a time.

I don't know if this would provide any benefits to either of you, but it's working pretty decent for me.

Quote:
Originally Posted by kid23
I see two ways of doing it:

1) You transform the whole file, and put everything into DIV tags which are hidden, and by javascript, you get your value from the URL and set the appropriate DIV tag to visible.

Of course, if you have to select from a large amount of possible values, then this is not the most acceptable version.. I'd only recommend it if you had say 4-5 values to choose from.

2) This second alternative is probably not the most flexible, but it works for sure, as I'm using it on my site. It works with 3 files:

- A very small XML file containing your key, and a basic structure to build your page
- The XSL file
- Another XML file which acts as your database

...

As I said, this works, and is acceptable if you have a few predefined key values to play with, but if you have many of them, or need much greater flexibility, I'm afraid we'll both have to wait until better support for xpointers is provided

Hope this helps.. as unfortunately, the only way to work around your original problem is to completely rethink the logic, which is what I've done here.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > javascript/xslt/xml/ Xpath issues!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT