[英]ABAP Query an XML file with xpath
My goal is to query an XML document by XPath.我的目标是通过 XPath 查询 XML 文档。 I couldn't find an option to do this with the
if_ixml
framework.我找不到使用
if_ixml
框架执行此操作的选项。 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:我发现这篇文章(遗憾的是它是德语)关于使用
CL_XSLT_PROCESSOR
使用 XPath 查询 XSLT 文件,但这似乎不再起作用,似乎->run( space )
是问题所在,我得到了这个简短的转储信息:
No valid XSLT program supplied
未提供有效的 XSLT 程序
Even while trying with the wrapper class CL_PROXY_XPATH
in the code below, I get the same short dump.即使在下面的代码中尝试使用包装类
CL_PROXY_XPATH
时,我也得到了相同的短转储。
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.
There are two issues in your code:您的代码中有两个问题:
$codeValue3
which results in a No valid XSLT program supplied
short dump.$codeValue3
,这会导致No valid XSLT program supplied
$codeValue3
short dump。<enumeration value="AAC"/>
, which contains an empty "text value" (it doesn't contain the attributes), so the method get_value
returns an empty value.<enumeration value="AAC"/>
包含一个空的“文本值”(它不包含属性),因此方法get_value
返回一个空值。 When it's corrected, both CL_XSLT_PROCESSOR
and CL_PROXY_XPATH
should work fine.更正后,
CL_XSLT_PROCESSOR
和CL_PROXY_XPATH
都应该可以正常工作。
Here is a working code tested in ABAP 7.52, one with CL_PROXY_XPATH
and one with CL_XSLT_PROCESSOR
:这是在 ABAP 7.52 中测试的工作代码,一个带有
CL_PROXY_XPATH
,一个带有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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.