stego
stego

Reputation: 193

ABAP Query an XML file with xpath

My goal is to query an XML document by XPath. I couldn't find an option to do this with the if_ixml framework. I have found this article (sadly it's in German) about querying an XSLT file with XPath using CL_XSLT_PROCESSOR but that doesn't seem to work anymore, it seems like ->run( space ) is the problem, and I get this short dump with message:

No valid XSLT program supplied

Even while trying with the wrapper class CL_PROXY_XPATH in the code below, I get the same short dump.

It feels like that there must be a class to do exactly this.

DATA(s) = |<?xml version="1.0" encoding="UTF-8"?><codedb><cl id="3">|
       && |<enumeration value="AAA"/><enumeration value="AAB"/><enumeration value="AAC"/>|
       && |<enumeration value="AAD"/><enumeration value="AAE"/>|
       && |</cl></codedb>|.

data(xpp) = new cl_proxy_xpath( ).
xpp->set_source_string( s ).
xpp->run( expression = '//cl[@id=3]/enumeration[@value=$codeValue3]' ).     "<== SHORTDUMP
data(nodes) = xpp->get_nodes( ).
"Calculate all values from the nodes
data(node) = nodes->get_next( ).
while node is bound.
    write: / node->get_value( ).
    node = nodes->get_next( ).
endwhile.

Upvotes: 1

Views: 1446

Answers (1)

Sandra Rossi
Sandra Rossi

Reputation: 13646

There are two issues in your code:

  • One is that your XPath expression contains $codeValue3 which results in a No valid XSLT program supplied short dump.
  • One is due to the node in your input XML, <enumeration value="AAC"/>, which contains an empty "text value" (it doesn't contain the attributes), so the method get_value returns an empty value.

When it's corrected, both CL_XSLT_PROCESSOR and CL_PROXY_XPATH should work fine.

Here is a working code tested in ABAP 7.52, one with CL_PROXY_XPATH and one with CL_XSLT_PROCESSOR:

DATA(s) = |<?xml version="1.0" encoding="UTF-8"?><codedb><cl id="3">|
       && |<enumeration value="AAA"/><enumeration value="AAB"/>|
       && |<enumeration value="AAC">AAC value</enumeration>|          "<====
       && |<enumeration value="AAD"/><enumeration value="AAE"/>|
       && |</cl></codedb>|.
DATA(expression) = `//cl[@id=3]/enumeration[@value="AAC"]`.

WRITE: / 'CL_PROXY_XPATH:'.
DATA(xpp) = NEW cl_proxy_xpath( ).
xpp->set_source_string( s ).
DATA(nodes) = xpp->get_nodes( expression = expression ).
DATA(node) = nodes->get_next( ).
WHILE node IS BOUND.
  WRITE node->get_value( ). " Get text value of node (not attributes)
  node = nodes->get_next( ).
ENDWHILE.

WRITE: / 'CL_XSLT_PROCESSOR:'.
DATA(xpp2) = NEW cl_xslt_processor( ).
xpp2->set_source_string( s ).
xpp2->set_expression( expression = expression ).
xpp2->run( ' ' ).
DATA(nodes2) = xpp2->get_nodes( ).
DO nodes2->get_length( ) TIMES.
  DATA(node2) = nodes2->get_item( sy-index - 1 ).
  WRITE node2->get_value( ).
ENDDO.

Output:

CL_PROXY_XPATH: AAC value
CL_XSLT_PROCESSOR: AAC value

Upvotes: 2

Related Questions