如何找到没有特定孩子的节点?

时间:2014-06-01 11:34:28

标签: sql xml oracle xpath oracle11g

以下xml是我的xml文件的一部分。我需要获得没有维护的汽车制造商的名称。对于小例子,我试过跟随但是它有效但是当汽车数量增加时它不会返回正确的输出:

@xpath /*/descendant::maintenances[not(maintenance)]/preceding::manufacturer/text()

顺便说一句,我也试过这些:

@xpath /descendant::maintenances[not(*)][not(normalize-space())]/preceding::manufacturer/text()


@xpath /descendant::maintenances[not(maintenance)]/preceding::manufacturer/text()

@xpath /descendant::maintenances[not(maintenance())]/preceding::manufacturer/text()

小例子:

<vehicles>
        <car rego="XYZ007">
          <manufacturer>Toyota2</manufacturer>
          <model>Camry</model>
          <capacity>5</capacity>
          <maintenances/>
        </car>
        <car rego="ABC123">
          <manufacturer>Toyota</manufacturer>
          <model>Corolla</model>
          <capacity>4</capacity>
          <maintenances>
            <maintenance enum="m0045">
                <mdate>20-JAN-2012</mdate>
                <parts>
                     <part>air filter</part>
                </parts>
                <description>Replaced air filter</description>
            </maintenance>
          </maintenances>
      </car>
</vehicles>

输出:丰田2

大例子:

   <vehicles>
    <bus rego="PKR856">
      <manufacturer>MAN</manufacturer>
      <capacity>52</capacity>
      <maintenances>
          <maintenance enum="m1234">
            <mdate>12-MAR-2009</mdate>
            <parts>
               <part>tire</part>
            </parts>
            <description>Changed tires</description>
        </maintenance>
      </maintenances>
    </bus>
    <bus rego="UUQ007">
      <manufacturer>Volvo</manufacturer>
      <capacity>60</capacity>
      <maintenances>
        <maintenance mnum="m1234">
            <mdate>10-JAN-2001</mdate>
            <parts>
               <part>air filter</part>
               <part>oil filter</part>
               <part>engine oil</part>
              </parts>
              <description>Replaced engine oil, oil filter, and air filter</description>
        </maintenance>
       </maintenances>
    </bus>
    <bus rego="XYZ987">
      <manufacturer>Volvo</manufacturer>
      <capacity>60</capacity>
    </bus>
    <truck>
      <manufacturer>Volvo</manufacturer>
      <capacity>6000</capacity>
      <maintenances>
        <maintenance enum="m1234">
            <mdate>10-JAN-2011</mdate>
            <parts>
               <part>air filter</part>
              </parts>
            <description>Replaced air filter</description>
        </maintenance>
        <maintenance enum="m1234">
            <mdate>13-MAY-2014</mdate>
            <parts>
               <part>fuel pump</part>
                </parts>
            <description>Replaced fuel pump</description>
        </maintenance>
       </maintenances>
    </truck>
        <truck rego="ZZZ777">
       <manufacturer>Kenworth</manufacturer>
       <capacity>5000</capacity>
           <horsepower>38</horsepower>
       <maintenances/>
        </truck>


    <car rego="XYZ007">
      <manufacturer>Toyota</manufacturer>
      <model>Camry</model>
      <capacity>5</capacity>
      <maintenances/>
    </car>



       <car rego="ABC123">
      <manufacturer>Toyota</manufacturer>
      <model>Corolla</model>
      <capacity>4</capacity>
      <maintenances>
        <maintenance enum="m0045">
            <mdate>20-JAN-2012</mdate>
            <parts>
               <part>air filter</part>
                </parts>
            <description>Replaced air filter</description>
        </maintenance>
          </maintenances>
       </car>
    <limousine rego="VVV001">
      <manufacturer>Maserati</manufacturer>
      <capacity>2</capacity>
       </limousine>
       <limousine rego="PKR856">
      <manufacturer>Rolls Royce</manufacturer>
      <capacity>4</capacity>
      <maintenances>
        <maintenance enum="m0001">
            <mdate>12-JUN-2006</mdate>
            <parts>
               <part>ash tray</part>
                </parts>
            <description>Replaced golden ash tray</description>
        </maintenance>
          </maintenances>
       </limousine>
   </vehicles>

输出所有车辆

2 个答案:

答案 0 :(得分:2)

你可以尝试这个XPath:

/vehicles/*[maintenances[not(maintenance)]]/manufacturer

使用第二个XML示例,输出为:

<manufacturer>Kenworth</manufacturer>
<manufacturer>Toyota</manufacturer>

答案 1 :(得分:1)

这似乎有效:

/vehicles/car[not(maintenances/maintenance)]/manufacturer

输出:

<manufacturer>Toyota2</manufacturer>