XSLT - Parameter value equals path to attribute, for query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrdancu
    New Member
    • Nov 2008
    • 4

    XSLT - Parameter value equals path to attribute, for query

    Hi
    I am very new to XSLT and have the following problem:
    I would like to use a parameters value for the path to an attribute in my select query. For example:

    I have an attribute named "pi" for the "date" element that sits in this path in my xml file:

    visitors/visitor/date/@pi

    so if I set the parameter:

    <xsl:param name="myvar">da te/@pi</xsl:param>

    and create my select query to show each record with a date pi attribute with a value of 'Y':

    <xsl:for-each select="visitor s/visitor[$myvar = 'Y']">

    it does not work as I was hoping???
    I am probably thinking about things wrong, so if anyone could post any tips, that would be great.
    Thanks,
    Mat.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Code:
    <xsl:param name="myvar">date/@pi</xsl:param>
    $myvar is in this case a string (think of $myvar = 'date/@pi').

    Code:
    <xsl:param name="myvar" select="date/@pi"/>
    will return a not-empty node/node-set if (and only if) the used xpath matches a node at the time of defining the parameter.

    rule of thumb: <param> and <variable> are either empty, a string or a node-set. you cannot use them as part of an xpath-string like "/node()/$param/node()"

    regards

    Comment

    • mrdancu
      New Member
      • Nov 2008
      • 4

      #3
      Thanks for your reply. I think I understand!
      I am currently passing the value of parameters from javascript using the following method:

      xslProc.addPara meter("myvar", "date/@pi");

      This would add "date/@pi" to the value of $myvar, not to the select value of $myvar.

      How would I achieve this?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I don't think that's correct, but I admit I don't use xslt via javascript.

        I assume that the expression of $myvar is empty when you set the parameter.
        add somewhere in your xsl: <xsl:value-of select="$myvar"/> to check for the content of $myvar.

        furthermore, visitors/visitor[$myvar = 'Y'] will imho never work as intended, because you can't save an xpath in a variable (which you'd need for that, you only have string, number or node-set available).

        Originally posted by mrdancu
        I would like to use a parameters value for the path to an attribute in my select query.
        to answer that clearly, you can't do that.


        regards

        PS: I wish I could save an xpath in a variable, would be very useful.....

        Comment

        • mrdancu
          New Member
          • Nov 2008
          • 4

          #5
          Thanks again for the reply... I think you have confirmed what I thought, I can't pass an xpath query from javascript to an xslt file.
          So I probably need to rethink what i am trying to acheive:

          Basically I have a xml file with many visitor records (path: visitors/visitor) that have a date element with differing attributes.
          Some will have an @pi1 = 'Y', some will have an @pi2 = 'Y', @pi3 = 'Y' etc etc etc up to @pi8.

          What I would like to do is select the records based on whether they have a specific attribute or not.
          I was trying to use javascript to achieve this with 1 xslt template and pass parameters to the xslt, but I may just have to use more than 1 template with a fixed query and vary the template used in the javascript.

          If you have any other suggestions, that would be great.

          Thanks again.
          Mat.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            you could try to get the parameter to hold the complete xpath.
            Code:
            // JS
            xslProc.addParameter("myvar", "//visitor[date/@pi1 = 'Y']");
            
            // XSL
            <xsl:param name="myvar" select="//visitor[date/@pi1 = 'Y']"/>
            // using
            <xsl:for-each select="$myvar">
            // code...
            </xsl:for-each>
            check with <value-of> what the variable holds.

            regards

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              If you treat the parameter as a name, then you could pass in a string. This would assume that values were either "Y" or "N".

              xslProc.addPara meter("myvar", "pi1");

              <xsl:param name="myvar" select="'pi1'"/>
              <xsl:for-each select="visitor s/visitor[.//@*[local-name() = $myvar and .='Y']">

              Comment

              • mrdancu
                New Member
                • Nov 2008
                • 4

                #8
                Thanks to both of you for you help. I seemed to have cracked it using the following code:

                Javascript:
                Code:
                xslProc.addParameter("myvar", "pi1");
                XSLT:
                Code:
                <xsl:param name="myvar"/>  
                <xsl:for-each select="visitors/visitor[date/@*[local-name() = $myvar]]">
                If the date element has a @pi1, the record gets included!

                Comment

                Working...