yoshimonkey
yoshimonkey

Reputation: 21

How to Extract Text Node Value From Payload Using XPath in MEL

I want to extract some text values from an message's XML payload so that I can use them in a jdbc query.

Given the test XML file below I want to obtain the string value of first book's author text node.

Something like:

INSERT INTO books VALUES (#[xpath('/catalog/book[0]/author/text()')])

To test the expression I am just using a logger but can't seem to get it to extract correctly.

<logger message="#[xpath('/catalog/book[0]/author/text()')]" level="DEBUG" doc:name="Logger"/>
<?xml version="1.0"?>
<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
   </book>
   <book id="bk102">
       <author>Ralls, Kim</author>
       <title>Midnight Rain</title>
       <genre>Fantasy</genre>
       <price>5.95</price>
       <publish_date>2000-12-16</publish_date>
   </book>
</catalog>

Upvotes: 2

Views: 10378

Answers (2)

jcb
jcb

Reputation: 195

David's answer worked fine except for one thing. For me, .text didn't work. I had to use .wholeText. This is probably because I'm using a xerces implementation somewhere in my work project.

Upvotes: 0

David Dossot
David Dossot

Reputation: 33413

Here is the correct MEL expression:

#[xpath('/catalog/book[1]/author/text()').text]

Note in XPath, the first node is 1 not 0.

Upvotes: 5

Related Questions