Re: [xsl] Convert String to node-set?

Subject: Re: [xsl] Convert String to node-set?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 2 May 2002 17:46:51 +0100
Hi Manish,

> What I need to do is iterate through JSPRoot/listing/saleTerms,and
> since it has a and b, i want to set a and b as selected in
> JSPRoot/WizardBluePrint/Property/Category/Group/Field.

It appears to me that what you're trying to do is process the Option
elements within
/JSPRoot/WizardBluePrint/Property/Category/Group/Field, create an
option element for each of them, and add a selected attribute (with
the value 'selected') for those whose value is held higher up in the
data. The location that you check is indicated by the DataLocation and
DatabaseVariableName of the Field that wraps around the particular

Given that I've interpreted that correctly, I think the easiest
approach is to first index JSPRoot grandchildren by a combination of
their parent's name and their name, as follows:

<xsl:key name="values" match="/JSPRoot/*/*"
         use="concat('/', name(parent::*), '/', name())" />

This means that, for example, you can get the saleTerms elements
within the listing element using:

  key('values', '/listing/saleTerms')

Given that you're on a Field element, then, you can get the second
argument for the call to the key() by concatenating its DataLocation
with the DatabaseVariableName:

<xsl:template match="Field">
  <xsl:variable name="values"
                concat(DataLocation, '/', DatabaseVariableName))" />

Then you can iterate over the Option elements within the Field and
create option elements for them, adding the selected attribute if
there value of the Option matches a value in $values:

<xsl:template match="Field">
  <xsl:variable name="values"
                concat(DataLocation, '/', DatabaseVariableName))" />
  <xsl:for-each select="Option">
    <option value="{normalize-space(@value)}">
      <xsl:if test=". = $values">
        <xsl:attribute name="selected">selected</xsl:attribute>
      <xsl:value-of select="normalize-space(@value)" />

As far as I can see, there's no need for you to use a node-set()
extension function, or even evaluate(), as long as DataLocation and
DatabaseVariableName only ever contain one element name each.



Jeni Tennison

 XSL-List info and archive:

Current Thread