2012-06-19 2 views
2

저는 XPath의 초보자이기 때문에 약간의 도움이 필요합니다. 나는 특정 브랜드의 고유 productType 년대의 전부를 얻는 방법을 알아 내려고 노력하고있어중첩 노드에서 고유 한 XPath 노드 값 가져 오기

<items> 
    <item> 
     <brandName>Brand 1</brandName> 
     <productTypes> 
      <productType>Type 1</productType> 
      <productType>Type 3</productType> 
     </productTypes> 
    </item> 
    <item> 
     <brandName>Brand 1</brandName> 
     <productTypes> 
      <productType>Type 2</productType> 
      <productType>Type 3</productType> 
     </productTypes> 
    </item> 
    <item> 
     <brandName>Brand 2</brandName> 
     <productTypes> 
      <productType>Type 4</productType> 
      <productType>Type 5</productType> 
     </productTypes> 
    </item> 
</items> 

: 나는 XML 파일과 같이 있습니다. 예를 들어 '브랜드 1'에 대한 고유 한 productType은 모두 '유형 1', 유형 2, 유형 3을 출력합니다.

나는 많은 행운도없이 인터넷 검색을 수행했습니다. 어떤 도움을 주시면 감사하겠습니다!

+0

난 당신이 노드 세트 XPath 식에 고유 한 작업을 수행 할 수 있다고 생각하지 않습니다, 당신은 당신이 표현을 구문 분석하는 데 사용하는 어떤 언어로 일부 추가 코드가 필요합니다. – jspboix

답변

3

이 작동 :

(/items/item[brandName='Brand 1']/productTypes/productType)[not(text()=preceding::*)] 

를 작동하는 방법 : 첫 번째 (...)는 한 brandname = '브랜드 1'의 모든 productType 가져옵니다. 이 시점에서 나는 productType 노드리스트를 가지고있다. 이제 노드 텍스트가 현재 노드 앞의 노드에 포함되지 않은 노드를 선택합니다.

파이썬에서 시도 :

n = libxml2dom.parseString(xml) 
[x.textContent for x in n.xpath("(/items/item[brandName='Brand 1']/productTypes/productType)[not(text()=preceding::*)]")] 
>>> [u'Type 1', u'Type 3', u'Type 2'] 
+0

매력처럼 작동했습니다. 감사합니다! –

관련 문제