how to pass a variable to xsl and how xsl can recognise it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • renoua
    New Member
    • Jun 2010
    • 11

    #1

    how to pass a variable to xsl and how xsl can recognise it?

    hello,
    I have this javascript code
    Code:
    function displayResult2(id) 
    {
    xml=loadXMLDoc("anna.xml");
    xsl=loadXMLDoc("c011.xsl");
    
    // code for IE
    if (window.ActiveXObject)
      {
      ex=xml.transformNode(xsl);
      document.getElementById("right").innerHTML=ex;
      }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
      {
      var xsltProcessor=new XSLTProcessor();
      xsltProcessor.importStylesheet(xsl);
      resultDocument = xsltProcessor.transformToFragment(xml,document);
     document.getElementById("right").appendChild(resultDocument);
      }
    }
    and I want to pass the variable id at the c011.xsl
    after that.. how c011.xsl can recognize that variable? what's the syntax?
    any help would be appreciated :)
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    setting parameters in Mozilla

    Comment

    • renoua
      New Member
      • Jun 2010
      • 11

      #3
      very usefull article.. thnx!
      so, i wrote that line of code :
      Code:
      xsltProcessor.setParameter(null, "id", xsl);
      I don't know why i put xsl in the parameters but alternatively it's not working.

      I added the xsl param at c011.xsl
      Code:
      <xsl:param name="id"/>
      but the xsl if condition does not work :(

      Code:
      <xsl:if test="did/unitid='$id'">
      [I]xsl tranformation[/I]
      </xsl:if>
      maybe the syntax is wrong..
      but I don't know how to solve this.. i google it i changed it hundred of times but still failure...

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        try
        Code:
        <xsl:if test="did/unitid/text() = $id">

        Comment

        • renoua
          New Member
          • Jun 2010
          • 11

          #5
          No, it doesn't work..
          I added ' ' but still nothing..

          Code:
          <xsl:if test="did/unitid/text() = '$id'">
          maybe the variable from javascript didn't pass successfully?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            I deliberately omitted the ' '.

            further, what does "/did/unitid" return normally, a node or a nodeset?

            Comment

            • renoua
              New Member
              • Jun 2010
              • 11

              #7
              With or without ' '
              I don't see xsl transformation

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                does it work, when you do it manually?

                Comment

                • renoua
                  New Member
                  • Jun 2010
                  • 11

                  #9
                  A node,
                  well when i choose one of 106 unitid, my xml file has,
                  for example 'L7346'

                  "/did/unitid='L7346'"
                  I see the result i want.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    well, I’ve never done XSLT in JavaScript (for obvious reasons) so I’m not of much help in debugging this. Personally, I prefer server-side XSLT, esp. since AJAX (target.appendCh ild(XHR.respons eXML.documentEl ement);).

                    Comment

                    • renoua
                      New Member
                      • Jun 2010
                      • 11

                      #11
                      well,
                      I suppose I have made wrong the syntax of this

                      xsltProcessor.s etParameter(nul l, "id", xsl);

                      anyway, thank you very very much for your interest and help ! I hope to find a solution asap.. :)

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        now that you mention it, yes. (look at the example provided) the 3rd parameter of .setParameter() is the value of the parameter, where you passed the XSL file.

                        Comment

                        • renoua
                          New Member
                          • Jun 2010
                          • 11

                          #13
                          well yes the syntax was wrong

                          i corrected the code but

                          id is a variable but when i write it like that:
                          xsltProcessor.s etParameter(nul l, "uid", "id")

                          it passes to c011.xsl like a static text "id" that's why I could not see result the condition was always false


                          do you know how can i pass it dynamically?
                          what i Have to write at the 3rd parameter exactly?

                          Comment

                          • renoua
                            New Member
                            • Jun 2010
                            • 11

                            #14
                            I found it i didn't need " ".

                            so the right syntax is
                            xsltProcessor.s etParameter(nul l, "uid", id);

                            Comment

                            Working...