XPpath-parsing leads to zero output

Tags: #<Tag:0x00007f616eca72f0> #<Tag:0x00007f616eca7200> #<Tag:0x00007f616eca7138>

Hi there,
I am having a problem with parsing an xml-document with XPath via http. My document looks like this (output truncated):

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <Device Name="Inverter" NominalPower="5000" Type="Inverter" Serial="XXX" BusAddress="X" NetBiosName="XXX" IpAddress="1.2.3.4" DateTime="2018-07-25T19:15:22">
    <Measurements>
      <Measurement Value="3414.9" Unit="W" Type="AC_Power"/>
      <Measurement Value="1140.4" Unit="W" Type="AC_Power1"/>
      <Measurement Value="1140.2" Unit="W" Type="AC_Power2"/>
      <Measurement Value="1136.5" Unit="W" Type="AC_Power3"/>
      <Measurement Value="49.989" Unit="Hz" Type="AC_Frequency1"/>
      <Measurement Value="49.998" Unit="Hz" Type="AC_Frequency2"/>
      <Measurement Value="49.998" Unit="Hz" Type="AC_Frequency3"/>
      <Measurement Value="49.9" Unit="°C" Type="Temp"/>
      <Measurement Value="4409.3" Unit="W" Type="GridPower"/>
      <Measurement Value="100.0" Unit="%" Type="Derating"/>
    </Measurements>
  </Device>
</root>

I am inetressted in the values of “AC Power” and “GridPower” (don’t ask me why the file has this format), but there must be a mistake in my .items-file. I only get an empty string.

String inv_acpower	"Inverter AC Power [%s]"  {http = "<[http://1.2.3.4/measurements.xml:60000:XPATH(/root/Device/Measurements/Measurement[@Type='AC_Power']/@Value)]"}

When I apply this code to some online testing tools (e.g. https://www.freeformatter.com/xpath-tester.html), the result of my parsing is “Value=3414.9”

Anyone can help?
The closest topic seems this one, but did not give any more hints.

Regards

1 Like

Running this rule returns the Value. Are you sure you installed the XPath transformation service?

rule "Test XPath"
    when
        Item Test_1 received command
    then
    val testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><Device Name=\"Inverter\" NominalPower=\"5000\" Type=\"Inverter\" Serial=\"XXX\" BusAddress=\"X\" NetBiosName=\"XXX\" IpAddress=\"1.2.3.4\" DateTime=\"2018-07-25T19:15:22\"><Measurements><Measurement Value=\"3414.9\" Unit=\"W\" Type=\"AC_Power\"/><Measurement Value=\"1140.4\" Unit=\"W\" Type=\"AC_Power1\"/><Measurement Value=\"1140.2\" Unit=\"W\" Type=\"AC_Power2\"/><Measurement Value=\"1136.5\" Unit=\"W\" Type=\"AC_Power3\"/><Measurement Value=\"49.989\" Unit=\"Hz\" Type=\"AC_Frequency1\"/><Measurement Value=\"49.998\" Unit=\"Hz\" Type=\"AC_Frequency2\"/><Measurement Value=\"49.998\" Unit=\"Hz\" Type=\"AC_Frequency3\"/><Measurement Value=\"49.9\" Unit=\"°C\" Type=\"Temp\"/><Measurement Value=\"4409.3\" Unit=\"W\" Type=\"GridPower\"/><Measurement Value=\"100.0\" Unit=\"%\" Type=\"Derating\"/></Measurements></Device></root>"
    val mytest = transform("XPATH","/root/Device/Measurements/Measurement[@Type='AC_Power']/@Value" , testXML)
    logInfo("XML Test","Result:" + mytest)
end

Hey!
Adapting your rule works fine! Thank you very much!

rule "Test XPath"
    when
        Item inv_acpower received update
    then
    val testXML = inv_acpower.state.toString
    val mytest = transform("XPATH","/root/Device/Measurements/Measurement[@Type='AC_Power']/@Value" , testXML)
    logInfo("XML Test","Result:" + mytest)
end

Output

[ipse.smarthome.model.script.XML Test] - Result:1873.3

Does this mean, I can only use this XPath-tranfsormation within a rule? :thinking:

Regards!

I don’t think so. Do you see anythink related to this transformaiton in the logs?

Maybe setting the logging level of the http binding to TRACE gives some hints.