XPATH帮助选择元素

时间:2015-05-15 12:58:56

标签: xml xpath

所以我有这个XML数据库

<uni>
<!-- SUBJECTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
   <subjects>
    <subject code="XYZ111">
      <title>Java for housewives</title>
      <credits>6</credits>
      <laboratories>
          <laboratory num="lab1">
            <date>12-MAR-2009</date>
            <topics>
               <topic>variables</topic>
               <topic>constants</topic>
            </topics>
            <description>Declarations of variables and constants</description>
        </laboratory>
          <laboratory num="lab2">
            <date>12-APR-2009</date>
            <topics>
               <topic>expressions</topic>
            </topics>
            <description>Types of expressions</description>
        </laboratory>
          <laboratory num="lab3">
            <date>12-MAY-2009</date>
            <topics>
               <topic>conditionals</topic>
               <topic>loops</topic>
            </topics>
            <description>Control statements</description>
        </laboratory>
      </laboratories>
    </subject>
    <subject code="XYZ235">
      <title>Database systems for politicians</title>
      <credits>6</credits>
      <laboratories>
        <laboratory num="lab1">
            <date>10-JAN-2001</date>
            <topics>
               <topic>classes</topic>
               <topic>associations</topic>
               <topic>properties</topic>
              </topics>
              <description>Conceptual modeling</description>
        </laboratory>
        <laboratory num="lab2">
            <date>10-JAN-2002</date>
            <topics>
               <topic>relational tables</topic>
               <topic>primary keys</topic>
               <topic>foreign keys</topic>
              </topics>
              <description>Relational data model</description>
        </laboratory>
       </laboratories>
    </subject>
    <subject code="XYZ987">
      <title>Ethics for car dealers</title>
      <credits>6</credits>
    </subject>
    <subject code="XYZ101">
      <title>Programming for mathematicians</title>
      <credits>6000</credits>
      <laboratories>
        <laboratory num="lab1">
            <date>10-JAN-2002</date>
            <topics>
               <topic>abstract algebra</topic>
                </topics>
            <description>Hilbert spaces of programming languages</description>
        </laboratory>
        <laboratory num="lab2">
            <date>13-MAY-2002</date>
            <topics>
               <topic>topology</topic>
                </topics>
            <description>Theory of homothopy</description>
        </laboratory>
       </laboratories>
    </subject>
        <subject code="ZZZ777">
       <title>Cooking</title>
       <credits>5</credits>
       <laboratories/>
        </subject>
    <subject code="ZZZ778">
      <title>Dancing</title>
      <credits>5</credits>
      <laboratories/>
       </subject>
       <subject code="ABC123">
      <title>General theory of everything</title>
      <credits>4</credits>
      <laboratories>
        <laboratory num="lab1">
            <date>20-JAN-2012</date>
            <topics>
               <topic>space</topic>
                </topics>
            <description>Nothing really interesting</description>
        </laboratory>
          </laboratories>
       </subject>
   </subjects>
<!--  ENROLMENTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <enrolments>
      <enrolment snum="d2345" code="ABC123">
        <date>12-DEC-2011</date>
        <status>dropped</status>
        <time>20</time>
        <IP>16.23.234.002</IP>
        <lab-enrolments>
          <laboratory num="lab1"/>
        </lab-enrolments>
      </enrolment>
      <enrolment snum="d2346" code="ABC123">
        <date>12-DEC-2011</date>
        <status>dropped</status>
        <time>2</time>
        <IP>16.23.234.003</IP>
        <lab-enrolments>
          <laboratory num="lab1"/>
        </lab-enrolments>
      </enrolment>
      <enrolment snum="d2347" code="ABC123">
        <date>12-DEC-2011</date>
        <status>dropped</status>
        <time>10</time>
        <IP>16.23.234.003</IP>
      </enrolment>
      <enrolment snum="d2348" code="ABC123">
        <date>12-DEC-2011</date>
        <status>dropped</status>
        <time>20</time>
        <IP>16.23.234.025</IP>
      </enrolment>
      <enrolment snum="d6767" code="ABC123">
        <date>13-DEC-2011</date>
        <status>valid</status>
        <time>5</time>
        <IP>16.23.234.202</IP>
      </enrolment>
      <enrolment snum="d2345" code="ZZZ778">
        <date>14-DEC-2007</date>
        <status>valid</status>
        <time>10</time>
        <IP>16.23.234.402</IP>
      </enrolment>
      <enrolment snum="d6767" code="ZZZ777">
        <date>12-SEP-2005</date>
        <status>valid</status>
        <time>15</time>
        <IP>16.23.234.202</IP>
      </enrolment>
      <enrolment snum="d2330" code="XYZ101">
        <date>01-JAN-2002</date>
        <status>valid</status>
        <time>3</time>
        <IP>16.23.234.203</IP>
        <lab-enrolments>
          <laboratory num="lab1"/>
          <laboratory num="lab2"/>
        </lab-enrolments>

      </enrolment>
      <enrolment snum="d2349" code="XYZ101">
        <date>01-JAN-2002</date>
        <status>valid</status>
        <time>7</time>
        <IP>16.23.234.202</IP>
        <lab-enrolments>
          <laboratory num="lab1"/>
          <laboratory num="lab2"/>
        </lab-enrolments>
      </enrolment>
      <enrolment snum="d2341" code="XYZ101">
        <date>01-JAN-2002</date>
        <status>valid</status>
        <time>6</time>
        <IP>16.23.234.201</IP>
        <lab-enrolments>
          <laboratory num="lab1"/>
        </lab-enrolments>
      </enrolment>
      <enrolment snum="d6767" code="XYZ101">
        <date>01-JAN-2002</date>
        <status>provisional</status>
        <time>5</time>
        <IP>16.23.234.002</IP>
      </enrolment>
      <enrolment snum="d6767" code="XYZ235">
        <date>31-DEC-2000</date>
        <status>provisional</status>
        <time>30</time>
        <IP>16.23.234.602</IP>
      </enrolment>
      </enrolments>
</uni>

我试图找到包含抽象代数实验室主题的标题。到目前为止,这是我能提出的最佳答案,但它找不到匹配项:/ //uni/subjects/subject/laboratories/laboratory/topics/topic[text()="abstract algebra"]

2 个答案:

答案 0 :(得分:3)

你在找这个:

//subject[laboratories/laboratory/topics/topic[.="abstract algebra"]]/title

答案 1 :(得分:2)

您可以尝试这种方式:

//subject[laboratories/laboratory/topics/topic="abstract algebra"]/title