本文介绍了如何使用xpath基于属性显示特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对于xml文件(1.xml) <?xml version =1.0encoding =UTF-8?> < xsl:stylesheet version =1.0 xmlns:xsl =http://www.w3.org/1999/XSL/Transform> < traceCollecFile xmlns =http://www.3gpp.org/ftp/specs/archive/32_series/32.423#traceData> < fileHeader fileFormatVersion =32.423 V10 .0vendorName =CDOT GROUP> < fileSender elementDn =DC = a1.companycdot.com,SubNetwork = 1,ManagedElement = MME-1_1_1elementType =MME/> ; < traceRecSession dnPrefix =DC = a1.companycdot.com,SubNetwork = 1traceRecSessionRef =992stime =2018-06-08T13:47 :12-05:30> < ue idType =IMSIidValue =311480000002255/> < msg function =S6name =UPDATE LOCATION ANSWERchangeTime =0.000000vendorSpecific =false> < ie name =RESULT_CODE> 2001 < traceSessionRef> < mcc> 311 < mnc> 480 < trace_id> 8323073 < traceRecSession dnPrefix =DC = a1.companycdot.com,SubNetwork = 1traceRecSessionRef =992stime =2018-06- 08T13:47:12-05:30> < ue idType =IMSIidValue =311480000002255/> < msg function = S11name =CREATE SESSION REQUESTchangeTime =0.000000vendorSpecific =false> < ie name =GTP_V2_IE_IMSI> 311480000002255 <即name =GTP_V2_IE_RAT_TYPE> 6 < ie name =GTP_V2_IE_MSISDN> 913114802255 < ieGroup name =GTP_V2_IE_FQ_CSID> < ie name =INSTANCE> 0 < ie name =GTP_V2_IE_FQ_CSID> 1 < ieGroup name =GTP_V2_IE_BEARER_CNTXT> < ie name =INSTANCE> 0 < ie name =GTP_V2_IE_EBI> 5 < ieGroup name =GTP_V2_IE_BEARER_QOS> < ie name =ARP> 92 < ie name =QCI > 9 < ie name =UPLINK_MBR> 0 < ie name =DOWNLINK_MBR> 0 < ie name =UPLINK_GBR> 0 < ie name =DOWNLINK_GBR> 0 < ie name =GTP_V2_IE_APN> vzwinternet.mnc480.mcc311.gprs < traceSessionRef> < mcc> 311 < mnc> 480 < trace_id> 8323073 我想只提取MSISDN值。 我尝试过: <?php $ doc = new DOMDocument(); $ doc-> load('tce_sgw.xml'); $ xpath = new DOMXPath($ doc); //如果你想读取确切的属性 //选择所有ie元素有一个name属性,其值为GTP_V2_IE_MSISDN。 $ ienameAttribute = $ xpath-> query(// traceCollecFile / traceRecSession / msg / ie [@ name ='GTP_V2_IE_MSISDN'] ); if($ ienameAttribute-> length> 0){ echo $ ienameAttribute-> item(0) - > value; } 解决方案 doc = new DOMDocument(); doc-> load('tce_sgw.xml'); xpath = new DOMXPath( For the xml file (1.xml)<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><traceCollecFile xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.423#traceData" ><fileHeader fileFormatVersion="32.423 V10.0" vendorName="CDOT GROUP" ><fileSender elementDn="DC=a1.companycdot.com,SubNetwork=1, ManagedElement=MME-1_1_1" elementType="MME" /><traceRecSession dnPrefix="DC=a1.companycdot.com,SubNetwork=1" traceRecSessionRef="992" stime="2018-06-08T13:47:12-05:30" ><ue idType="IMSI" idValue="311480000002255" /><msg function="S6" name="UPDATE LOCATION ANSWER" changeTime="0.000000" vendorSpecific="false" ><ie name="RESULT_CODE">2001<traceSessionRef ><mcc>311<mnc>480<trace_id>8323073<traceRecSession dnPrefix="DC=a1.companycdot.com,SubNetwork=1" traceRecSessionRef="992" stime="2018-06-08T13:47:12-05:30" ><ue idType="IMSI" idValue="311480000002255" /><msg function="S11" name="CREATE SESSION REQUEST" changeTime="0.000000" vendorSpecific="false" ><ie name="GTP_V2_IE_IMSI">311480000002255<ie name="GTP_V2_IE_RAT_TYPE">6<ie name="GTP_V2_IE_MSISDN">913114802255<ieGroup name="GTP_V2_IE_FQ_CSID" ><ie name="INSTANCE">0<ie name="GTP_V2_IE_FQ_CSID">1<ieGroup name="GTP_V2_IE_BEARER_CNTXT" ><ie name="INSTANCE">0<ie name="GTP_V2_IE_EBI">5<ieGroup name="GTP_V2_IE_BEARER_QOS" ><ie name="ARP">92<ie name="QCI">9<ie name="UPLINK_MBR">0<ie name="DOWNLINK_MBR">0<ie name="UPLINK_GBR">0<ie name="DOWNLINK_GBR">0<ie name="GTP_V2_IE_APN">vzwinternet.mnc480.mcc311.gprs<traceSessionRef ><mcc>311<mnc>480<trace_id>8323073 I want to extract only the MSISDN values.What I have tried:<?php$doc = new DOMDocument();$doc->load('tce_sgw.xml');$xpath = new DOMXPath($doc);// If you want to read exact attribute//Selects all the ie elements that have a "name" attribute with a value of "GTP_V2_IE_MSISDN". $ienameAttribute = $xpath->query("//traceCollecFile/traceRecSession/msg/ie[@name='GTP_V2_IE_MSISDN']"); if ($ienameAttribute->length > 0) { echo $ienameAttribute->item(0)->value; } 解决方案 doc = new DOMDocument();doc->load('tce_sgw.xml');xpath = new DOMXPath( 这篇关于如何使用xpath基于属性显示特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-27 22:48