sikrip Posted October 11, 2006 Share Posted October 11, 2006 Hi everyone,I have a xml file and I want to filter the nodes to be shown in the final result of my web page.For example when I want to view only the nodes that hav price equal to 10 i write the following. <xsl:for-each select="catalog/cd"> <xsl:if test="price=10"> ... </xsl:if></xsl:for-each> But i want to make a page that the user will type the price value in a text field and this value will be passed in the 'test' section above. In other words I want to put a variable in the xml:if.Can you please give me some sample code of how to do that? Must I use javascript?I am in my very first steps in xstl, sorry if my question is trivial...Thanks in advance... Link to comment Share on other sites More sharing options...
aalbetski Posted October 11, 2006 Share Posted October 11, 2006 You can solicit the user input in whatever fashion you desire, a winform, webform, etc.. You would pass that information into the stylesheet as a parameter. I have created a complete working example using javascript and XMLDOM (with IE6.0). You will need to modify based on your enviroment (.NET, ASP, etc..)Sample XML (price.xml) <catalog> <cd> <name>CD1</name> <price>10</price> </cd> <cd> <name>CD2</name> <price>10</price> </cd> <cd> <name>CD3</name> <price>15</price> </cd> <cd> <name>CD4</name> <price>15</price> </cd> <cd> <name>CD5</name> <price>12</price> </cd></catalog> Sample XSL (price.xsl) <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:param name="price" /> <xsl:template match="/"> <xsl:for-each select="catalog/cd"> <xsl:if test="price = $price"> <xsl:value-of select="name" /><br/> </xsl:if> </xsl:for-each> </xsl:template></xsl:stylesheet> HTML Page that performs transform. Note the value of '10' being passed into the TransformXML() function. This is where you would pass the value to the transformer. How it gets there is up to you.price.html <html> <body onLoad="InitPage()"> <div id="display"></div> </body> <script> function InitPage() { document.getElementById("display").innerHTML = TransformXML(10) } function TransformXML(price) { var xmlSource = new ActiveXObject("Msxml2.DOMDocument.4.0") var xslStyle = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0") xmlSource.async = false xslStyle.async = false xmlSource.load("price.xml") xslStyle.load("price.xsl") xslStyle.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'") var objTransformer = new ActiveXObject("Msxml2.XSLTemplate.4.0") objTransformer.stylesheet = xslStyle.documentElement var objProcessor = objTransformer.createProcessor() objProcessor.addParameter("price",price,"") objProcessor.input = xmlSource objProcessor.transform() return objProcessor.output } </script></html> Link to comment Share on other sites More sharing options...
boen_robot Posted October 11, 2006 Share Posted October 11, 2006 The question is indeed trivial, but scince it's not descibed in the tutorial when it should be, I understand you and thus, will assist you .You must use the <xsl:param> element as a top-level element. In other words: <xsl:stylesheet version="1.0" ...><xsl:param name="price"/>...<xsl:template match="/"><xsl:for-each select="catalog/cd"> <xsl:if test="price=$price"> ... </xsl:if></xsl:for-each></xsl:template>...</xsl:stylesheet> When the user fills out the form, it will most probably be sent to a server side scripting page, right? So, in that case, you can execute the transformation with that language and return the output. All processors can also pass parameters to the stylesheet to influence the output. For example, in PHP5 you can assign a parameter to the XSLT processor with something like: $processor->setParameter(NULL, "price", $_POST['user_entered_data']); The complete code of course is longer, but this is the parameter line.Depending on what language you use to execute the transformation, the ways vary. The only way to set up a parameter without a server side script is with JavaScript, but JavaScript will only work if the form is on the same page as the results. This post shows a script that can change a parameter of the XSLT that is currently in use.[edit] Whoa. That's the first time I've been beaten up in this section. Today is a very weird day. Only new things happening . And... is time running faster then usual or am I just sleepy?[/edit] Link to comment Share on other sites More sharing options...
sikrip Posted October 11, 2006 Author Share Posted October 11, 2006 Thanks a lot both of you. I am not able to use server side scripting because the web server that will host my site does not support it.I hope to make it work with javascript...! Link to comment Share on other sites More sharing options...
sikrip Posted October 12, 2006 Author Share Posted October 12, 2006 Thanks a lot both of you. I am not able to use server side scripting because the web server that will host my site does not support it.I hope to make it work with javascript...!Well I was not able to make it work...I did the following:I declared a xsl param <xsl:param name="pagenumber" select="1"/> When the page loads I see the first record as expected. The goal is to press next and see the next record!!I copied the javascript function from the link you posted above in a page.js file and put the <script language="javascript" src="page.js" /> in the head of the page.Finally I called the function from a button like this <input type="button" value="Next" onclick="changePage(2)" /> When I press the buuton (and expect to see the second record) nothing hapens.I added the document.write("error occured number="+number) in the catch statement of the js function and then when I click the button i see "error occured number=2 "in my browser. I tried it in both MS IExplorer and firefox. So, the function is called, the parameter is passed, but an exception is thrown...(So the javascript code has some problem?)Can you help me by sending me a simple xstl page that does the work i want to do:to display one xml record at time using a parameter(using client side scripting).PSI am coding desktop applications for many years but I am newbie in web development, and -for now- I have not the required time to propertly study web development, I just have to implement this simple(?) task so please help me... Link to comment Share on other sites More sharing options...
boen_robot Posted October 12, 2006 Share Posted October 12, 2006 I think the function is designed to work from a link. I'm not good at JS, so I'm just guessing. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now