Amin Sayed
Amin Sayed

Reputation: 1260

Generating XSLT in the following Scenerio

I'm getting a following response from the back-end servers to display the data as quick links on the right side of the search results page.

 <NavigatorItems>
  <Navigator Name="Shoes">
   <Name>Nike</Name>
   <WebSite>www.nike.com</WebSite>
   <Name>Reebok</Name>
   <WebSite>www.reebok.com</WebSite>
   <Name>Adidas</Name>
   <WebSite>www.adidas.com</WebSite>
   <ShowAll>www.mysite.com/showallshoes</ShowAll>
  </Navigator>
  <Navigator Name="Clothes">
   <Name>Lee Jeans</Name>
   <WebSite>www.lee.com</WebSite>
   <Name>Levis</Name>
   <WebSite>www.levi.com</WebSite>
   <Name>Lawman</Name>
   <WebSite>www.lawman.com</WebSite>
   <ShowAll>www.mysite.com/showallclothes</ShowAll>
  </Navigator>
 </NavigatorItems>

I need to display these items using XSLT something like this:

enter image description here

The sample XSLT suggested by someone is something like this:

 <xsl:for-each select="NavigatorItems/Navigator">
   <xsl:variable name="link" select="WebSite"/>
   <tr>
     <td><a href ="{$link}"><xsl:value-of select="Name"/></td>
   </tr>
   <xsl:test select="ShowAll"> 
     <xsl:variable name="linkShowAll" select="ShowAll"/>
      <tr> <td> <a href="{$linkShowAll}"> View More Results <td> </tr>
   </xsl:test>
 </xsl:for-each>

But it displays only

 Nike (with its appropriate link)

 Lee (with its appropriate link)

Where I'm going wrong with this ? I tried a lot to modify the XSLT and checked but no luck.

Please suggest.

Upvotes: 1

Views: 118

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 
 <xsl:template match="Navigator">
  <p><xsl:value-of select="@Name"/></p>
  <ul>
   <xsl:apply-templates select="Name"/>
  </ul>
  <xsl:apply-templates select="ShowAll"/>
 </xsl:template>
 
 <xsl:template match="Name">
  <li>
   <a href="http://{following-sibling::WebSite[1]}">
     <xsl:value-of select="."/>
   </a>
  </li>
 </xsl:template>
 
 <xsl:template match="ShowAll">
  <p>
    <a href="http://{.}">
     <xsl:text>View More Results</xsl:text>
    </a>
  </p>
 </xsl:template>
</xsl:stylesheet>

when applied to the provided XML document:

 <NavigatorItems>
  <Navigator Name="Shoes">
   <Name>Nike</Name>
   <WebSite>www.nike.com</WebSite>
   <Name>Reebok</Name>
   <WebSite>www.reebok.com</WebSite>
   <Name>Adidas</Name>
   <WebSite>www.adidas.com</WebSite>
   <ShowAll>www.mysite.com/showallshoes</ShowAll>
  </Navigator>
  <Navigator Name="Clothes">
   <Name>Lee Jeans</Name>
   <WebSite>www.lee.com</WebSite>
   <Name>Levis</Name>
   <WebSite>www.levi.com</WebSite>
   <Name>Lawman</Name>
   <WebSite>www.lawman.com</WebSite>
   <ShowAll>www.mysite.com/showallclothes</ShowAll>
  </Navigator>
 </NavigatorItems>

produces the wanted, correct result:

<p>Shoes</p>
<ul>
   <li>
      <a href="http://www.nike.com">Nike</a>
   </li>
   <li>
      <a href="http://www.reebok.com">Reebok</a>
   </li>
   <li>
      <a href="http://www.adidas.com">Adidas</a>
   </li>
</ul>
<p>
   <a href="http://www.mysite.com/showallshoes">View More Results</a>
</p>
<p>Clothes</p>
<ul>
   <li>
      <a href="http://www.lee.com">Lee Jeans</a>
   </li>
   <li>
      <a href="http://www.levi.com">Levis</a>
   </li>
   <li>
      <a href="http://www.lawman.com">Lawman</a>
   </li>
</ul>
<p>
   <a href="http://www.mysite.com/showallclothes">View More Results</a>
</p>

and the browser displays it like this:

Shoes

View More Results

Clothes

View More Results

Upvotes: 1

Related Questions