locked
How to run .xsl file in html RRS feed

  • Question

  • User-893002196 posted

    Hi All,

     

    How to run my .xsl file in HTML?

     

    Please advise.

     

    compare.xsl

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:exslt="http://exslt.org/common" version="1.0">
      
      <xsl:param name="url1" select="'tests3a.xml'"/>
      <xsl:param name="url2" select="'tests3b.xml'"/>
    
      <xsl:variable name="docA" select="document($url1)"/>
      <xsl:variable name="docB" select="document($url2)"/>
    
    
      <!-- This produces a whole nother copy of both docs!
           So, is the performance cost worth it?? -->
    
      <xsl:variable name="sortedNodesA">
        <!-- produce a sorted, flattened RTF of A's nodes -->
        <xsl:for-each select="$docA/Items/Item/*">
          <xsl:sort select="name()" />
          <xsl:copy-of select="." />
        </xsl:for-each>
      </xsl:variable>
    
      <xsl:variable name="sortedNodesB">
        <!-- produce a sorted, flattened RTF of B's nodes -->
        <xsl:for-each select="$docB/Items/Item/*">
          <xsl:sort select="name()" />
          <xsl:copy-of select="." />
        </xsl:for-each>
      </xsl:variable>
    
      <xsl:template match="/">
        <xsl:call-template name="recurse">
          <xsl:with-param name="nodesA"
            select="exslt:node-set($sortedNodesA)/*" />
          <xsl:with-param name="nodesB"
            select="exslt:node-set($sortedNodesB)/*" />
        </xsl:call-template>
      </xsl:template>
     
      <xsl:template name="recurse">
        <xsl:param name="nodesA" />
        <xsl:param name="nodesB" />
        <xsl:if test="$nodesA | $nodesB">
          <xsl:variable name="nameA" select="name($nodesA[1])" />
          <xsl:variable name="nameB" select="name($nodesB[1])" />
          <xsl:variable name="compar">
            <xsl:call-template name="compare-names">
              <xsl:with-param name="a" select="$nodesA[1]" />
              <xsl:with-param name="b" select="$nodesB[1]" />
            </xsl:call-template>
          </xsl:variable>
    
          <xsl:choose>
            <xsl:when test="0 > $compar"> <!-- $nodesA[1] is alph. first -->
              <p><xsl:value-of select="$nameA" /> is only in doc A.</p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA[position()>1]" />
                <xsl:with-param name="nodesB" select="$nodesB" />
              </xsl:call-template>
            </xsl:when>
    
            <xsl:when test="$compar > 0"> <!-- $nodesB[1] is alph. first -->
              <p><xsl:value-of select="$nameB" /> is only in doc B.</p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA" />
                <xsl:with-param name="nodesB" select="$nodesB[position()>1]" />
              </xsl:call-template>
            </xsl:when>
    
            <xsl:otherwise>
              <p><xsl:value-of select="$nameB" /> is in both documents.
                <!-- Do I need string(text(...))? -->
                <xsl:if
                  test="string($nodesA[1]/text()) != string($nodesB[1]/text())">
                  But their contents differ:
                  '<xsl:value-of select="$nodesA[1]/text()" />' !=
                  '<xsl:value-of select="$nodesB[1]/text()" />'.
                </xsl:if>
              </p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA[position()>1]" />
                <xsl:with-param name="nodesB" select="$nodesB[position()>1]" />
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:template>
    
      <xsl:template name="compare-names">
        <!-- Output -1, 0, or 1 as name of node A sorts before, equal to,
             or after name of node B. -->
        <xsl:param name="a" />
        <xsl:param name="b" />
        <xsl:choose>
          <xsl:when test="name($a) = name($b)"> 0 </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="$a|$b">
              <xsl:sort select="name()" />
              <xsl:if test="position() = 1">
                <xsl:choose>
                  <xsl:when test="name(.) = name($a)"> -1 </xsl:when>
                  <xsl:otherwise> 1 </xsl:otherwise>
                </xsl:choose>
              </xsl:if>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    </xsl:stylesheet>

    tests3a.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Items>
    <Item>
       <name> Item1 </name>
       <value> 10   </value>
    </Item>
    <Item>
       <name> Item2 </name>
       <value> 20   </value>
    </Item>
    <Item>
       <name> Item3 </name>
       <value> 30   </value>
    </Item>
    </Items>
    

    tests3b.xml

    <?xml version="1.0" encoding="utf-8"?>
    <Items>
    <Item>
       <name> Item1 </name>
       <value> 20   </value>
    </Item>
    <Item>
       <name> Item2 </name>
       <value> 20   </value>
    </Item>
    <Item>
       <name> Item4 </name>
       <value> 50   </value>
    </Item>
    </Items>
    
    

     

    Please advise.

     

    Thank you.

     

    Regards,

    Micheale

    Tuesday, April 2, 2013 9:43 PM

