xquerywebharvest

is it possible to use a string variable as a tag in xquery


Can I use something like following in xquery? Basically I want to use string variable as a tag. I am using web harvest to extract data. Any help or pointers appreciated.

declare variable $doc as node() external;
declare variable $tag as xs:string;
let $tag := $doc//div[@class=details]//tr[1]/td[1]
return 
    <$tag>{$doc//div[@class=details]//tr[1]/td[2]</$tag>

Solution

  • Use the element constructor element { $tag } { $content }.

    There are two additional problems with your query:

    This code should work in quite everything but MSSQL which does not support variable names in element constructor's tag name fields:

    declare variable $doc as node() external;
    
    let $tag := $doc//div[@class=details]//tr[1]/td[1]
    return 
        element { $tag } { $doc//div[@class=details]//tr[1]/td[2] }