|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 xsl arameter) 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 xsl aram and xsl:varable is? As I want to use the abovementioned javascript querystring variable in variious places, I guess I should be using xsl aram? Many thanks for any help! Leila ![]() |
|
#2
|
|||
|
|||
|
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 ![]() |
|
#3
|
|||
|
|||
|
Quote:
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? |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > javascript/xslt/xml/ Xpath issues! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|