2016-03-30 19 views
1

Yani şu XML vardır:Bir elemanın değerin niteliğe dayalı olarak veri alt kümesini göstermek için nasıl

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<couriersystem title="System" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="schema.xsd"> 
    <!-- snip --> 
    <employees> 
     <employee eid="1"> 
      <nin>AZ123518D</nin> 
      <firstname>Peter</firstname> 
      <lastname>Smith</lastname> 
      <gender>Male</gender> 
      <dob>1994-02-11</dob> 
      <email>[email protected]</email> 
      <address> 
       119, London Street, Nidrie, F57 8NE 
      </address> 
      <tel>07005748900</tel> 
      <salary>30526</salary> 
      <empbranch bid="1" /> 
      <supervisor sid="1" /> 
     </employee> 
     <employee eid="2"> 
      <nin>CN174869F</nin> 
      <firstname>Jennifer</firstname> 
      <lastname>Black</lastname> 
      <gender>Male</gender> 
      <dob>1984-12-24</dob> 
      <email>[email protected]</email> 
      <address> 
       161, South Road, Nidrie, W79 8WG 
      </address> 
      <tel>07555111222</tel> 
      <salary>40576</salary> 
      <empbranch bid="2" /> 
      <supervisor sid="1" /> 
     </employee> 
     <employee eid="3"> 
      <nin>ET127654M</nin> 
      <firstname>Aaron</firstname> 
      <lastname>Jones</lastname> 
      <gender>Male</gender> 
      <dob>1968-03-15</dob> 
      <email>[email protected]</email> 
      <address> 
       66, High Road, Yoker, Q47 4SR 
      </address> 
      <tel>07856471267</tel> 
      <salary>30526</salary> 
      <empbranch bid="3" /> 
      <supervisor sid="1" /> 
     </employee> 
     <employee eid="4"> 
      <nin>GC765238A</nin> 
      <firstname>Alistair</firstname> 
      <lastname>Smith</lastname> 
      <gender>Male</gender> 
      <dob>1976-11-26</dob> 
      <email>[email protected]</email> 
      <address> 
       109, West Plaza, Clydebank, G55 8RC 
      </address> 
      <tel>0700</tel> 
      <salary>25400</salary> 
      <empbranch bid="4" /> 
      <supervisor sid="1" /> 
     </employee> 
     <employee eid="5"> 
      <nin>HP146854D</nin> 
      <firstname>Emma</firstname> 
      <lastname>Reynolds</lastname> 
      <gender>Male</gender> 
      <dob>1995-05-05</dob> 
      <email>[email protected]</email> 
      <address> 
       57, Scott Street, Aberdeen, O75 2KS 
      </address> 
      <tel>07625361536</tel> 
      <salary>25400</salary> 
      <empbranch bid="5" /> 
      <supervisor sid="7" /> 
     </employee> 
     <employee eid="6"> 
      <nin>JA213465F</nin> 
      <firstname>Taylor</firstname> 
      <lastname>Smith</lastname> 
      <gender>Male</gender> 
      <dob>1983-08-25</dob> 
      <email>[email protected]</email> 
      <address> 
       6, West Place, Perth, F65 9LK 
      </address> 
      <tel>07765665123</tel> 
      <salary>26940</salary> 
      <empbranch bid="6" /> 
      <supervisor sid="7" /> 
     </employee> 
     <employee eid="7"> 
      <nin>PH173957M</nin> 
      <firstname>Jamie</firstname> 
      <lastname>White</lastname> 
      <gender>Male</gender> 
      <dob>1992-07-17</dob> 
      <email>[email protected]</email> 
      <address> 
       85, West Street, BathgateGlasgow, W1 6KH 
      </address> 
      <tel>07649284671</tel> 
      <salary>26940</salary> 
      <empbranch bid="7" /> 
      <supervisor sid="1" /> 
     </employee> 
     <employee eid="8"> 
      <nin>RG185656A</nin> 
      <firstname>Oliver</firstname> 
      <lastname>Jack</lastname> 
      <gender>Male</gender> 
      <dob>1990-09-09</dob> 
      <email>[email protected]</email> 
      <address> 
       173, London Street, Edinburgh, V66 5BI 
      </address> 
      <tel>07987612635</tel> 
      <salary>26940</salary> 
      <empbranch bid="8" /> 
      <supervisor sid="1" /> 
     </employee> 
     <employee eid="9"> 
      <nin>TE199927D</nin> 
      <firstname>Nicole</firstname> 
      <lastname>Summers</lastname> 
      <gender>Male</gender> 
      <dob>1988-01-12</dob> 
      <email>[email protected]</email> 
      <address> 
       104, North Lane, Perth, J3 7WP 
      </address> 
      <tel>07998812671</tel> 
      <salary>26940</salary> 
      <empbranch bid="9" /> 
      <supervisor sid="7" /> 
     </employee> 
     <employee eid="10"> 
      <nin>WC333245F</nin> 
      <firstname>Barney</firstname> 
      <lastname>Brown</lastname> 
      <gender>Male</gender> 
      <dob>1990-10-10</dob> 
      <email>[email protected]</email> 
      <address> 
       173, London Street, Edinburgh, V66 5BI 
      </address> 
      <tel>07111346572</tel> 
      <salary>25400</salary> 
      <empbranch bid="1" /> 
      <supervisor sid="1" /> 
     </employee> 
    </employees> 
    <!-- snip --> 
</couriersystem> 

Ve belirli supervisorsid tarafından tüm çalışanları almak istiyorum.

Şimdiye kadar bu XPath denedim:

//*[@sid='1']

Ama bu bana tüm çalışanları almak istiyorum bu sonucu verir:

<?xml version="1.0" encoding="UTF-8"?> 
<result> 
<supervisor sid="1"/> 
<supervisor sid="1"/> 
<supervisor sid="1"/> 
<supervisor sid="1"/> 
<supervisor sid="1"/> 
<supervisor sid="1"/> 
<supervisor sid="1"/> 
</result> 

cevap

2

Bu yolla da :

//employee[supervisor/@sid='1'] 

kısa bir açıklama:

  • //employee: her yerde XML belgesinde employee unsurları bulmak ...
  • [supervisor/@sid='1']: doğrudan çocuk eleman supervisorsid nitelik değerine sahip olduğu ... '1'
+0

İşleri mükemmel teşekkür ederim eşittir! Bunu açıklamaktan sakıncası var mı? – madcrazydrumma

+1

@madcrazydrumma: Eğer yardım ederseniz, bu cevap [** kabul **] (http://meta.stackoverflow.com/q/5234/234215). Teşekkürler. – kjhughes

+1

@madcrazydrumma, XPath'ın '// * [@ sid =' 1 '] 'anladığınız için sezgisel olduğunu düşündü. Neyse, cevabımı kısa bir açıklama ile güncelledim – har07