Menu

How to do dynamic xquery?

Help
David
2006-12-26
2012-09-04
  • David

    David - 2006-12-26

    I find we can dynamically create XPath expressions and use them for the XPath command <xpath expression="${my_xpath}">...
    I have not been able to generate a dynamic query in XQuery. Example:

    <var-def name="my_xpath">//td[3]/h2[1]</var-def>
    <xquery>
    <xq-param name="item"><var name="item"/></xq-param>
    <xq-param name="my_xpath"><var name="my_xpath"/></xq-param>
    <xq-expression><![CDATA
    let $name := data($item{$my_xpath})
    return
    <product>
    <name>{normalize-space($name)}</name>
    </product>
    ]></xq-expression>
    </xquery>

    This code produces an exception. Is there a way to make this work?

     
    • Vladimir Nikic

      Vladimir Nikic - 2006-12-26

      try to surround xquery expression with <template>:

      <xq-expression>
      <template>
      <![CDATA
      let $name := data($item${my_xpath}) return
      <product>
      <name>{normalize-space($name)}</name>
      </product>
      ]>
      </template>
      </xq-expression>

      so that templating processor will first replace your xpath with variable.

       
    • David

      David - 2006-12-29

      makes perfect sense. thanks a million

       

Log in to post a comment.