如何对一系列元素进行外键查找?

时间:2016-03-29 11:18:22

标签: xml xsd

所以我有当前的XML文档:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="couriersystem.xsl"?>
<couriersystem title="Courier System"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="couriersystem.xsd">
    <branches>
        <branch bid="1">
            <name>Headquarters</name>
            <address>
                58, Main Street, Edinburgh, R79 2LR
            </address>
            <manager mid="1" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="none" />
            </deliverymethods>
        </branch>
        <branch bid="2">
            <name>Convenience Local Branch</name>
            <address>
                118, Renfrew Avenue, Dumfries, J6 8CZ
            </address>
            <manager mid="2" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="bicycle" />
                <method name="car" />
            </deliverymethods>
        </branch>
        <branch bid="3">
            <name>Westwood Shopping Centre</name>
            <address>
                119, London Street, Nidrie, F57 8NE
            </address>
            <manager mid="3" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="bicycle" />
            </deliverymethods>
        </branch>
        <branch bid="4">
            <name>Logistics Warehouse</name>
            <address>
                160, Main Road, Dunfermline, A15 0PO
            </address>
            <manager mid="4" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="car" />
                <method name="van" />
            </deliverymethods>
        </branch>
        <branch bid="5">
            <name>Kingdom of Fife Shopping Centre</name>
            <address>
                92, Central Lane, Dunfermline, U38 2OD
            </address>
            <manager mid="5" />
            <headoffice hid="7" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="bicycle" />
                <method name="car" />
            </deliverymethods>
        </branch>
        <branch bid="6">
            <name>Capital Branch</name>
            <address>
                95, High Street, Inverness, W91 8IW
            </address>
            <manager mid="6" />
            <headoffice hid="7" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="car" />
                <method name="van" />
            </deliverymethods>
        </branch>
        <branch bid="7">
            <name>Glasgow City</name>
            <address>
                94, Harris Place, BathgateGlasgow, E74 2MR
            </address>
            <manager mid="7" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="car" />
            </deliverymethods>
        </branch>
        <branch bid="8">
            <name>Edinburgh Omni Centre</name>
            <address>
                119, West Road, Edinburgh, A79 2EG
            </address>
            <manager mid="8" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="none" />
            </deliverymethods>
        </branch>
        <branch bid="9">
            <name>Royal Plaza</name>
            <address>
                81, Royal Plaza, BathgateGlasgow, U52 7GV
            </address>
            <manager mid="9" />
            <headoffice hid="7" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="bicycle" />
                <method name="car" />
            </deliverymethods>
        </branch>
    </branches>
    <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>
    <customers>
        <customer cid="1">
            <firstname>Sam</firstname>
            <lastname>Mitchell</lastname>
            <gender>Male</gender>
            <dob>1991-12-12</dob>
            <email>sammitch@hotmail.co.uk</email>
            <address>
                161, South Road, Nidrie, W79 8WG
            </address>
            <tel>07811119542</tel>
            <cbranch bid="2" />
        </customer>
        <customer cid="2">
            <firstname>Chris</firstname>
            <lastname>Bernards</lastname>
            <gender>Male</gender>
            <dob>1976-04-05</dob>
            <email>berny5476@gmail.com</email>
            <address>
                179, Park Street, Inverness, Z27 0YU
            </address>
            <tel>07565641232</tel>
            <cbranch bid="3" />
        </customer>
        <customer cid="3">
            <firstname>Markus</firstname>
            <lastname>Imrie</lastname>
            <gender>Male</gender>
            <dob>1995-06-20</dob>
            <email>markus117@gmail.com</email>
            <address>
                49, Ferry Plaza, Dunfermline, D24 1VD
            </address>
            <tel>07712312488</tel>
            <cbranch bid="1" />
        </customer>
        <customer cid="4">
            <firstname>Ryan</firstname>
            <lastname>Murray</lastname>
            <gender>Male</gender>
            <dob>1995-06-23</dob>
            <email>murray_ryan@hotmail.co.uk</email>
            <address>
                186, Skye Avenue, Inverness, K56 0MX
            </address>
            <tel>07508611248</tel>
            <cbranch bid="6" />
        </customer>
        <customer cid="5">
            <firstname>Alice</firstname>
            <lastname>Montgomery</lastname>
            <gender>Female</gender>
            <dob>1985-01-01</dob>
            <email>alicemont@btinternet.com</email>
            <address>
                158, John Road, Clydebank, P23 8DW
            </address>
            <tel>07560002143</tel>
            <cbranch bid="8" />
        </customer>
        <customer cid="6">
            <firstname>Daniel</firstname>
            <lastname>West</lastname>
            <gender>Male</gender>
            <dob>1966-06-06</dob>
            <email>danielwest@gmail.com</email>
            <address>
                94, Regent Avenue, Perth, S34 9TE
            </address>
            <tel>07454651321</tel>
            <cbranch bid="4" />
        </customer>
        <customer cid="7">
            <firstname>George</firstname>
            <lastname>Burnett</lastname>
            <gender>Male</gender>
            <dob>1978-09-06</dob>
            <email>gb205@hotmail.co.uk</email>
            <address>
                76, South Lane, Aberdeen, V28 0TF
            </address>
            <tel>07562537541</tel>
            <cbranch bid="9" />
        </customer>
        <customer cid="8">
            <firstname>Katie</firstname>
            <lastname>Drummond</lastname>
            <gender>Female</gender>
            <dob>1993-11-16</dob>
            <email>katiedrummond@gmail.com</email>
            <address>
                45, West Road, Edinburgh, A79 2EG
            </address>
            <tel>07654546123</tel>
            <cbranch bid="5" />
        </customer>
        <customer cid="9">
            <firstname>Lizzie</firstname>
            <lastname>Alderan</lastname>
            <gender>Female</gender>
            <dob>1976-05-28</dob>
            <email>lizziealderan@gmail.com</email>
            <address>
                49, Ferry Plaza, Dunfermline, D24 1VD
            </address>
            <tel>07651246572</tel>
            <cbranch bid="7" />
        </customer>
        <customer cid="10">
            <firstname>Rebecca</firstname>
            <lastname>Almond</lastname>
            <gender>Female</gender>
            <dob>1997-03-30</dob>
            <email>beckyalmond@gmail.com</email>
            <address>
                13, Harris Place, BathgateGlasgow, E74 2MR
            </address>
            <tel>07876123451</tel>
            <cbranch bid="8" />
        </customer>
    </customers>
    <packages>
        <package pid="1" cid="3">
            <name>ACER Aspire F5-571 Laptop</name>
            <weight>3.5</weight>
            <price>30</price>
            <category></category>
        </package>
        <package pid="2" cid="6">
            <name>Apple iPad Mini</name>
            <weight>0.7</weight>
            <price>20</price>
            <category></category>
        </package>
        <package pid="3" cid="7">
            <name>Black Leather Shoes</name>
            <weight>2</weight>
            <price>5</price>
            <category></category>
        </package>
        <package pid="4" cid="10">
            <name>Swarovski Silver Necklace</name>
            <weight>0.1</weight>
            <price>10</price>
            <category></category>
        </package>
        <package pid="5" cid="8">
            <name>Adele 25 CD</name>
            <weight>0.2</weight>
            <price>3</price>
            <category></category>
        </package>
        <package pid="6" cid="9">
            <name>The Maze Runner Book</name>
            <weight>0.5</weight>
            <price>5</price>
            <category></category>
        </package>
        <package pid="7" cid="1">
            <name>Game of Thrones: The Complete Collection</name>
            <weight>3.1</weight>
            <price>25</price>
            <category></category>
        </package>
        <package pid="8" cid="5">
            <name>Assassins Creed: Syndicate Xbox</name>
            <weight>0.2</weight>
            <price>5</price>
            <category></category>
        </package>
        <package pid="9" cid="4">
            <name>Sony S52 Wireless Speakers</name>
            <weight>0.8</weight>
            <price>10</price>
            <category></category>
        </package>
        <package pid="10" cid="2">
            <name>Reebok ZR10 Treadmill</name>
            <weight>74.2</weight>
            <price>50</price>
            <category></category>
        </package>
    </packages>