Answers

  • User-893002196 posted

    <?xml version="1.0" encoding="utf-8"?> <stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/exsl" version="1.1" extension-element-prefixes="exsl" exsl:doc="http://www.exslt.org/exsl">    <import href="node-set/exsl.node-set.xsl"/>    <import href="object-type/exsl.object-type.xsl"/> </stylesheet>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 3, 2013 5:42 AM

All replies

  • User-1137493631 posted

    you can do this with XSLTProcessor object. did you had a chance to view this page ?

    http://stackoverflow.com/questions/3014281/how-to-run-xsl-file-using-javascript-html-file

    Tuesday, April 2, 2013 9:57 PM
  • User-893002196 posted

    Hi Thirumaran,

     

    I know the method as shown:-

    http://stackoverflow.com/questions/3014281/how-to-run-xsl-file-using-javascript-html-file 

    This only work if i only have 1 xml to transform using xsl.

     

    But, i do not want to pass the .xml file as Load. You can see my .xsl calling 2 xml to do comparison:- 

    <xsl:param name="url1" select="'tests3a.xml'"/>  
    <xsl:param name="url2" select="'tests3b.xml'"/>


     <xsl:variable name="docA" select="document($url1)"/>  
    <xsl:variable name="docB" select="document($url2)"/>
    :
    :

     

    Do you have any idea how can i execute the .xsl to shown the output in html view? I really get stuck on this matter.

     

    Please advise.

     

    Thank you.

     

    Regards,

    Micheale

     

    Tuesday, April 2, 2013 10:05 PM
  • User-893002196 posted

    Hi All,

     

    I try using below method, but still failed.

    Please advise.

     

    compare.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>UCD Course Selector</title>
    
    <style>
    body{
    font:75%/150% "Trebuchet MS", "Lucida Grande", "Bitstream Vera Sans", Arial, Helvetica, sans-serif;
    color:#666666;
    margin:100px;
    }
    td, th{
    padding:5px;
    }
    th{
    background:#993333;
    color:#ffffff;
    }
    </style>
    <script>
    //
    // function which loads table from XSL to browser based on user selection
    //
    function runSelection(){
    if(navigator.appName!="Microsoft Internet Explorer"){
    // Mozilla
    var xsltProcessor = new XSLTProcessor();
    // load the xsl file
    var myXMLHTTPRequest = new XMLHttpRequest();
    myXMLHTTPRequest.open("GET", "compare.xsl", false);
    myXMLHTTPRequest.send(null);
    
    // get the XML document
    xslStylesheet = myXMLHTTPRequest.responseXML;
    xsltProcessor.importStylesheet(xslStylesheet);
    // load the xml file
    myXMLHTTPRequest = new XMLHttpRequest();
    myXMLHTTPRequest.open("GET", "", false);
    myXMLHTTPRequest.send(null);
    var xmlSource = myXMLHTTPRequest.responseXML;
    // set the parameters
    //transform and display
    var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);
    document.getElementById("example").appendChild(resultDocument);
    // IE
    }else if(window.ActiveXObject){
    // Load XML
    var xslt = new ActiveXObject("Msxml2.XSLTemplate.3.0");
    var xsldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");
    var xslproc;
    xsldoc.async = false;
    xsldoc.load("compare.xsl");
    if (xsldoc.parseError.errorCode != 0) {
       var myErr = xsldoc.parseError;
       alert("You have error " + myErr.reason);
    } else {
       xslt.stylesheet = xsldoc;
       var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
       xmldoc.async = false;
       xmldoc.load("compare.xml");
       if (xmldoc.parseError.errorCode != 0) {
          var myErr = xmldoc.parseError;
          alert("You have error " + myErr.reason);
       } else {
          xslproc = xslt.createProcessor();
          xslproc.input = xmldoc;      
          xslproc.transform();
    	  alert(xmldoc.transformNode(xsldoc));
          document.getElementById("example").innerHTML=xmldoc.transformNode(xsldoc);
       }
    }
    }else{
    // Browser unknown
    alert("Browser unknown");
    }
    }
    </script>
    </head>
    <body onload="runSelection()">
    <div id="example">
    </div>
    </body></html>
    


    compare.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <files>
      <file href="tests3a.xml" />
      <file href="tests3b.xml" />
    </files>
    


    compare.xsl:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:exslt="http://exslt.org/common" version="1.0">
      <xsl:output method="html" version="4.01" encoding="UTF-8" omit-xml-declaration="yes" standalone="yes" indent="yes"/>
     
     <xsl:variable name="docA"
                  select="document(/files/file[1]/@href, /)" />
    <xsl:variable name="docB"
                  select="document(/files/file[2]/@href, /)" />
    
      <!-- This produces a whole nother copy of both docs!
           So, is the performance cost worth it?? -->
    
      <xsl:variable name="sortedNodesA">
        <!-- produce a sorted, flattened RTF of A's nodes -->
        <xsl:for-each select="$docA/Items/Item/*">
          <xsl:sort select="name()" />
          <xsl:copy-of select="." />
        </xsl:for-each>
      </xsl:variable>
    
      <xsl:variable name="sortedNodesB">
        <!-- produce a sorted, flattened RTF of B's nodes -->
        <xsl:for-each select="$docB/Items/Item/*">
          <xsl:sort select="name()" />
          <xsl:copy-of select="." />
        </xsl:for-each>
      </xsl:variable>
    
      <xsl:template match="/">
        <xsl:call-template name="recurse">
          <xsl:with-param name="nodesA"
            select="exslt:node-set($sortedNodesA)/*" />
          <xsl:with-param name="nodesB"
            select="exslt:node-set($sortedNodesB)/*" />
        </xsl:call-template>
      </xsl:template>
     
      <xsl:template name="recurse">
        <xsl:param name="nodesA" />
        <xsl:param name="nodesB" />
        <xsl:if test="$nodesA | $nodesB">
          <xsl:variable name="nameA" select="name($nodesA[1])" />
          <xsl:variable name="nameB" select="name($nodesB[1])" />
          <xsl:variable name="compar">
            <xsl:call-template name="compare-names">
              <xsl:with-param name="a" select="$nodesA[1]" />
              <xsl:with-param name="b" select="$nodesB[1]" />
            </xsl:call-template>
          </xsl:variable>
    
          <xsl:choose>
            <xsl:when test="0 > $compar"> <!-- $nodesA[1] is alph. first -->
              <p><xsl:value-of select="$nameA" /> is only in doc A.</p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA[position()>1]" />
                <xsl:with-param name="nodesB" select="$nodesB" />
              </xsl:call-template>
            </xsl:when>
    
            <xsl:when test="$compar > 0"> <!-- $nodesB[1] is alph. first -->
              <p><xsl:value-of select="$nameB" /> is only in doc B.</p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA" />
                <xsl:with-param name="nodesB" select="$nodesB[position()>1]" />
              </xsl:call-template>
            </xsl:when>
    
            <xsl:otherwise>
              <p><xsl:value-of select="$nameB" /> is in both documents.
                <!-- Do I need string(text(...))? -->
                <xsl:if
                  test="string($nodesA[1]/text()) != string($nodesB[1]/text())">
                  But their contents differ:
                  '<xsl:value-of select="$nodesA[1]/text()" />' !=
                  '<xsl:value-of select="$nodesB[1]/text()" />'.
                </xsl:if>
              </p>
              <xsl:call-template name="recurse">
                <xsl:with-param name="nodesA" select="$nodesA[position()>1]" />
                <xsl:with-param name="nodesB" select="$nodesB[position()>1]" />
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:template>
    
      <xsl:template name="compare-names">
        <!-- Output -1, 0, or 1 as name of node A sorts before, equal to,
             or after name of node B. -->
        <xsl:param name="a" />
        <xsl:param name="b" />
        <xsl:choose>
          <xsl:when test="name($a) = name($b)"> 0 </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="$a|$b">
              <xsl:sort select="name()" />
              <xsl:if test="position() = 1">
                <xsl:choose>
                  <xsl:when test="name(.) = name($a)"> -1 </xsl:when>
                  <xsl:otherwise> 1 </xsl:otherwise>
                </xsl:choose>
              </xsl:if>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    </xsl:stylesheet>
    
    

    tests3a.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <Items>
    <Item>
       <name> Item1 </name>
       <value> 10   </value>
    </Item>
    <Item>
       <name> Item2 </name>
       <value> 20   </value>
    </Item>
    <Item>
       <name> Item3 </name>
       <value> 30   </value>
    </Item>
    </Items>
    
    

     

    tests3b.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <Items>
    <Item>
       <name> Item1 </name>
       <value> 20   </value>
    </Item>
    <Item>
       <name> Item2 </name>
       <value> 20   </value>
    </Item>
    <Item>
       <name> Item4 </name>
       <value> 50   </value>
    </Item>
    </Items>
    


    Please advise.

     

    Thank you.

     

    Regards,

    Micheale

    Wednesday, April 3, 2013 12:12 AM
  • User-893002196 posted

    <?xml version="1.0" encoding="utf-8"?> <stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/exsl" version="1.1" extension-element-prefixes="exsl" exsl:doc="http://www.exslt.org/exsl">    <import href="node-set/exsl.node-set.xsl"/>    <import href="object-type/exsl.object-type.xsl"/> </stylesheet>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 3, 2013 5:42 AM
  • User-893002196 posted

    Method that my superior insist to use, refer to this post until resolve:-

    http://forums.asp.net/t/1895035.aspx/1?Problem+with+exsl+in+xsl

    Wednesday, April 3, 2013 5:44 AM