Xpath transformation with date pattern

Tags: #<Tag:0x00007f617de5f638> #<Tag:0x00007f617de5f188> #<Tag:0x00007f617de5eaa8>

Hi

I’m trying to access some values from a remote xml. And the XPath expression is not working with the predefined date parameter.

These are my files:

Number probLluvia	"[%d]" {http="<[http://www.aemet.es/xml/municipios/localidad_12040.xml:100000:XPATH(/root/prediccion/dia[4]/prob_precipitacion[@periodo='00-24'])]"}
Number probLluviaX	"[%d]" {http="<[http://www.aemet.es/xml/municipios/localidad_12040.xml:15000:XPATH(/root/prediccion/dia[@fecha='%1$tY-%1$tm-%1$td']/prob_precipitacion[@periodo='00-24'])]"}
Number probLluviaXTest	"%1$tY-%1$tm-%1$td [%s]" {http="<[http://www.aemet.es/xml/municipios/localidad_12040.xml:15000:XPATH(/root/prediccion/dia[@fecha='2018-01-02']/prob_precipitacion[@periodo='00-24'])]"}

The second one is the one I want to make work, the other two are tests to check my syntax.
In the first one it works but, i have to access by index which is not reliable because could be another day data.
In the the third item, I access harcoding the date and it also work.

I print logs on the state of these item in a rule:

logInfo("lluvia","Lluvia: " + probLluvia.state.toString)  
logInfo("lluvia","LluviaFecha: " + probLluviaX.state.toString)  
logInfo("lluvia","LluviaFechaTest: " + probLluviaXTest.state.toString)

and these are the logs I get:

    [INFO ] [clipse.smarthome.model.script.lluvia] - Lluvia: 5
    [INFO ] [clipse.smarthome.model.script.lluvia] - LluviaFecha: NULL
    [INFO ] [clipse.smarthome.model.script.lluvia] - LluviaFechaTest: 5

Any ideas what I’m doing wrong? Any help would be apreciated.

Thank you


*(If you test this in the future be sure to change the hardcoded values)

Hi,
This might not be the most elegant solution, but you could perhaps use a rule to retrieve the value you want from the XML by some dirty stuff like below. You would of cause need to read the date you want to query from somewhere, but that should be quite easy.

        // setting the date to 2018-01-02
        val DateTime datetime = new DateTime(2018,1,2,0,0)
        // creating a string of that date with the correct format
        val String textDateTime = datetime.toString("yyyy-MM-dd")

        // constructing yourXPATH
        val String yourXPATH = String::format("/root/prediccion/dia[@fecha='%s']/prob_precipitacion[@periodo='00-24']",textDateTime)
        //getting the XML
        val String yourXML = sendHttpGetRequest("http://www.aemet.es/xml/municipios/localidad_12040.xml")
        // Do the transformation 
        val Number probLluviaXTest = transform("XPATH",yourXPATH,yourXML)


        logInfo("XPATH",yourXPATH)
        logInfo("XPATH RESULT",probLluviaXTest)

The result is

2017-12-30 16:04:12.740 [INFO ] [eclipse.smarthome.model.script.XPATH] - /root/prediccion/dia[@fecha='2018-01-02']/prob_precipitacion[@periodo='00-24']
2017-12-30 16:04:12.743 [INFO ] [.smarthome.model.script.XPATH RESULT] - 5

It totally works.

Thank you very much. :slight_smile:

You’re welcome. What goes around, comes around :+1: