【问题标题】:How to find nodes which does not have specific child?如何找到没有特定子节点的节点?
【发布时间】:2014-07-21 16:15:58
【问题描述】:

以下 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>

输出: Toyota2

大例子:

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

输出:所有汽车

【问题讨论】:

标签: sql xml oracle xpath oracle11g


【解决方案1】:

这似乎有效:

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

输出:

<manufacturer>Toyota2</manufacturer>

【讨论】:

  • 这个问题是只返回汽车。还有一辆卡车也没有保养。
  • 然后要么改变xml结构,要么使用*而不是car
【解决方案2】:

你可以试试这个 XPath:

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

第二个 XML 示例的输出是:

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

【讨论】:

  • 一个简单的问题:为什么*[maintenances[not(maintenance)]] 返回的东西与*[not(maintenances/maintenance)] 不同?
猜你喜欢
  • 2011-01-13
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多