遇到XPATH问题

时间:2015-08-25 14:53:08

标签: xml parsing xpath

这是我读过的XML:

    <?xml version="1.0" encoding="UTF-8"?>

<ns10:UtilityData xmlns:ns10="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message" xmlns="http://www.newyorkfed.org/xml/schemas/RateBase/utility" xmlns:ns11="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/cross" xmlns:ns12="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/compact" xmlns:ns13="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/structure" xmlns:ns14="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/utility" xmlns:ns15="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/query" xmlns:ns16="http://rpc.xml.coldfusion" xmlns:ns2="http://www.newyorkfed.org/xml/schemas/FFBase/utility" xmlns:ns3="http://www.newyorkfed.org/xml/schemas/FF/utility" xmlns:ns4="http://www.newyorkfed.org/xml/schemas/FFMetadata/utility" xmlns:ns5="http://www.newyorkfed.org/xml/schemas/FFMethod/utility" xmlns:ns6="http://www.newyorkfed.org/xml/schemas/FFRange/utility" xmlns:ns7="http://www.newyorkfed.org/xml/schemas/FFTarget/utility" xmlns:ns8="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/common" xmlns:ns9="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/generic">  
  <ns10:Header>...</ns10:Header>  
  <DataSet> 
    <Series xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" AVAILABILITY="A" DECIMALS="2" DISCLAIMER="G" FF_METHOD="D" TIME_FORMAT="P1D" xsi:type="ns5:SeriesType">  
      <ns6:Key> 
        <FREQ>D</FREQ>  
        <RATE>FF</RATE>  
        <MATURITY>O</MATURITY>  
        <ns2:FF_SCOPE>D</ns2:FF_SCOPE> 
      </ns6:Key>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-24</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-21</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-20</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs LAST_DAY_OF_MAINTENANCE_PERIOD="true" OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-19</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-18</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-17</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-14</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-13</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-12</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-11</TIME_PERIOD>  
        <OBS_VALUE>0.15</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-10</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-07</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-06</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs LAST_DAY_OF_MAINTENANCE_PERIOD="true" OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-05</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-04</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-08-03</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-31</TIME_PERIOD>  
        <OBS_VALUE>0.08</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-30</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-29</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-28</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-27</TIME_PERIOD>  
        <OBS_VALUE>0.14</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-24</TIME_PERIOD>  
        <OBS_VALUE>0.13</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-23</TIME_PERIOD>  
        <OBS_VALUE>0.13</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs LAST_DAY_OF_MAINTENANCE_PERIOD="true" OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-22</TIME_PERIOD>  
        <OBS_VALUE>0.13</OBS_VALUE> 
      </ns3:Obs>  
      <ns3:Obs OBS_CONF="F" OBS_STATUS="A"> 
        <TIME_PERIOD>2015-07-21</TIME_PERIOD>  
        <OBS_VALUE>0.13</OBS_VALUE> 
      </ns3:Obs> 
    </Series> 
  </DataSet> 
</ns10:UtilityData>

我想要的是&#34; TIME_PERIOD&#34;和&#34; OBS_VALUE&#34;,表示日期和值(如图所示)。

我一直在使用在线XPATH测试人员并且在执行&#39; / ns10:UtilityData / DataSet&#39;时没有得到任何结果。 - 我假设将返回下面的整个结构。

正确的XPATH应该是什么样的?

1 个答案:

答案 0 :(得分:1)

尝试使用namespace-uri指定命名空间:

//*[local-name()='Obs' and namespace-uri()='http://www.newyorkfed.org/xml/schemas/FF/utility']