[xsl] Building a HTML select list from XML nodes

Subject: [xsl] Building a HTML select list from XML nodes
From: "Moore, Simon" <Simon.Moore@xxxxxxxxxx>
Date: Tue, 26 Nov 2002 14:43:18 +0100
	
> Hi
> 
> Another New recruit... :-)     I would be grateful for any help on this as it is my first real XSL project.
> 
> I am trying to build a list of all the unique nodenames available in an xml file and present it as a selectable drop down list in an HTML page.
> I am using IE 5.5 and MSXML on Windows 95.  At the moment I have all the files saved on my windows desktop so everything is local.
> My problem is that when I open the HTML page, the surrounding text and select box apprear, but there is nothing to select in the
> box.  If I change the output method to text I can see that my newline has no effect at all.  The listbox OPTION statements appear as one
> long line.  If I put a <BR /> tag in the loop and comment out the FORM and SELECT tags then it will print the text in the same way as if I had 
> typed the HTML by hand - if I paste it into a file and test it in the browser as a fixed page it all works perfectly.
> 
> I searched on the Web and found that preserveWhiteSpace has some effect on spacing but, I'm not so sure that this is the solution. I just can't
> get it to work. I'm sure that I am missing something simple.
> 
> I have included a sample XML plus my XSL and HTML page.  The XSL, HTML/JavaScript have been patched together from different sources (web/books).
> 
> Many thanks
> Simon.
> 
> --------- START XML --------
> <?xml version="1.0" encoding="UTF-8"?>
> <?xml-stylesheet type="text/xsl" href="./list_test.xsl"?>
> <computer-list>
> <computer>
> <name>fred</name>
> <project>fasttrack</project>
> <architecture>w32-ix86</architecture>
> <status>UP</status>
> 	<diskdrives>
> 		<drive>A</drive>
> 		<drive>B</drive>
> 		<drive>C</drive>
> 	</diskdrives>
> </computer>
> 
> .... more computers are in the list ....
> 
> </computer-list>
> 
> --------- END XML --------
> 
> 
> --------- START XSL --------
> 
> <?xml version="1.0" ?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
> 
> <xsl:output method="html"/>
> 
> <xsl:variable name="newline">
> <xsl:text>
> </xsl:text>
> </xsl:variable>
> 
> <xsl:variable name="testtext">
> <xsl:text>
> This is a test of the variables and it works when selected later in the XSL.
> </xsl:text>
> </xsl:variable>
> 
> <xsl:key name="elements" match="*" use="name()" />
> <xsl:template match="/">
> <HTML>
> <HEAD><TITLE>
> <xsl:text>Maclaren - Select The elements required</xsl:text>
> </TITLE></HEAD>
> <BODY>
> <H1>
> <xsl:text>Summary of Elements</xsl:text>
> </H1>
> <BR />
> <xsl:value-of select="$testtext"/>
> <BR />
> <FORM>
> <SELECT NAME = "eltype">
> <xsl:for-each select="//*[generate-id(.)=generate-id(key('elements',name())[1])]">
>         <xsl:sort select="name()" />
>         <xsl:for-each select="key('elements', name())">
>                 <xsl:if test="position()=1">
> 
>                         <xsl:text>&lt;OPTION VALUE = &quot;</xsl:text>
>                         <xsl:value-of select="name()" />
>                         <xsl:text>&quot;&gt;</xsl:text>
>                         <xsl:value-of select="name()" />
>                         <xsl:value-of select="$newline"/>
> 
> 	      <!-- If I put BR/ here the text prints the correct HTML text -->
> 	</xsl:if>
>         </xsl:for-each>
> </xsl:for-each>
> </SELECT>
> </FORM>
> <BR />
> <xsl:text>there should be a newline after this but it doesn't work...</xsl:text>
> <xsl:value-of select="$newline"/>
> <xsl:text>This line just gets concatenated to the previous line.</xsl:text>
> </BODY>
> </HTML>
> </xsl:template>
> </xsl:stylesheet>
> 
> 
> --------- END XSL  --------
> 
> 
> 
> --------- START HTML/JAVASCRIPT --------
> <html>
> <head>
> <title>
> XML test !!
> </title>
> </head>
> 	<body>
> 
> 		<SCRIPT LANGUAGE="JavaScript">
> 			
> 			// Load XSL
> 			var objXSLT = new ActiveXObject("MSXML2.FreeThreadedDomDocument");
> 			objXSLT.async = false;
> 			objXSLT.load("list_test.xsl");
> 
> 			// create a compiled XSL-object
> 			var objCompiled = new ActiveXObject("MSXML2.XSLTemplate");> 
> 			objCompiled.stylesheet = objXSLT.documentElement;
> 				
> 			// create XSL-processor
> 			var objXSLProc = objCompiled.createProcessor();
> 
> 			// Load XML
> 			var objXML = new ActiveXObject("MSXML2.FreeThreadedDomDocument");
> 			objXML.preserveWhiteSpace = true;
> 			objXML.async = false;
> 			objXML.load("tmp.xml");
> 
> 			// input for XSL-processor
> 			objXSLProc.input = objXML;
> 			// objXSLProc.addParameter("NameOfYourParameter1", "ValueOfYourParameter1");
> 			// objXSLProc.addParameter("NameOfYourParameter2", "ValueOfYourParameter2");
> 			// etc...
> 
> 			// transform
> 			objXSLProc.transform();
> 
> 			// display
> 			document.write(objXSLProc.output);
> 		</SCRIPT>
> 
> 	</body>
> 
> </html>
> 
> --------- END HTML/JAVASCRIPT --------

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread