0

I'm trying to produce a search filter that will work with an existing XML data file and I'm starting with trying to display just unique values from one of the nodes in a drop down list box.

An example of the XML file can be seen at... http://kirk.rapweb.co.uk/testing/filter/tidy/plain/products.xml

I've managed to use XSLT to display the XML in a more readable format... http://kirk.rapweb.co.uk/testing/filter/tidy/products.xml

The same data with filter applied... http://kirk.rapweb.co.uk/testing/filter/tidy/filter/products.xml

The values I would like to list in the 1st drop down list box filtered out... http://kirk.rapweb.co.uk/testing/filter/tidy/distinct/products.xml

All the data pulled into a HTML page... http://kirk.rapweb.co.uk/testing/filter/tidy/html/

I'm struggling to work out how to get the data from... http://kirk.rapweb.co.uk/testing/filter/tidy/distinct/products.xml into a HTML page drop down list box.

Can anyone offer advice, point me in the right direction or confirm that I'm on the right track?

The end result that I'm trying to achieve is to have 2 drop down boxes.

Drop Down List 1 would contain... Brakes, Exhaust, Lighting

Drop Down List 2 would contain...

  • If Brakes were selected previously... Discs and Drums, Pads and Shoes.
  • If Exhaust were selected previously... Centre, Rear.
  • If Lighting were selected previously... Headlamps, Rear Lights.

Right now I'd just like to focus on populating Drop Down Box 1 with the data discussed above.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
user1503689
  • 169
  • 1
  • 3
  • 12

2 Answers2

1

With your updated decription it looks like you want to create a dropdown with the distinct group values. This is an example of a grouping problem and in XSLT 1.0, the way to do this is with a technique called Muenchian grouping. It goes like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:key name="kGroup" match="product" use="group"/>

  <xsl:template match="/">
    <select name="productGroup" id="productGroup">
      <xsl:apply-templates 
        select="dataroot/product[generate-id() = 
                                 generate-id(key('kGroup', group)[1])]" />
    </select>
  </xsl:template>

  <xsl:template match="product">
    <option value="{group}">
      <xsl:value-of select="group"/>
    </option>
  </xsl:template>
</xsl:stylesheet>

When run on your input XML, it produces this:

<select name="productGroup" id="productGroup">
  <option value="Brakes">Brakes</option>
  <option value="Exhaust">Exhaust</option>
  <option value="Lighting">Lighting</option>
</select>

Then you would use the same JavaScript as you're using now to put the result inside a particular element in the HTML DOM that can be easily found by its ID.

Now as for the next step, once the above is working, you would put a JavaScript event on this dropdown so that it runs another transform when an item is selected. You would get this selected value and then you can pass this into yet another XSLT as a parameter value. This page has good information on passing parameters into an XSLT in JavaScript. The XSLT would look like this (yet again with Muenchian grouping):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" omit-xml-declaration="yes"/>
  <xsl:key name="kType" match="product" use="type"/>

  <xsl:param name="group" />

  <xsl:template match="/">
    <select name="productType" id="productType">
      <xsl:apply-templates 
        select="dataroot/product[generate-id() = 
                     generate-id(key('kType', type)[1])][group = $group]" />
    </select>
  </xsl:template>

  <xsl:template match="product">
    <option value="{type}">
      <xsl:value-of select="type"/>
    </option>
  </xsl:template>
</xsl:stylesheet>

When the parameter value "Lighting" is passed in as the group parameter and this is run on your input XML, this produces:

<select name="productType" id="productType">
  <option value="Headlamps">Headlamps</option>
  <option value="Rear Lights">Rear Lights</option>
