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: Bible Book

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:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="CATEGORY" select="Book"/>

<xsl:template match="/">
<xsl:for-each select="BOOKS/BOOK">
<xsl:if test="CATEGORY=$CATEGORY"><!-- Show category NOVEL only -->
<div class="item">
       <xsl:value-of select="TITLE"/>
        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="TITLE//@image"/>
            </xsl:attribute>
        </img>
        Price: <xsl:value-of select="PRICE"/>
        <button id="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:if test="CATEGORY=$category">

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