</couriersystem>

我只是想从中创建几个xpath表达式。

到目前为止,我正在努力让所有员工按照他们工作的分支机构的顺序。我已经尝试过这个xpath表达式,但我似乎无法正确/工作:

key('bid', $employee/empbranch)/@bid

任何帮助将不胜感激!感谢

编辑:我也试过了,但没有结果:

/couriersystem/employees/employee[/couriersystem/branches/branch/@bid = @empbranch]/@firstname

1 个答案:

答案 0 :(得分:1)

@符号查找属性,但是empbranch是包含@bid属性的元素,与firstname一样。此外,string()调用将采用名字字符串本身。

我认为您正在寻找的表达方式是:

/couriersystem/employees/employee[/couriersystem/branches/branch/@bid = empbranch/@bid]/string(firstname)

至于按分支顺序列出员工,以下XQuery完成工作:

for $branch in /couriersystem/branches/branch
let $employees := /couriersystem/employees/employee[$branch/@bid = empbranch/@bid]/string(firstname)
return <branch bid="{$branch/@bid}">{
  for $employee in $employees
  return <employee>{$employee}</employee>
}</branch>

纯XPath 2.0解决方案(无法创建新节点)将是:

for $branch in /couriersystem/branches/branch
return
/couriersystem/employees/employee[$branch/@bid = empbranch/@bid]/string(firstname)

我不确定密钥查找是否可以使其更快,因为您需要流式传输所有分支机构以及每个分支机构内的所有员工。

我希望这有帮助!