Note that there are some explanatory texts on larger screens.

plurals
  1. POusing a single xsl file to display different elements
    primarykey
    data
    text
    <p>I have an xml file with the following data:</p> <pre><code>&lt;BOOKS&gt; &lt;BOOK&gt; &lt;TITLE image="./images/govnahDesign.jpg"&gt;Bible&lt;/TITLE&gt; &lt;CATEGORY&gt;Book&lt;/CATEGORY&gt; &lt;GENRE&gt;Gospel&lt;/GENRE&gt; &lt;PRICE&gt;0.00&lt;/PRICE&gt; &lt;SUMMARY&gt;This is the BOOK of life. The truth and the light.&lt;/SUMMARY&gt; &lt;REVIEW&gt;This indeed the truth and the light. This BOOK is pure fact no fiction.&lt;/REVIEW&gt; &lt;/BOOK&gt; &lt;BOOK&gt; &lt;TITLE image="./images/govnahDesign.jpg"&gt;Scrap Book&lt;/TITLE&gt; &lt;CATEGORY&gt;Novel&lt;/CATEGORY&gt; &lt;GENRE&gt;Ghetto Gospel&lt;/GENRE&gt; &lt;PRICE&gt;19.00&lt;/PRICE&gt; &lt;SUMMARY&gt;This a message from Guvnah B. UK gospel artist doing it big no long things&lt;/SUMMARY&gt; &lt;REVIEW&gt;I love this message. It gives life a fresh air and makes you love life.&lt;/REVIEW&gt; &lt;/BOOK&gt; &lt;BOOK&gt; &lt;TITLE image="./images/govnahDesign.jpg"&gt;Daddy's Boy&lt;/TITLE&gt; &lt;CATEGORY&gt;Magazine&lt;/CATEGORY&gt; &lt;GENRE&gt;Gospel&lt;/GENRE&gt; &lt;PRICE&gt;2.00&lt;/PRICE&gt; &lt;SUMMARY&gt;This is for Christians who has a father up in Heaven. He really loves his children.&lt;/SUMMARY&gt; &lt;REVIEW&gt;This makes me want a father like they talking about. He must be a great father. Great magazine.&lt;/REVIEW&gt; &lt;/BOOK&gt; &lt;/BOOKS&gt; </code></pre> <p>I am currently displaying each in separate xsl files. example: this would be xsl file for categry='Book"</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="/"&gt; &lt;xsl:for-each select="BOOKS/BOOK"&gt; &lt;xsl:if test="CATEGORY='Book'"&gt;&lt;!-- Show category NOVEL only --&gt; &lt;div class="item"&gt; &lt;xsl:value-of select="TITLE"/&gt; &lt;img&gt; &lt;xsl:attribute name="src"&gt; &lt;xsl:value-of select="TITLE//@image"/&gt; &lt;/xsl:attribute&gt; &lt;/img&gt; Price: &lt;xsl:value-of select="PRICE"/&gt; &lt;button id="view" onclick="javascript:viewBook()"&gt;View&lt;/button&gt; &lt;/div&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>My question is, is it possible to have just one xsl file which i can change the value of the category in the if statement when the xsl file opens? </p> <pre><code>&lt;xsl:if test="CATEGORY='Book'"&gt; </code></pre> <p>I open the xsl file with javascript into a div on the main page. </p> <p>I have searched for a couple of days now trying to use javascript to pass a value but no avail. Any directions, help and advice would be much appreciated. </p> <p>Thank You</p> <p>Addition:</p> <p>the java script that opens the required xsl file depending on the category name:</p> <pre><code>var root=xmlDoc.getElementsByTagName("CATEGORY"); strVal = new Array(); for (i=0;i&lt;root.length;++i) { var title=(root[i].childNodes[0].nodeValue); strVal[i]='&lt;li&gt;&lt;a href="#" onClick="javascript:Navigate(\''+xslCatFileName+'\')"&gt;'+xslCatFileName+'&lt;/a&gt;&lt;/li&gt;'; string = string + strVal[i]; } document.getElementById("category").innerHTML+=string; </code></pre> <p>Here is the Navigation() function the above calls:</p> <pre><code>function Navigate(catName) { //create an instance of the DOMDocument object. var xmlDoc=new ActiveXObject ("Msxml2.DOMDocument.6.0"); var xslDoc=new ActiveXObject("Msxml2.DOMDocument.6.0"); var i; xmlDoc.async=false; xslDoc.async=false; //stores the category passed var xslCatName = catName; xmlDoc.load("./xml/books.xml"); xslDoc.load("./xml/"+xslCatName+".xsl"); //check wether the process of loading is complete and that there are no errors in the XML document. if (xmlDoc.readyState == 4 &amp;&amp; xmlDoc.parseError.errorCode == 0) { var output=xmlDoc.transformNode(xslDoc); //transforms the xml sheet to xslt //this clears the div before it loads var y = document.getElementById('showBOOKS'), child; while(child=y.firstChild) y.removeChild(child); document.getElementById("showBOOKS").innerHTML+=output; } //if the XML document contains an error, display an error message. else { alert("Failed to load the document. Check wether your XML document is well-formed"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload