Alexandr Manuylov
Alexandr Manuylov

Reputation: 9

XSLT Transform UL to SELECT

need your help with the following... how transform this:

<ul>
    <li value="xxx">Text XXX</li>
    <li value="zzz">Text ZZZ</li>
    <li value="yyy">Text YYY</li>
</ul>

to this

<select>
    <option value="xxx">Text XXX</option>
    <option value="zzz">Text ZZZ</option>
    <option value="yyy">Text YYY</option>
</select>

Thank you in advance.

I tried something that is very far from being correct

<xsl:for-each select="ul/li">
    <option value="*/@value">
        <xsl:copy-of select="li"/>
    </option>
</xsl:for-each>

Upvotes: 0

Views: 30

Answers (2)

Matti Virkkunen
Matti Virkkunen

Reputation: 65156

I wouldn't call your attempt "total crap", it's actually pretty close. What you need to change is:

  1. Put {} around values if you want to interpolate them into arguments
  2. To access an attribute on the current element you just need @value, no */
  3. To get the text of the current element you can use value-of and . to refer to the current element.

Like this:

<select>
  <xsl:for-each select="ul/li">
    <option value="{@value}">
      <xsl:value-of select="." />
    </option>
  </xsl:for-each>
</select>

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167696

It is straight-forward with templates:

<xsl:template match="ul">
  <select>
    <xsl:apply-templates/>
  </select>
</xsl:template>

<xsl:template match="li">
  <option value="{@value}">
    <xsl:apply-templates/>
  </option>
</xsl:template>

Upvotes: 1

Related Questions