0

Let's say I have the following XML file..

<?xml version="1.0" encoding="UTF-8"?>
<products>

    <test>
        <id>test_value1</id>
        <value>1</value>
    </test>

    <test>
        <id>test_value2</id>
        <value>2</value>
    </test>

</products>

And the following XSLT file..

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
    <xsl:template match="/">

        <xsl:for-each select="products/test">
            <xsl:if test="id='test_value1'">
                 <a href="#"> Hello #{value} </a>
            </xsl:if>
        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

And I'm loading this data into a HTML page using the following code..

<!DOCTYPE html>
<html>
   <head>
      <script>
         function loadXMLDoc(filename) {
             if (window.ActiveXObject) {
                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
             }
             else {
                 xhttp = new XMLHttpRequest();
             }
             xhttp.open("GET", filename, false);
             try {
                 xhttp.responseType = "msxml-document"
             } catch (err) {
             } // Helping IE11
             xhttp.send("");
             return xhttp.responseXML;
         }

         function displayXMLDoc(xml_file, xsl_file, element_id) {
             xml = loadXMLDoc(xml_file);
             xsl = loadXMLDoc(xsl_file);

             // BROWSER IS IE / EDGE.
             if (window.ActiveXObject) {
                 ex = xml.transformNode(xsl);
                 document.getElementById(element_id).innerHTML = ex;
             }
             // BROWSER IS MOZILLA, FIREFOX, OPERA, ETC.
             else if (document.implementation && document.implementation.createDocument) {
                 var xsltProcessor = new XSLTProcessor();
                 xsltProcessor.importStylesheet(xsl);
                 var resultDocument = xsltProcessor.transformToFragment(xml, document);
                 document.getElementById(element_id).appendChild(resultDocument);
             }
         }

         function toDisplay(){
             displayXMLDoc('test.xml', 'test.xsl', 'test_output');
         }

      </script>
   </head>
   <body onload="toDisplay()">
      <div id="test_output"></div>
   </body>
</html>

How can I pass a value from HTML into the XSLT file so that the test_value1 in <xsl:if test='id='test_value1'"> is equal to the value passed from HTML, using JavaScript?

For example, if I wanted to load the second tag values using the same code found here, how would I do it?

My current thought process would be that I could assign the id as an id='' tag in the XSLT, but I'm unsure of how to load the data from HTML.

10
  • XMLHttpRequest returns asynchronous results Commented Aug 10, 2016 at 3:13
  • Is there an alternative to XMLHttpRequest that I can use that would work in my situation? Commented Aug 10, 2016 at 3:15
  • Possible duplicate of How to pass parameter to XSLT from Javascript function Commented Aug 10, 2016 at 3:16
  • Just read over this question. While it is very similar to my question posed here, I'm struggling to adapt it to my context. Commented Aug 10, 2016 at 3:39
  • What value are you trying to pass from html to xsl ? Note, closing <products> tag is missing /, e.g., </products> Commented Aug 10, 2016 at 3:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.