Topic
  • 13 replies
  • Latest Post - ‏2016-08-31T14:51:18Z by HermannSW
neoiet2009
neoiet2009
89 Posts

Pinned topic Dynamic Xquery

‏2016-08-18T13:19:49Z | xquery

Hi All,

 

Is there any way I can create XQUERY in xslt dynamically and can call using dp:xquery-transform. Tried using XSLT variable but its giving error  local:///$x is expected. I think it expecting file in local drive perhaps. 

 

Thanks

  • neoiet2009
    neoiet2009
    89 Posts
    ACCEPTED ANSWER

    Re: Dynamic Xquery

    ‏2016-08-24T14:05:34Z  
    • HermannSW
    • ‏2016-08-24T12:13:18Z

    See the stylesheet above, the  <dp:set-variable name=" 'var://context/xquery' " value=" '...' "/>  does populate "xquery" context.

    Hermann.

    Ok Got it ...thanks

  • HermannSW
    HermannSW
    8536 Posts
    ACCEPTED ANSWER

    Re: Dynamic Xquery

    ‏2016-08-30T13:21:33Z  

    Hi Sumit,

    you really created a brain teaser for me.

    Finally I found the difference between what I showed and what you did, and that allows resolution of the issue you faced.

    I did pass in a STRING containing an XQuery script into <dp:set-variable>, you did pass in a TEXT NODE.

    Just pass "string($xquery)" to <dp:set-variable> and everything works fine, I tested that:

    <dp:set-variable name="'var://context/xquery'" value="string($xquery)" />
    


    Hermann.

  • HermannSW
    HermannSW
    8536 Posts

    Re: Dynamic Xquery

    ‏2016-08-18T16:42:45Z  

    You can write the dynamic XQuery into a context "var://context/xquery" (not a context variable "var://context/ctx/var") and have the XQuery action reference "var://context/xquery" for the script.

    Find attached a sample service that uses Fetch action to populate the xquery context with this XQeury:

    fn:reverse(//*)
    


    The XQuery action does reference "var://context/xquery" and processes input from INPUT context.

    Here you can see that it works:

    💻  echo '<x>1<y/></x>' | curl --data-binary @- http://quicksilver.boeblingen.de.ibm.com:2057; echo
    <?xml version="1.0" encoding="UTF-8"?>
    <y/><x>1<y/></x>
    💻  
    

     

    Hermann.

    Attachments

  • HermannSW
    HermannSW
    8536 Posts

    Re: Dynamic Xquery

    ‏2016-08-18T16:52:09Z  
    • HermannSW
    • ‏2016-08-18T16:42:45Z

    You can write the dynamic XQuery into a context "var://context/xquery" (not a context variable "var://context/ctx/var") and have the XQuery action reference "var://context/xquery" for the script.

    Find attached a sample service that uses Fetch action to populate the xquery context with this XQeury:

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">fn:reverse(//*) </pre>


    The XQuery action does reference "var://context/xquery" and processes input from INPUT context.

    Here you can see that it works:

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">💻 echo '<x>1<y/></x>' | curl --data-binary @- http://quicksilver.boeblingen.de.ibm.com:2057; echo <?xml version="1.0" encoding="UTF-8"?> <y/><x>1<y/></x> 💻 </pre>

     

    Hermann.

    This is better, directly answers your question (dyn.xquery.xsl attached).

    [I lost the fight with forum software to get this as text in -- but I won in getting it displayed ;-) ]

     

    Hermann.

    Attachments

    Updated on 2016-08-18T16:58:58Z at 2016-08-18T16:58:58Z by HermannSW
  • neoiet2009
    neoiet2009
    89 Posts

    Re: Dynamic Xquery

    ‏2016-08-24T10:05:51Z  
    • HermannSW
    • ‏2016-08-18T16:52:09Z

    This is better, directly answers your question (dyn.xquery.xsl attached).

    [I lost the fight with forum software to get this as text in -- but I won in getting it displayed ;-) ]

     

    Hermann.

    Tried using var://context/xquery getting black response now. In value you are using some function extension of dp? how can we put dynamic query in context variable?

     

    Thanks in advance

  • HermannSW
    HermannSW
    8536 Posts

    Re: Dynamic Xquery

    ‏2016-08-24T12:13:18Z  

    Tried using var://context/xquery getting black response now. In value you are using some function extension of dp? how can we put dynamic query in context variable?

     

    Thanks in advance

    See the stylesheet above, the  <dp:set-variable name=" 'var://context/xquery' " value=" '...' "/>  does populate "xquery" context.

    Hermann.

  • neoiet2009
    neoiet2009
    89 Posts

    Re: Dynamic Xquery

    ‏2016-08-24T14:05:34Z  
    • HermannSW
    • ‏2016-08-24T12:13:18Z

    See the stylesheet above, the  <dp:set-variable name=" 'var://context/xquery' " value=" '...' "/>  does populate "xquery" context.

    Hermann.

    Ok Got it ...thanks

  • neoiet2009
    neoiet2009
    89 Posts

    Re: Dynamic Xquery

    ‏2016-08-26T14:18:53Z  
    • HermannSW
    • ‏2016-08-24T12:13:18Z

    See the stylesheet above, the  <dp:set-variable name=" 'var://context/xquery' " value=" '...' "/>  does populate "xquery" context.

    Hermann.

    Hi Hermann,I tried below code bu getting error. what I am missing here?

    Error-  dp:xquery-transform(): Fatal Error at /temp_00820:1: [XPST0003] An expression is not a valid instance of the grammar defined for XQuery. Cause: "xml not allowed as PI target"

    code- 

    <xsl:variable name="xquery">        
    for $x in sql/row
    where $x/column/name='APPLICATION_ID'  
    return $x</xsl:variable>
            <dp:set-variable name="'var://context/xquery'" value="$xquery"/>

    <xsl:copy-of select="dp:xquery-transform('var://context/xquery',.)"/>

     

    Thanks

     

  • HermannSW
    HermannSW
    8536 Posts

    Re: Dynamic Xquery

    ‏2016-08-26T17:43:54Z  

    Hi Hermann,I tried below code bu getting error. what I am missing here?

    Error-  dp:xquery-transform(): Fatal Error at /temp_00820:1: [XPST0003] An expression is not a valid instance of the grammar defined for XQuery. Cause: "xml not allowed as PI target"

    code- 

    <xsl:variable name="xquery">        
    for $x in sql/row
    where $x/column/name='APPLICATION_ID'  
    return $x</xsl:variable>
            <dp:set-variable name="'var://context/xquery'" value="$xquery"/>

    <xsl:copy-of select="dp:xquery-transform('var://context/xquery',.)"/>

     

    Thanks

     

    You should first try to create valid XQuery in a Transform action and verify that it works.

    Then go for dynamic creation, which works if the XQuery script is flawless as I showed.

    Not knowing your input I would expect the error message if zero, two or more where clause tests are positive, because in this case the response is an XML nodeset, but no XML document because of the missing enclosing document element.

    It would be helpful if you can show/attach complete XQuery script as well as input sample.

    Hermann.

  • neoiet2009
    neoiet2009
    89 Posts

    Re: Dynamic Xquery

    ‏2016-08-26T17:54:26Z  
    • HermannSW
    • ‏2016-08-26T17:43:54Z

    You should first try to create valid XQuery in a Transform action and verify that it works.

    Then go for dynamic creation, which works if the XQuery script is flawless as I showed.

    Not knowing your input I would expect the error message if zero, two or more where clause tests are positive, because in this case the response is an XML nodeset, but no XML document because of the missing enclosing document element.

    It would be helpful if you can show/attach complete XQuery script as well as input sample.

    Hermann.

    hi Hermann,

     

    when i try to run same by calling xquery from file it works fine but when i use this approach get this error..

  • HermannSW
    HermannSW
    8536 Posts

    Re: Dynamic Xquery

    ‏2016-08-26T18:13:49Z  

    hi Hermann,

     

    when i try to run same by calling xquery from file it works fine but when i use this approach get this error..

    Please attach the sample input you use.

  • neoiet2009
    neoiet2009
    89 Posts

    Re: Dynamic Xquery

    ‏2016-08-30T09:29:53Z  
    • HermannSW
    • ‏2016-08-26T18:13:49Z

    Please attach the sample input you use.

    Hi Its the sql result message.

    PFB

    <sql result="success">
        <row>
            <column>
                <name>test</name>
                <value>test</value>
            </column>
            <column>
                <name>test</name>
                <value>test</value>
            </column>
        

        </row>
        
    </sql>

  • HermannSW
    HermannSW
    8536 Posts

    Re: Dynamic Xquery

    ‏2016-08-30T13:21:33Z  

    Hi Sumit,

    you really created a brain teaser for me.

    Finally I found the difference between what I showed and what you did, and that allows resolution of the issue you faced.

    I did pass in a STRING containing an XQuery script into <dp:set-variable>, you did pass in a TEXT NODE.

    Just pass "string($xquery)" to <dp:set-variable> and everything works fine, I tested that:

    <dp:set-variable name="'var://context/xquery'" value="string($xquery)" />
    


    Hermann.

  • neoiet2009
    neoiet2009
    89 Posts

    Re: Dynamic Xquery

    ‏2016-08-31T10:37:27Z  
    • HermannSW
    • ‏2016-08-30T13:21:33Z

    Hi Sumit,

    you really created a brain teaser for me.

    Finally I found the difference between what I showed and what you did, and that allows resolution of the issue you faced.

    I did pass in a STRING containing an XQuery script into <dp:set-variable>, you did pass in a TEXT NODE.

    Just pass "string($xquery)" to <dp:set-variable> and everything works fine, I tested that:

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr"><dp:set-variable name="'var://context/xquery'" value="string($xquery)" /> </pre>


    Hermann.

    hahah....

    Its works like a charm this time... Thanks a lot...

  • HermannSW
    HermannSW
    8536 Posts

    Re: Dynamic Xquery

    ‏2016-08-31T14:51:18Z  

    hahah....

    Its works like a charm this time... Thanks a lot...

    Attaching complete stylesheet and showing it here for convenience.

    A colleague made me aware that this method "nearly" inlines XQuery into DataPower XSLT:

    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:dp="http://www.datapower.com/extensions"
      extension-element-prefixes="dp"
    >
      <xsl:output omit-xml-declaration="yes"/>
    
      <xsl:template match="/">
        <xsl:variable name="xquery">
    for $x in sql/row
    where $x/column/name='APPLICATION_ID'  
    return $x
        </xsl:variable>
    
        <dp:set-variable name="'var://context/xquery'" value="string($xquery)" />
    
        <xsl:copy-of select="dp:xquery-transform('var://context/xquery', /*)"/>
      </xsl:template>
    
    </xsl:stylesheet>
    


    Hermann.

    Attachments

    Updated on 2016-08-31T14:52:00Z at 2016-08-31T14:52:00Z by HermannSW