 |
| 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
|
|
|
|

June 9th, 2008, 02:48 AM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
help to create dynamic listbox using xslt & JS
Hi
I need help to create a listbox based on the input whatever i have set the value for the parameter.
test.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<xsl:param name="Ref"></xsl:param>
<xsl:param name="Cat"> </xsl:param>
<xsl:param name="Pro"></xsl:param>
<xsl:variable name="k" select="$Pro" />
<xsl:template match="/">
<select name="test" size="40" multiple="true" style="width:100%">
<xsl:for-each select="Documents/Doc">
<xsl:sort select="@PN" order="ascending" />
<option value="{$k}"><xsl:value-of select="$k" /></option>
</xsl:for-each>
</select>
</xsl:template>
</xsl:stylesheet>
test.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <Documents>
<Doc Ref="6BEED03539C1B2D98525707C0068EA9E0000" Cat="Other HPC" PN="PRION" />
<Doc Ref="1FFDA27D49323254C1256FF500454DCA0000" Cat="Beverages" PN="AMAZONIA_L" />
<Doc Ref="490FD757DC09E331C1256F72003782E80000" Cat="Beverages" PN="PAPARAZZI_EU_RU" />
<Doc Ref="6A3C4EE7307F9B5C802570380059FD3F0000" Cat="Beverages" PN="NIRVANA_ROLLOUT GB_EU" />
<Doc Ref="8BE62F073CF99969C1257073004FFCDB0000" Cat="Beverages" PN="NIRVANA APHRODITE_EU_FR" />
<Doc Ref="AB8DD8B7F97C8266C1256E230057B1E50000" Cat="Beverages" PN="GLD_SAINT2-EU" />
</Documents>
Javascript:
var xslt = new ActiveXObject("Msxml2.XSLTemplate");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
var xslProc;
xslDoc.async = false;
xslDoc.resolveExternals = false;
//xslDoc.load(view+".xsl");
xslDoc.load("dynamic_cdcatalog.xsl");
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
//xmlDoc.load(product+".xml");
xmlDoc.load("vwOTIFXML.xml");
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.addParameter("Cat","@Cat");
xslProc.addParameter("Pro","@PN");
//for (var i=0; i<params.length; i++) {
// xslProc.addParameter(params[i].split("=")[0],params[i].split("=")[1]);
//}
xslProc.transform();
document.write(xslProc.output);
The above are the files i have used in my thing.
My problem is whatever i passing or assigning a value for variable or param, it will be the options of the listbox.
I want to generate the dynamic listbox based on my values whatever i m passing the values.
Thanks & Regards
Ramachandran K
Thanks & Regards
Ramachandran K
|
|

June 9th, 2008, 03:10 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
In general you can only pass a value as a parameter, not an expression to be evaluated to yield a value. Some XSLT processors have extensions (saxon:evaluate(), dyn:evaluate()) that allow you to evaluate an XPath expression passed as a string, but you appear to be using MSMXL and that doesn't. You could call out to JavaScript to do it, but that's a bit messy. If the expression is always in the form "@xxxx" then you could pass "xxxx" as the parameter value and your stylesheet could do <option value="{@*[name()=$Pro]}"><xsl:value-of select="@*[name()=$Pro]" /></option>.
Another option is to modify the stylesheet before you compile it, so that you edit the XPath expression into the stylesheet source code. You can do this modification either using DOM interfaces or using XSLT.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

June 9th, 2008, 04:12 AM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael
Great. wonderful. you have given me the thing whatever i expected.
I got the exact output.
I have another doubt. i want to pass the value for "for-each select".Please refer the below code. How i have to do that.
value for XXXXXXX may be like
"/Document/Doc" or "/Documents/Doc[@PN="test"]"
<xsl:for-each select="XXXXXXXXX">
<xsl:sort select="@*[name()=$Pro]" order="ascending" />
<option value="{@*[name()=$Pro]}"><xsl:value-of select="@*[name()=$Pro]" /></option>
</xsl:for-each>
I m in need of your valueable reply.
Thanks & Regards
Ramachandran K
Thanks & Regards
Ramachandran K
|
|

June 9th, 2008, 05:14 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
If you want an XPath expression in your stylesheet to be completely dynamic (supplied as a string), your options are:
(a) use a vendor-provide extension such as saxon:evaluate()
(b) write your own such extension (not actually too difficult with MSXML)
(c) modify the stylesheet before use
As discussed in my previous reply.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

June 10th, 2008, 01:49 AM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Micheal
please guide me how to create a listbox with unique values.
"/Documents/Doc[@PN="test"]".
I want to use the unique code in the above xpath expression.After that i want use that expression in the for-each select ....
<xsl:for-each select="XXXXXXXXX">
<xsl:sort select="@*[name()=$Pro]" order="ascending" />
<option value="{@*[name()=$Pro]}"><xsl:value-of select="@*[name()=$Pro]" /></option>
</xsl:for-each>
Thanks & Regards
Ramachandran K
|
|

June 10th, 2008, 02:59 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>Hi Micheal
Please don't address your questions to specific indivuduals. (And if you do, please spell their names correctly)
>please guide me how to create a listbox with unique values.
Please indicate the inputs and desired outputs of your transformation.
Your question could mean a number of things. Perhaps you want to eliminate duplicates appearing in the input? Or perhaps you want to generate unique (but arbitrary) values in the output? Many people find it difficult to specify their requirements clearly, which is why we ask for samples of input and output - it often makes the problem far clearer.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

June 10th, 2008, 04:27 AM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael,
Sorry for misspelled ur name.
The Below is the input xml file. based on some condition like
If PN = "AMAZONIA_L" means then i want to get Unique Cat values from xml document.
here
Cat values are Other HPC,Beverages. In this i want to get Beverages.
How to perform.Help me.
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <Documents>
<Doc Ref="6BEED03539C1B2D98525707C0068EA9E0000" Cat="Other HPC" PN="PRION" />
<Doc Ref="1FFDA27D49323254C1256FF500454DCA0000" Cat="Beverages" PN="AMAZONIA_L" />
<Doc Ref="490FD757DC09E331C1256F72003782E80000" Cat="Beverages" PN="PAPARAZZI_EU_RU" />
<Doc Ref="6A3C4EE7307F9B5C802570380059FD3F0000" Cat="Beverages" PN="NIRVANA_ROLLOUT GB_EU" />
<Doc Ref="8BE62F073CF99969C1257073004FFCDB0000" Cat="Beverages" PN="NIRVANA APHRODITE_EU_FR" />
<Doc Ref="AB8DD8B7F97C8266C1256E230057B1E50000" Cat="Beverages" PN="GLD_SAINT2-EU" />
</Documents>
Thanks & Regards
Ramachandran K
|
|

June 10th, 2008, 05:00 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Assuming your context node is the document root, you can select all the elements with category="Beverages" using
/Documents/Doc[@Cat='Beverages']
If you don't know in advance what all the possible categories are, then your problem is described as "grouping". In XSLT 2.0, use the xsl:for-each-group instruction. If you have to use XSLT 1.0 then it is more difficult, use Muenchian grouping (which you can find in a textbook or from google).
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |