如何根据元素的value属性显示数据子集

时间:2016-03-30 12:00:42

标签: xml xpath

所以我有以下XML:

<?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>ps11@gmail.com</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>jb21@gmail.com</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>aj31@gmail.com</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>as11@gmail.com</email>
            <address>
                109, West Plaza, Clydebank, G55 8RC
            </address>
            <tel>07000123123</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>er11@yahoo.com</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>taysmith@hotmail.co.uk</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>jw21@gmail.com</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>oj21@hotmail.com</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>ns31@gmail.com</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>bb31@yahoo.com</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>

我想通过特定的supervisor sid来获取所有员工。

到目前为止,我已尝试过这个XPath:

//*[@sid='1']

但是当我想要所有员工时,这给了我这个结果:

<?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>

1 个答案:

答案 0 :(得分:2)

你可以这样做:

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

简要说明:

  • //employee:在XML文档的任何位置找到employee个元素......
  • [supervisor/@sid='1']:...其中直接子元素supervisor的{​​{1}}属性值等于sid