Search code examples
xmlxpathxqueryzorba

XML node containing an XPath expression from another XML file - how to get its value?


File common.xml:

<common>
  <os1>Windows 7</os1>
</common>

File item.xml:

<item>
  <os>common/os1</os>
</item>

So the os node in the item.xml file contains an XPath expression pointing to a node in the common.xml file. How can I get the actual value of the node pointed by the XPath expression (Windows 7). If I use $input-context/item/os I get common/os1.

I'm using zorba-xquery and I have declared:

declare variable $input-context external;
declare variable $common-context external;

Solution

  • This can be done with the eval function of the reflection module:

    import module namespace reflection = "http://www.zorba-xquery.com/modules/reflection";
    
    declare variable $common-context :=
      document {
        <common>
          <os1>Windows 7</os1>
        </common>
      };
    
    declare variable $input-context :=
      document {
        <item>
          <os>common/os1</os>
        </item>
      };
    
    let $path := $input-context/item/os/text()
    let $query := fn:concat('$common-context/', $path, '/text()')
    return
      reflection:eval($query)