2017-02-23 5 views
0

XML 파일에 문제가 있습니다. 여기simpleXML (php)을 사용하여 xpath로 부모 노드를 얻는 방법

그것입니다

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> 
<sheetPr codeName="Feuil3"> 
    <tabColor rgb="FF00B050"/> 
</sheetPr> 
<dimension ref="A18"/> 
<sheetViews> 
    <sheetView tabSelected="1" topLeftCell="A3" workbookViewId="0"> 
     <selection activeCell="A3" sqref="A3"/> 
    </sheetView> 
</sheetViews> 
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/> 
    <cols> 
     <col min="1" max="1" width="29.140625" bestFit="1" customWidth="1"/> 
     <col min="2" max="2" width="24.42578125" bestFit="1" customWidth="1"/> 
     <col min="3" max="3" width="14.28515625" bestFit="1" customWidth="1"/> 
     <col min="4" max="4" width="5.42578125" bestFit="1" customWidth="1"/> 
     <col min="5" max="5" width="6.140625" bestFit="1" customWidth="1"/> 
    </cols> 

<sheetData> 
    <row r="18" ht="16.5" customHeight="1"/> 
</sheetData> 
<sortState ref="A2:E1036"> 
    <sortCondition descending="1" ref="C1"/> 
</sortState> 
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/> 
</worksheet> 
나는이 XPath는 제한이 부모 노드 (행) (그 일)하고 싶은

: 그것은 나에게이를 반환 지금

$row2 = $xml->xpath("//*[local-name()='row']/@*[local-name()='r' and .= '18']"); 

을 :

array(1) { 
    [0]=> 
    object(SimpleXMLElement)#383 (1) { 
    ["@attributes"]=> 
    array(1) { 
     ["r"]=> 
     string(2) "18" 
    } 
    } 
} 

나는 부모님을 갖고 싶습니다. (행)

어떻게해야합니까?

고마워요.

+0

@Andersson 아니요, 작동하지 않습니다. :( –

+0

속성을 선택하고 있습니다. "and"를 사용하여 속성 값을 필터링해야합니다. – Fildor

+0

작동하지 않습니다 : // * –

답변

0

먼저. local-name()을 없애고 네임 스페이스를 무시하지 않으려면 네임 스페이스 접두어를 등록하십시오. 그 후에 그것은 단순한 조건 일뿐입니다.

$worksheet = new SimpleXMLElement($xml); 
$worksheet->registerXpathNamespace(
    'm', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main' 
); 

var_dump(
    $worksheet->xpath('//m:row[@r=18]') 
); 

출력 :

array(1) { 
    [0]=> 
    object(SimpleXMLElement)#2 (1) { 
    ["@attributes"]=> 
    array(3) { 
     ["r"]=> 
     string(2) "18" 
     ["ht"]=> 
     string(4) "16.5" 
     ["customHeight"]=> 
     string(1) "1" 
    } 
    } 
} 

SimpleXMLElement::xpath()는 항상 배열을 반환합니다. 여기에는 몇 가지 또는 전혀 row 요소가있을 수 있습니다. 조건을 사용하여 노드를 반입했는지 또는 루프를 사용하여 배열을 반복하는지 확인하십시오.

이 경우 표현식에는 두 부분으로 구성됩니다. //m:rowrow 요소 노드를 문서에서 가져옵니다. []에는 발견 된 노드에 대한 필터 조건이 있습니다. 이 경우 @id=18, 속성 노드 id18과 같아야합니다.