[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Choosing Elements

Subject: RE: Choosing Elements
From: "Jacoby, Peter R." <PJACOBY@xxxxxxxxxxxx>
Date: Tue, 16 Jul 2002 14:52:51 -0400
html select choosing element
Jennifer,

> My XML file looks a little like this:
  <books>
> 	<Book>
> 		<Name>Sample</Name>
> 		<Parameter>
> 			<Name>Branch</Name>
> 			<Value>56789</Value>
> 		</Parameter>
>                 <Parameter>
> 			<Name>Division</Name>
> 			<Value>56789</Value>
> 		</Parameter>
> 	</Book>
> 	<Book>
> 		<Name>Sample2</Name>
> 		<Parameter>
> 			<Name>Branch</Name>
> 			<Value>7890</Value>
> 		</Parameter>
> 	</Book>
  </books>

 I added a <books> element as a wrapper because you had no root node in your
example


> Where there can be any number of Books and Books can
> have any number of Parameters (they may or may not
> have the same Parameters). 

When you need to match an unknown number of different Parameter elements,
keys can be very useful.  They can be difficult to learn initially but are
well worth the time spent.  My solution creates a key that matches on the
Name child of each Parameter.

You then have to apply-templates to only the unique values in the key which
I do with generate-id.  I wasn't sure exactly what you wanted your html
select element to look like (as discussed earlier today the format can be a
matter of personal opinion) but I figured this would give you a start.


<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />

<xsl:key name="options" match="Book/Parameter" use="Name" />

<xsl:template match="books">
	<xsl:apply-templates select="Book/Parameter[generate-id(.) =
generate-id(key('options', Name))]"/>
</xsl:template>

<xsl:template match="Parameter">
	<xsl:value-of select="concat(Name,': ')" />
	<select name="{Name}">
		<xsl:for-each select="key('options', Name)">
			<option>
				<xsl:value-of select="Value" />
			</option>
		</xsl:for-each>
	</select>
	<br/>
</xsl:template>

<xsl:template match="text()"/>

</xsl:stylesheet>

Hope this helps.

-Peter 



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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.