Try OpenEdge Now
skip to main content
Managed Adapters Guide
File Managed Adapter : Working with the File Adapter : Configuring the File Adapter : Using variables and conditions in XPath
 
Using variables and conditions in XPath
When the XML file is more complex, the additional information can be given in the XPath expression. For example:
<library>
    <book id="187510">
        <author>Johnny Appleseed</author>
        <title>Pocket Guide to Apple Trees</title>
    </book>
    <book id="187621">
        <author>Johnny Appleseed</author>
        <title>Another Pocket Guide to Apple Trees</title>
    </book>
    <book id="273531">
        <author>Johnny Appleseed</author>
        <title>Apples for Dummies</title>
    </book>
</library>
In this case, if you need to get data from the first record only, you can define the XPath expressions for selecting the author's name and the title for the book in the first record as shown below:
/library/book[1]/author
/library/book[1]/title
This definition will always return the same (first) record. If you want to make the record number configurable at run time, you can change the expressions as follows:
/library/book[${RecNo}]/author
/library/book[${RecNo}]/title
Note that you have a new parameter named "RecNo" that could be later mapped to a dataslot. As you are reading the file, the AuthorName and BookTitle are output fields (returned from the File Adapter), while the new parameter RecNo is input (passed to the File Adapter from the dataslot, before the adapter is executed).
More often, instead of using a record number to locate your book, it is more convenient to select the record by the ID attribute. If you want to search by an ID that comes with a dataslot, you can define the XPath expressions as follows:
/library/book[@id='${BookID}']/author
/library/book[@id='${BookID}']/title/library/
This will generate a new adapter input, called "BookID" that can then be mapped to a dataslot. Here you can put the book ID in a dataslot, and then invoke the File Adapter. The adapter will search the XML file for a book with this specific ID number and will return the author's name and book title.