# Xpath problem

**URL:** <https://forums.plex.tv/t/xpath-problem/29444>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [February 19, 2013, 10:16pm UTC](https://forums.plex.tv/t/xpath-problem/29444 "2013-02-19T22:16:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oysteing](https://avatars.discourse-cdn.com/v4/letter/o/dfb087/32.png) [@oysteing](https://forums.plex.tv/u/oysteing)\
**Post date:** [February 19, 2013, 10:16pm UTC](https://forums.plex.tv/t/xpath-problem/29444/1 "2013-02-19T22:16:16Z")

</div>

Hi!  
  
  
  
I recently started developing my first plugin, and so far it has worked out just fine. However, I have a small problem using Xpath. Here’s an explanation:  
  
  
  
I got a xml looking like:

```auto

<br />
<e2event><br />
<e2eventid><br />
57070<br />
</e2eventid><br />
<e2eventstart><br />
1361307600<br />
</e2eventstart><br />
<e2eventduration><br />
4200<br />
</e2eventduration><br />
<e2eventcurrenttime><br />
1361311791<br />
</e2eventcurrenttime><br />
<e2eventtitle><br />
Spartacus: War of the Damned<br />
</e2eventtitle><br />
<e2eventdescription><br />
(2:10) Amerikansk actionserie.<br />
</e2eventdescription><br />
<e2eventdescriptionextended><br />
Spartacus fører an i et dristig angrep for å skaffe mat og ly til sitt folk. Crassus bygger opp en fryktinngytende hær samtidig som han får et uventet besøk.<br />
</e2eventdescriptionextended><br />
<e2eventservicereference><br />
1:0:1:1772:47:46:E080000:0:0:0:<br />
</e2eventservicereference><br />
<e2eventservicename><br />
MAX<br />
</e2eventservicename><br />
</e2event><br />

```

  
  
I'm reading the different values with i.e.   

```auto

<br />
epgdescription = urlHtml.xpath("//e2eventdescription/text()")<br />

```

  
  
This way, I read out all the values, and getting lists, normally with the same amount of strings in it. The problem occurs when the xml sometimes don't fill inn empty values, and I get like this:  
  

```auto

<br />
<e2event><br />
<e2eventid><br />
32537<br />
</e2eventid><br />
<e2eventstart><br />
1361307600<br />
</e2eventstart><br />
<e2eventduration><br />
4800<br />
</e2eventduration><br />
<e2eventcurrenttime><br />
1361311791<br />
</e2eventcurrenttime><br />
<e2eventtitle><br />
24/7: NHL Penguins vs Capitals<br />
</e2eventtitle><br />
<e2eventdescription/><br />
<e2eventdescriptionextended><br />
Nå får du eksklusiv tilgang til hockeyrivalene Pittsburgh Penguins og Washington Capitals når de gjør seg klare til 2011 NHL Winter Classic.<br />
</e2eventdescriptionextended><br />
<e2eventservicereference><br />
1:0:1:196:4:46:E080000:0:0:0:<br />
</e2eventservicereference><br />
<e2eventservicename><br />
Discovery (N)<br />
</e2eventservicename><br />
</e2event><br />

```

  
  
If you look at , there is no value here. My list will then be missing one value, messing up everything, and finally crash my plugin with a "index out of bounds" fault.  
  
Is there a simple solution to be sure we get a string in the list? If there is no value in the XML, i want an empty string in my list. Probably a easy solution, but new to this so... :)  
  
oysteing

---

<div class="post-metadata">

**Author:** ![aclark20](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@aclark20](https://forums.plex.tv/u/aclark20)\
**Post date:** [February 20, 2013, 3:29pm UTC](https://forums.plex.tv/t/xpath-problem/29444/2 "2013-02-20T15:29:48Z")

</div>

Sounds like you’re trying to grab multiple separate lists and then title[3] goes with description[3] etc.  
  
  
  
I’d suggest grabbing the whole event (assuming there are multiple in the full XML). Something like this:

```auto

<br />
events = html.xpath("//e2events")<br />
<br />
for event in events:<br />
	title = event.xpath("./e2eventtitle")<br />
	desc = event.xpath("./e2eventdescription")<br />
	..... do whatever you want with that information<br />

```

  
  
that way everything will remain synched, and you can then add logic to handle blanks if you wanted to give them some default value.  
  

```auto

<br />
if desc == "":<br />
	desc = "No Description Available<br />

```

---

<div class="post-metadata">

**Author:** ![Gerk](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/gerk/32/81980_2.png) [@Gerk](https://forums.plex.tv/u/Gerk)\
**Post date:** [February 20, 2013, 5:45pm UTC](https://forums.plex.tv/t/xpath-problem/29444/3 "2013-02-20T17:45:38Z")

</div>

Yep what aclark20 says ^^^  
  
  
  
Looping through is the best way to handle that sort of stuff.

---

<div class="post-metadata">

**Author:** ![oysteing](https://avatars.discourse-cdn.com/v4/letter/o/dfb087/32.png) [@oysteing](https://forums.plex.tv/u/oysteing)\
**Post date:** [February 20, 2013, 9:59pm UTC](https://forums.plex.tv/t/xpath-problem/29444/4 "2013-02-20T21:59:22Z")

</div>

Thanx!  
  
  
  
Tried it out this way, and it works perfectly. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 20, 2019, 10:56pm UTC](https://forums.plex.tv/t/xpath-problem/29444/5 "2019-12-20T22:56:23Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
