Вопрос

I need to pick a node that its date+timezone element has to be equal of some known date (XPath 1.0), i.e.:

<Root>
  <Houses>
    <House>
      <BuildDate>2001-05-11-06:00</BuildDate>
      <Price>$400,000</Price>
    </House>
    <House>
      <BuildDate>2005-09-01-05:00</BuildDate>
      <Price>$300,000</Price>        
    </House>
    <House>
      <BuildDate>2004-10-11-06:00</BuildDate>
      <Price>$200,000</Price>        
    </House>
  </Houses>
</Root>

I need to pick a Price that its BuildDate is 2005-09-01 (notice the lack of timezone). The input can only be a date (2005-09-01). What I have is:

//Root/Houses/House[BuildDate[text()='2005-09-01']]/Price/text()

However, since the dates have timezones, such comparison will never yield any result. I am running this in SoapUI 4.6.4 using Groovy scripts.

Это было полезно?

Решение

It doesn't look like you can actually use the timezone information meaningfully, because you don't have the time/timezone in your query. You lack information, so you can never get the desired result. You could however compare only the date.

You could check if the queried date is contained inside any of the BuildDate nodes:

//Root/Houses/House[BuildDate[contains(text(),'2005-09-01')]]/Price/text()

or essentially the same

//Root/Houses/House[contains(BuildDate/text(),'2005-09-01')]/Price/text()

Which is perfectly fine XPath 1.0.

Другие советы

Since the dates are text here you can directly do a string contains check. Using XmlSlurper, it would be somethinglike:

def xml = '''
<Root>
  <Houses>
    <House>
      <BuildDate>2001-05-11-06:00</BuildDate>
      <Price>$400,000</Price>
    </House>
    <House>
      <BuildDate>2005-09-01-05:00</BuildDate>
      <Price>$300,000</Price>        
    </House>
    <House>
      <BuildDate>2005-09-01-11:00</BuildDate>
      <Price>$600,000</Price>        
    </House>
    <House>
      <BuildDate>2004-10-11-06:00</BuildDate>
      <Price>$200,000</Price>        
    </House>
  </Houses>
</Root>
'''

def parsed = new XmlSlurper().parseText( xml ) 

def myDate = '2005-09-01'
parsed.Houses.House.findAll { it.BuildDate.text().contains( myDate ) }
                   .Price*.text()

Note:- Added another item with same date but different price.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top