Xpath transformation match logic/expression

Tags: #<Tag:0x00007f6165b284a0>

Hi there,

I have an issue with an XPATH transformation that I can’t solve. The xpath transformation doesn’t match any element while I’ve tested with an online validator (and applied my own common sense). Probably something specific to the Openhab path transformation that I’m missing.

Input xml:

<CurrentURIMetaData>
 <?xml version="1.0" encoding="UTF-8"?>
 <DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:song="www.wiimu.com/song/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
   <upnp:class>object.item.audioItem.musicTrack</upnp:class>
   <item id="0">
     <song:subid>Continuum</song:subid>
     <song:description>un_known</song:description>
     <song:skiplimit>0</song:skiplimit>
     <song:id>0</song:id>
     <song:like>0</song:like>
     <song:singerid>0</song:singerid>
     <song:albumid>0</song:albumid>
     <res protocolInfo="http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;" duration="00:03:21.000">spotify:track:5imShWWzwqfAJ9gXFpGAQh</res>
     <dc:title>Waiting On the World to Change</dc:title>
     <dc:creator>John Mayer</dc:creator>
     <upnp:artist>John Mayer</upnp:artist>
     <upnp:album>Continuum</upnp:album> 
     <upnp:albumArtURI>http://i.scdn.co/image/ab67616d0000b2737af5fdc5ef048a68db62b85f</upnp:albumArtURI>
   </item>
 </DIDL-Lite> 
</CurrentURIMetaData>

The following rule triggers the xpath transformation:

rule "begin your execution"
when
   Item media_upnp_mediainfoxml changed
then
   val track_name = transform("XPATH", "/*[name()='CurrentURIMetaData']/*[name()='DIDL-Lite' and namespace-uri()='urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/']/*[name()='item' and namespace-uri()='urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/']/*[name()='dc:title' and namespace-uri()='http://purl.org/dc/elements/1.1/']/text()", 
                                    media_upnp_mediainfoxml.state.toString ) 
    media_upnp_trackname.postUpdate(track_name)
end

Log output:
Formatting of the output is terrible, basically the log states that pre-updae value is same as post-update value from which I conclude that the xpath didn’t match

Well, the whole post formatting is terrible :wink: SCNR

Using code fences does help a lot:

<CurrentURIMetaData>
	<?xml version="1.0" encoding="UTF-8"?>
	<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:song="www.wiimu.com/song/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
		<upnp:class>object.item.audioItem.musicTrack</upnp:class>
		<item id="0">
			<song:subid>Continuum</song:subid>
			<song:description>un_known</song:description>
			<song:skiplimit>0</song:skiplimit>
			<song:id>0</song:id>
			<song:like>0</song:like>
			<song:singerid>0</song:singerid>
			<song:albumid>0</song:albumid>
			<res protocolInfo="http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;" duration="00:03:21.000">spotify:track:5imShWWzwqfAJ9gXFpGAQh</res>
			<dc:title>Waiting On the World to Change</dc:title>
			<dc:creator>John Mayer</dc:creator>
			<upnp:artist>John Mayer</upnp:artist>
			<upnp:album>Continuum</upnp:album>
			<upnp:albumArtURI>http://i.scdn.co/image/ab67616d0000b2737af5fdc5ef048a68db62b85f</upnp:albumArtURI>
		</item>
	</DIDL-Lite>
</CurrentURIMetaData>

and the rule:

rule "begin your execution"
when
    Item media_upnp_mediainfoxml changed
then
    val track_name = transform("XPATH", 
	"/[name()='CurrentURIMetaData']/[name()='DIDL-Lite' and namespace-uri()='urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/']/[name()='item' and namespace-uri()='urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/']/[name()='dc:title' and namespace-uri()='http://purl.org/dc/elements/1.1/']/text()",
	media_upnp_mediainfoxml.state.toString)
    media_upnp_trackname.postUpdate(track_name)
end

The xpath expression doesn’t look correct to me, but I maybe wrong here.

HI Udo,

Sorry, kind of new to the community and didn’t go over posting guidelines. I was looking for the code fence option in the editor but couldn’t find it. Found them now and fixed my first post. Increases readability a lot :wink: Thank for reminding me.

Given the XML from the first message, the following xpath expressions are tested using https://www.freeformatter.com/xpath-tester.html (the one mentioned on the xpath transformation page):

The initial one that just follows the document structure results in 'Text=‘Waiting On the World to Change’

/*[name()='CurrentURIMetaData']
/*[name()='DIDL-Lite' ]
/*[name()='item']
/*[name()='dc:title']
/text()

A second one that includes the various namespaces used, also results in 'Text=‘Waiting On the World to Change’

/*[name()='CurrentURIMetaData']
/*[name()='DIDL-Lite' and namespace-uri()='urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/']
/*[name()='item' and namespace-uri()='urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/']
/*[name()='dc:title' and namespace-uri()='http://purl.org/dc/elements/1.1/']
/text()

What do you believe is wrong with the shown xpath? I’ve been going at this for a while now and can’t figure it out.

Hi Chris!

I have the same problem with the xpath transformer and xmlns.
Did you find a way to get this running?

Thanks in advance!
Christoph