Xpath解析问题

时间:2015-11-11 09:18:45

标签: xml xpath xml-parsing

我试图找到正确的xPath字符串来解析这个XML示例。目前我无法得到它,因为自我关闭'元素。

这是XML i-m处理的一部分。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Body>
      <getRecordsResponse xmlns="http://www.dummy.com">
         <getRecordsResult>
            <business_duration/>
            <calculation_complete>1</calculation_complete>
            <definition>0050aedb58064100ad1a7198c9974545</definition>
            <duration/>
            <end/>
            <field/>
            <field_value>PRODUKTIE</field_value>
            <id>b5f520716fae82001c41df971e3ee4e3</id>
            <start/>
            <sys_created_by>system</sys_created_by>
            <sys_created_on>2015-09-17 13:21:36</sys_created_on>
            <sys_id>0006a479c8a68200a7ffccf9faada3ce</sys_id>
            <sys_mod_count>0</sys_mod_count>
            <sys_updated_by>system</sys_updated_by>
            <sys_updated_on>2015-09-17 13:21:36</sys_updated_on>
            <table>cmdb_ci_db_ora_instance</table>
            <u_created_on_last_7_days>0</u_created_on_last_7_days>
            <u_old_value/>
            <u_uummss>152136</u_uummss>
            <u_yyyymmdd>20150917</u_yyyymmdd>
            <value>PRODUKTIE</value>
         </getRecordsResult>
      </getRecordsResponse>      
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

问题在于&#34; <u_old_value/>&#34;例如,其中没有字符串。

我想从XML中接收所有带或不带值的元素名称。所以我可以用它执行下一个任务。

像这样:

                business_duration = NULL
                calculation_complete = 1
                definition = 0050aedb58064100ad1a7198c9974545
                duration = NULL
                end = NULL
                field = NULL
                field_value = PRODUKTIE
                id = b5f520716fae82001c41df971e3ee4e3
                start = NULL
                sys_created_by = system
                sys_created_on = 2015-09-17 13:21:36
                sys_id = 0006a479c8a68200a7ffccf9faada3ce
                sys_mod_count = 0
                sys_updated_by = system
                sys_updated_on = 2015-09-17 13:21:36
                table = cmdb_ci_db_ora_instance
                u_created_on_last_7_days = 0
                u_old_value = NULL
                u_uummss = 152136
                u_yyyymmdd = 20150917
                value = PRODUKTIE

2 个答案:

答案 0 :(得分:0)

我找到了解决办法!

我需要的xPath是'/*/*/*/*'

答案 1 :(得分:0)

你在这里(XPath 2.0)

//getRecordsResult/*/concat(local-name(), '=', if(empty(text())) then 'Null' else string(text()))

在跳过命名空间

的情况下
 //*[local-name()='getRecordsResult']/*/concat(local-name(), '=', if(empty(text())) then 'Null' else string(text()))

结果将是

business_duration=Null
calculation_complete=1
definition=0050aedb58064100ad1a7198c9974545
duration=Null
end=Null
field=Null
field_value=PRODUKTIE
id=b5f520716fae82001c41df971e3ee4e3
start=Null
sys_created_by=system
sys_created_on=2015-09-17 13:21:36
sys_id=0006a479c8a68200a7ffccf9faada3ce
sys_mod_count=0
sys_updated_by=system
sys_updated_on=2015-09-17 13:21:36
table=cmdb_ci_db_ora_instance
u_created_on_last_7_days=0
u_old_value=Null
u_uummss=152136
u_yyyymmdd=20150917
value=PRODUKTIE