Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

XPATH with CL_XSLT_PROCESSOR->set_expression( ... )

Former Member
0 Kudos

Hi all,

I have a problem by using the method cl_xslt_processor->set_expression( expression = ... nsdeclarations = ... ). I want to use this method to get some nodes by using Xpath.

My problem is the parameter 'nsdeclarations' which should be filled like 'prefix1 URI1 prefix2 URI2 ...'. But my xml document has no prefix. It looks like

<ROOT xmlns="http://uri">....</ROOT>

After getting a node collection I create a iterator und try to get the first element ( lr_element ?= lr_iterator->get_next( ). )

But lr_element is always initial. I tried to call the method without the parameter 'nsdeclarations' (is optional) but it didn't work, as well as passing only the uri.

If I put a prefix into my xml and my namespace parameter it works.

Does anybody have an idea what has to be passed in my case (no namespace prefix)?

Thanks in advance.

Best regards

Guenter

1 ACCEPTED SOLUTION

former_member182670
Contributor

Hi,

You have to map default namespance to your own prefix and the use it in the XPath.

See example where I declare 'my' prefix

DATA l_xslt TYPE REF TO cl_xslt_processor.
DATA: nodes TYPE REF TO if_ixml_node_collection.

CREATE OBJECT l_xslt TYPE cl_xslt_processor.
l_xslt->set_source_string(
    '<?xml version="1.0" encoding="ISO-8859-1"?>' &
    '<a xmlns="http://uri.com">' &
    '<foo>bar</foo>' &
    '</a>' ).

l_xslt->set_expression(
  expression = 'my:a/my:foo'
  nsdeclarations = 'my http://uri.com'
).

l_xslt->run( '' ).

nodes = l_xslt->get_nodes( ).

1 REPLY 1

former_member182670
Contributor

Hi,

You have to map default namespance to your own prefix and the use it in the XPath.

See example where I declare 'my' prefix

DATA l_xslt TYPE REF TO cl_xslt_processor.
DATA: nodes TYPE REF TO if_ixml_node_collection.

CREATE OBJECT l_xslt TYPE cl_xslt_processor.
l_xslt->set_source_string(
    '<?xml version="1.0" encoding="ISO-8859-1"?>' &
    '<a xmlns="http://uri.com">' &
    '<foo>bar</foo>' &
    '</a>' ).

l_xslt->set_expression(
  expression = 'my:a/my:foo'
  nsdeclarations = 'my http://uri.com'
).

l_xslt->run( '' ).

nodes = l_xslt->get_nodes( ).