Tintin81
Tintin81

Reputation: 10215

How to combine two templates in XSLT?

I have two XSLT templates that I need to combine into one and then sort it in alphabetic order:

<xsl:template match="properties-for-sale">
  <xsl:for-each select="entry">
    <option value="{name}">
        <xsl:value-of select="name"/>
    </option>
  </xsl:for-each>
</xsl:template>

<xsl:template match="properties-for-rent">
  <xsl:for-each select="entry">
    <option value="{name}">
        <xsl:value-of select="name"/>
    </option>
  </xsl:for-each>
</xsl:template>

How can this be achieved in XSLT?

Thanks for any help!

Upvotes: 1

Views: 805

Answers (2)

Rookie Programmer Aravind
Rookie Programmer Aravind

Reputation: 12154

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="properties-for-sale|properties-for-rent">
    <xsl:for-each select="entry">
      <xsl:sort select="name" order="ascending"/>
      <option value="{name}">
        <xsl:value-of select="name"/>
      </option>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

use | for multiple XPaths.. and use <xsl:sort> for sorting the values ..

a reference if you want to know more about it!

http://www.w3schools.com/xsl/el_sort.asp

Upvotes: 3

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196227

You could use the | operator for matching

<xsl:template match="properties-for-sale|properties-for-rent">
  <xsl:for-each select="entry">
    <option value="{name}">
        <xsl:value-of select="name"/>
    </option>
  </xsl:for-each>
</xsl:template>

Upvotes: 1

Related Questions