Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. using xslt params to search for elements

using xslt params to search for elements

Scheduled Pinned Locked Moved XML / XSL
xmlregexhelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Hodnick
    wrote on last edited by
    #1

    I'd like to pass a parameter into an XSLT to search for a specific element within an XML document. My document has this form: I want to be able to pass in a parameter into my XSLT and search for a particular folder - let's say id="4". This is how I've been approaching it: But I get this error: System.Xml.Xsl.XsltException: '/library//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: /library//folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. What might be a better or more correct way to approach this? Michael Hodnick

    M S 3 Replies Last reply
    0
    • M Mike Hodnick

      I'd like to pass a parameter into an XSLT to search for a specific element within an XML document. My document has this form: I want to be able to pass in a parameter into my XSLT and search for a particular folder - let's say id="4". This is how I've been approaching it: But I get this error: System.Xml.Xsl.XsltException: '/library//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: /library//folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. What might be a better or more correct way to approach this? Michael Hodnick

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi Michael. At first glance, I think the syntax

      <xsl:template match="/library//folder[@id=$folderID]">

      is wrong. You have two slashes between library and folder - did you want instead something like this?

      <xsl:template match="//folder[@id=$folderID]">

      to match a folder node regardless of where it is in the heirarchy?

      M 1 Reply Last reply
      0
      • M Mike Ellison

        Hi Michael. At first glance, I think the syntax

        <xsl:template match="/library//folder[@id=$folderID]">

        is wrong. You have two slashes between library and folder - did you want instead something like this?

        <xsl:template match="//folder[@id=$folderID]">

        to match a folder node regardless of where it is in the heirarchy?

        M Offline
        M Offline
        Mike Hodnick
        wrote on last edited by
        #3

        I've also tried what you are suggesting, but still receive the following error: '//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: //folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. Michael Hodnick www.kindohm.com blogs.kindohm.com

        1 Reply Last reply
        0
        • M Mike Hodnick

          I'd like to pass a parameter into an XSLT to search for a specific element within an XML document. My document has this form: I want to be able to pass in a parameter into my XSLT and search for a particular folder - let's say id="4". This is how I've been approaching it: But I get this error: System.Xml.Xsl.XsltException: '/library//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: /library//folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. What might be a better or more correct way to approach this? Michael Hodnick

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          I've just tried your approach in xsltproc (part of libxslt) and it works fine. I presume you are passing a value for folderID into the transform engine??? I used the command 'xsltproc --param folderID 4 a.xsl a.xml' to perform the transform (the xsl and xml names are obvious :-)) This was the XSL I used (based on yours - the template selector is the same as yours)<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"> <xsl:output method="xml" encoding="ISO8859-1" indent="yes" omit-xml-declaration="no"/> <xsl:param name="folderID"/> <xsl:template match="/library//folder[@id=$folderID]"> <Wib> <xsl:value-of select="count(./file)"/> </Wib> </xsl:template> </xsl:stylesheet>
          Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

          1 Reply Last reply
          0
          • M Mike Hodnick

            I'd like to pass a parameter into an XSLT to search for a specific element within an XML document. My document has this form: I want to be able to pass in a parameter into my XSLT and search for a particular folder - let's say id="4". This is how I've been approaching it: But I get this error: System.Xml.Xsl.XsltException: '/library//folder[@id=$folderID]' is an invalid XPath expression. ---> System.Xml.XPath.XPathException: /library//folder[@id=$folderID] is an invalid key pattern. It either has a variable reference or key function. What might be a better or more correct way to approach this? Michael Hodnick

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            My previous reply was wrong - you can't use a variable reference in a template selector - see this You'll have to use something like the following instead (i.e. put the predicate on a higher-level select expression and keep the template matching rule wide):

            <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            exclude-result-prefixes="xsl">

            <xsl:output method="xml" encoding="ISO8859-1" indent="yes" omit-xml-declaration="no"/>
            <xsl:param name="folderID"/>
            <xsl:template match="/">
            <xsl:apply-templates select="/library//folder[@id=$folderID]"/>
            </xsl:template>
            <xsl:template match="/library//folder">
            <Wib>
            <xsl:value-of select="count(./file)"/>
            </Wib>
            </xsl:template>
            </xsl:stylesheet>

            HTH Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

            M 1 Reply Last reply
            0
            • S Stuart Dootson

              My previous reply was wrong - you can't use a variable reference in a template selector - see this You'll have to use something like the following instead (i.e. put the predicate on a higher-level select expression and keep the template matching rule wide):

              <xsl:stylesheet version="1.0"
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
              exclude-result-prefixes="xsl">

              <xsl:output method="xml" encoding="ISO8859-1" indent="yes" omit-xml-declaration="no"/>
              <xsl:param name="folderID"/>
              <xsl:template match="/">
              <xsl:apply-templates select="/library//folder[@id=$folderID]"/>
              </xsl:template>
              <xsl:template match="/library//folder">
              <Wib>
              <xsl:value-of select="count(./file)"/>
              </Wib>
              </xsl:template>
              </xsl:stylesheet>

              HTH Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

              M Offline
              M Offline
              Mike Hodnick
              wrote on last edited by
              #6

              That did the trick. Thanks for the w3c link and the example. Michael Hodnick www.kindohm.com blogs.kindohm.com

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups