Jump to content
Sign in to follow this  
Brett_UK_coder

XSLT Combo / drop down box

Recommended Posts

Hi there, Im new to coding and have found this website extremly usefulI have an xml file, which is the CD catalog xml used in the examples.I have an xsl file, which originally came from another one of the examples, but it used a table to output the data into. I am trying to amend this so that instead of a table, a combo box appears, which when selected is populated with all the artists names from the corresponding xml file.This is the XML file:

<?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><catalog>	<cd>		<title>Empire Burlesque</title>		<artist>Bob Dylan</artist>		<country>USA</country>		<company>Columbia</company>		<price>10.90</price>		<year>1985</year>	</cd>	<cd>		<title>Hide your heart</title>		<artist>Bonnie Tyler</artist>		<country>UK</country>		<company>CBS Records</company>		<price>9.90</price>		<year>1988</year>	</cd>	<cd>		<title>Greatest Hits</title>		<artist>Dolly Parton</artist>		<country>USA</country>		<company>RCA</company>		<price>9.90</price>		<year>1982</year>	</cd>	<cd>		<title>Still got the blues</title>		<artist>Gary Moore</artist>		<country>UK</country>		<company>Virgin records</company>		<price>10.20</price>		<year>1990</year>	</cd>

This is the html file, which I dont believe is causing the problem. Its just taken from the example which transforms the XML file to XHTML on the client

<html><head><script>function loadXMLDoc(dname){if (window.XMLHttpRequest)  {  xhttp=new XMLHttpRequest();  }else  {  xhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xhttp.open("GET",dname,false);xhttp.send("");return xhttp.responseXML;}function displayResult(){xml=loadXMLDoc("catalog.xml");xsl=loadXMLDoc("catalog.xsl");// code for IEif (window.ActiveXObject)  {  ex=xml.transformNode(xsl);  document.getElementById("example").innerHTML=ex;  }// code for Mozilla, Firefox, Opera, etc.else if (document.implementation && document.implementation.createDocument)  {  xsltProcessor=new XSLTProcessor();  xsltProcessor.importStylesheet(xsl);  resultDocument = xsltProcessor.transformToFragment(xml,document);  document.getElementById("example").appendChild(resultDocument);  }}</script></head><body onload="displayResult()"><div id="example" /></body></html>

In an attempt to get this working I have two variations of the XSL file, this one only populates it with one selection, which is the first 'Bob Dylan':

<?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/catalog">  <html>  <body>  <h2>My CD Collection</h2><select><xsl:value-of select="cd/artist"/></select>  </body>  </html></xsl:template></xsl:stylesheet>

This is one I adapted, from the first, which I thought would finally work, but it doesn't :-(, instead all the heading and combo box elements dissappear

<?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/catalog">  <html>  <body>  <h2>My CD Collection</h2><xsl:for-each select="catalog/cd"/><select><xsl:value-of select="artist"/></xsl:for-each></select>  </body>  </html></xsl:template></xsl:stylesheet>

How do I get the drop down to populate all the artist names from the xml using the xsl, any help would be greatly appreciated - Thank youBrett

Share this post


Link to post
Share on other sites

All artists per CD, or artists on all CDs?If on all CDs:

<select><xsl:for-each select="catalog/cd"><option><xsl:value-of select="artist"/></option></xsl:for-each></select>

Which is very close to what you had, only it's actually well formed, and produces an <option/> element for every artist.

Share this post


Link to post
Share on other sites
All artists per CD, or artists on all CDs?If on all CDs:
<select><xsl:for-each select="catalog/cd"><option><xsl:value-of select="artist"/></option></xsl:for-each></select>

Which is very close to what you had, only it's actually well formed, and produces an <option/> element for every artist.

Thank you, Thank you, Thank you, Thank you.I had tried to add the option tag throughout my attempts and it wouldnt work then either, but I pasted your code and in addition removed the catalog from the below line:from: <xsl:template match="/catalog">To: <xsl:template match="/">And those 2 things combined worked. Really Really grateful. I have 5 days off of work, which I am using for some intense code learning and you've just prevented me from stewing over one thing the whole time.CheersBrett

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...