Skip to content Skip to sidebar Skip to footer

Using A Single Xsl File To Display Different Elements

I have an xml file with the following data: BibleBook

Solution 1:

If the function you are using supports passing parameters to the xsl transform, you can simply define a parameter for that, and call the same xsl using different parameter values:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:paramname="CATEGORY"select="Book"/><xsl:templatematch="/"><xsl:for-eachselect="BOOKS/BOOK"><xsl:iftest="CATEGORY=$CATEGORY"><!-- Show category NOVEL only --><divclass="item"><xsl:value-ofselect="TITLE"/><img><xsl:attributename="src"><xsl:value-ofselect="TITLE//@image"/></xsl:attribute></img>
        Price: <xsl:value-ofselect="PRICE"/><buttonid="view"onclick="javascript:viewBook()">View</button></div></xsl:if></xsl:for-each></xsl:template></xsl:stylesheet>

This is the XSLT side. Are you searching for this?


On the javascript side you need to add the parameter before calling the transform, with something like:

xslDoc.addParameter("CATEGORY", "Novel");

I think you are also using the wrong API for your xslDoc. See this example.

Solution 2:

You could include a small xsl file which contains the value:

<xsl:variable name="category"select="'Book'"/>

and in the if statement use it:

<xsl:iftest="CATEGORY=$category">

Post a Comment for "Using A Single Xsl File To Display Different Elements"