</select>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Thanks for your input... I've edited my post (waiting approval) to remove the URL showing the data filtered down to just brakes, as this seems to be causing confusion. I've modified my question to include additional information. – user1503689 Feb 08 '13 at 15:22
  • Thanks for your quick response. I've done a brief test and all seems to be working as expected, so many thanks... I really appreciate it! I will need some more time to study your code and gain a better understanding of what is going on. Unfortunately, I'm going away for the weekend now, so will have to pick this up Monday morning. I'll let you know how I get on. – user1503689 Feb 08 '13 at 16:10
  • I now understand the Muenchian Method, thanks to http://www.jenitennison.com/xslt/grouping/muenchian.html. I used JS to populate HTML Doc Drop Down, based on XML and XSL document. http://kirk.rapweb.co.uk/testing/filter/tidy/new/index.htm. I've added onchange event to Drop Down and printed the selection to ensure I'm storing the right value. The same onchange function calls the function from the page you mentioned. I modified the set param section, to docProcessor.setParameter(storeProductGroupValue); removing the For Loop, as I should only have 1 param to pass in. No luck with Drop Down 2. :( – user1503689 Feb 11 '13 at 12:06
  • Walking the JS in the debugger, it looks like your `transformXml()` function has parameters named `xmlURL` and `xslURL`, but the variable names used in the actual code (in both the first `try{}` block and the second) are `xmlDocURL` and `xslDocURL`. That's definitely one issue. Could you remedy that? – JLRishe Feb 11 '13 at 12:41
  • Also, you need to specify the parameter name when adding parameters: `docProcessor.addParameter("group", productGroupValue);` and `docProcessor.setParameter(null, "group", productGroupValue);` – JLRishe Feb 11 '13 at 12:43
  • Many thanks for you help... That is working a treat! I thought I'd probably trimmed a bit too much out when changing the Set Parameter section. Apologies for not spotting the xmlURL/xslURL and xmlDocURL/xslDocURL discrepancies. Need to spend a bit more time learning what's going on in the Passing Parameters script. I've not quite got my head around stepping through JS code in FireBug... Would you suggest a better tool for debugging JS? – user1503689 Feb 11 '13 at 13:14
  • I've just noticed it works fine in Chrome and Safari... http://kirk.rapweb.co.uk/testing/filter/tidy/final/ Mozilla doesn't seem to like it though. Not yet tested in IE. In the Passing Parameters Script, would Chrome and Safari not come under the Mozilla code, as apposed to IE? Any ideas what's up with Mozilla? v18.0.2 on Mac OS. – user1503689 Feb 11 '13 at 13:27
  • It looks like it's failing in Mozilla because you still have this line hanging around: `docProcessor.setParameter(productGroupValue);`. I believe Chrome, Safari, and Mozilla would use the same code, but maybe the first two are more forgiving. I was actually debugging the page in IE Developer Tools because I spend most of my time in IE, but I think that and Firebug are about equally good. I haven't tried any other web debuggers. – JLRishe Feb 11 '13 at 13:33
  • I've been a bit lazy with Firebug... Will read through http://www.developerfusion.com/article/139949/debugging-javascript-with-firebug/ and make sure I debug my scripts in future. – user1503689 Feb 11 '13 at 13:35
  • Walking the code in Firebug allows finding this message in `e` when the exception is caught: _"Not enough arguments [nsIXSLTProcessor.setParameter]"_, which is precisely the issue. – JLRishe Feb 11 '13 at 13:38
  • Mozilla is fixed now. Thanks again. Rushing too much and missed the duplicate line... Poor excuse I know. I should be able to complete my project now. Really appreciate the assistance! – user1503689 Feb 11 '13 at 13:38
  • Glad to help. I hope the rest of your project goes well. :) – JLRishe Feb 11 '13 at 13:39
1

I guess you are under the assumption that you do not know how many different 'groups' there are going to be in the data (I mean, someone could add a new group anytime, no?).

One of the standard techniques in XSLT 1.0 for grouping elements is 'Muenchian Grouping' (XSLT 2.0 implements native grouping functions and elements) which is based in comparing unique ids.

In the following solution I assume that the data from the XML document is not already grouped.

<xsl:key name="group-key"
         match="product"
         use="group" />

<xsl:template match="dataroot"> 
    <select>
        <xsl:for-each select="product[generate-id(.) = generate-id(key('group-key', group)[1])]">
            <!-- We sort the group names alphabetically. If the names of the groups are already ordered alphabetically, the xsl:sort can be omitted. -->
            <xsl:sort select="group" />
            <option><xsl:value-of select="group" /></option>
        </xsl:for-each>
    </select>
</xsl:template>

The result of applying this template (adding the xsl:stylesheet element) to your original 'product.xml' is:

<select>
    <option>Brakes</option>
    <option>Exhaust</option>
    <option>Lighting</option>
</select>

Note: even if we remove the 'xsl:sort' element, the output is going to be the same because the data is already ordered in 'product.xml'.

Pablo Pozo
  • 1,880
  • 13
  • 9
  • Thanks for your quick response. I've done a brief test and all seems to be working as expected, so many thanks... I really appreciate it! As JLRishe has provided information regarding my next step, I will continue working from that answer. Unfortunately, I'm going away for the weekend now, so will have to pick this up Monday morning. I'll let you know how I get on. – user1503689 Feb 08 '13 at 16:15