2014-10-17 1 views
0

안녕하세요 저는 그 안에 "123abc"라는 값을 가진 요소 A를 얻고 싶습니다. 나는 둘 다 시도했지만 실패했다.특정 값을 가진 그랜드 손자 노드가있는 노드를 선택하는 방법

// 패키지/A [A/B/C /. = 123ABC]

// 패키지/A [포함 (A/B/C, 123ABC)]

내가이를 반환 할 :

<A> 
    <System mtm="8742" os="Windows XP" oslang="en" /> 
    <System mtm="2055" os="Windows XP" oslang="jp" /> 
    <A> 
     <B> 
      <C>123abc</C> 
      <C>789</C> 
      <C>567</C> 
     </B> 
    </A> 
    </A> 

샘플 XML 쿼리에 실행 :

<?xml version="1.0" encoding="UTF-8"?> 
<Database version="300"> 
<Package> 
<A> 
    <System mtm="8742" os="Windows XP" oslang="en" /> 
    <System mtm="2055" os="Windows XP" oslang="jp" /> 
    <A> 
     <B> 
      <C>123abc</C> 
      <C>789</C> 
      <C>567</C> 
     </B> 
    </A> 
</A> 
</Package> 
<Package> 
<A> 
    <System mtm="8742" os="Windows XP" oslang="en" /> 
    <System mtm="2055" os="Windows XP" oslang="jp" /> 
    <A> 
     <B> 
      <C>efg123</C> 
      <C>789</C> 
      <C>567</C> 
     </B> 
    </A> 
</A> 
</Package> 
</Database> 

당신은 여기에 답을 테스트 할 수 http://www.freeformatter.com/xpath-tester.html#ad-output

처리하려고 시도한 실제 XML이 추가되었지만 작동하지 않습니다. 특수 문자를 이스케이프 처리 할 수 ​​있습니까? 내가 프로세스에 노력하고

실제 XML은 내가

// TableSection/SectionItem [SectionItem/세포/

을 시도, 다음과 같습니다. = 「00-18-E7-17-48-64」]

// TableSection/SectionItem [포함가 (SectionItem/셀, 「00-18-E7-17-48-64」)]

 <TableSection name="SNMP Devices" IsTreeFormat="true"> 
     <SectionProperties> 
     <Column id="1" Name="IP Address" /> 
     <Column id="2" Name="Description" /> 
     </SectionProperties> 
     <SectionItem> 
     <Cell columnid="1"> 
     192.168.99.54 
     </Cell> 
     <Cell columnid="2"> 
     WMI XScan 
     </Cell> 
     <SectionItem> 
     <Cell columnid="1"> 
      ScanType 
     </Cell> 
     <Cell columnid="2"> 
      WMIScan 
     </Cell> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
      Device description 
     </Cell> 
     <Cell columnid="2"> 
      WMI dscription 
     </Cell> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
      MACAddress 
     </Cell> 
     <Cell columnid="2"> 
      00-18-E7-17-48-64 
     </Cell> 
     </SectionItem> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
     192.168.99.55 
     </Cell> 
     <Cell columnid="2"> 
     WMI XScan 
     </Cell> 
     <SectionItem> 
     <Cell columnid="1"> 
      ScanType 
     </Cell> 
     <Cell columnid="2"> 
      WMIScan 
     </Cell> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
      Device description 
     </Cell> 
     <Cell columnid="2"> 
      WMI dscription 
     </Cell> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
      MACAddress 
     </Cell> 
     <Cell columnid="2"> 
      90-2B-34-64-16-9D 
     </Cell> 
     </SectionItem> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
     192.168.99.107 
     </Cell> 
     <Cell columnid="2"> 
     VMWare : &quot;navvms08.Crest.local&quot; 
     </Cell> 
     <SectionItem> 
     <Cell columnid="1"> 
      MACAddress 
     </Cell> 
     <Cell columnid="2"> 
      00-07-E9-0D-05-C5 
     </Cell> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
      Device identifier 
     </Cell> 
     <Cell columnid="2"> 
      1.3.6.1.4.1.6876.4.1 
     </Cell> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
      Device name 
     </Cell> 
     <Cell columnid="2"> 
      &quot;navvms08.Crest.local&quot; 
     </Cell> 
     </SectionItem> 
     <SectionItem> 
     <Cell columnid="1"> 
      Device description 
     </Cell> 
     <Cell columnid="2"> 
      &quot;VMware ESXi 5.5.0 build-1623387 VMware, Inc. x86_64&quot; 
     </Cell> 
     </SectionItem> 
     </SectionItem> 
     </TableSection> 

답변

1

원본 샘플에서는 비교 문자열을 인용해야합니다. 이러한 작업은 모두 :

//Package/A[A/B/C/. = "123abc"] 

//Package/A[contains(A/B/C,"123abc")] 

당신이에 대해 실행중인 실제 XML에 대한 쿼리를해야합니다 :

//TableSection/SectionItem[SectionItem/Cell[contains(.,"00-18-E7-17-48-64")]] 

문제와 :

//TableSection/SectionItem[SectionItem/Cell/. = "00-18-E7-17-48-64"] 

가 그 것이다 텍스트 내용은 정확히 일치해야하며 XML에는 앞뒤 공백이 있어야합니다. 와

문제 :

//TableSection/SectionItem[contains(SectionItem/Cell,"00-18-E7-17-48-64")] 

contains()은 그 안에 텍스트를 찾을 수 //TableSection/SectionItem 대신 SectionItem/Cell 어떤 의 내 첫 SectionItem/Cell에 보이는 것입니다.

+0

감사 폴. 나는 아직도 쿼리를 작동시킬 수 없기 때문에 원래의 질문을 처리하려고 시도하고있는 실제 xml을 추가했다. 많은 사람들이 나를 위해 이것을 해결할 수 있다면 고맙게 생각합니다. – Terry

0

첫 번째 예에서는 비교 대상 문자열을 인용하지 않았습니다. 어쨌든 문자열 비교에 노드 원자화 규칙이 적용되므로 추가 /. 표현식이 필요하지 않습니다.

XPath //Package/A[A/B/C = "123abc"]이면 충분합니다.

C의 텍스트 콘텐츠와 부분 일치를 수행하려면 //Package/A[A/B/C/contains(., "123abc")]을 사용할 수 있습니다.이 .A 요소를 찾으려면 123abc 텍스트가 포함 된 C 그랜드 터 번 요소가 있어야합니다. .은 현재 컨텍스트를 가리키며 여기에는 개별 C 요소가 있습니다. 그렇지 않은 경우 A/B/contains(C, "123abc")을 입력하면 C 개의 요소가 여러 개 있기 때문에 유형 오류가 발생하고 fn:contains은 시퀀스에서 작동 할 수 없습니다. 두 번째 예를 들어


은 포함하지 않고 예는 공백 문제, 당신은 그러나 당신이 시퀀스에서 작동하도록 시도를 포함, contains를 사용하여이 문제를 해결할 수 있습니다. 대신 다음과 같이 다시 작성해야합니다.

//TableSection/SectionItem[SectionItem/Cell/contains(., "00-18-E7-17-48-64")] 
관련 문제