Topic
  • 5 replies
  • Latest Post - ‏2010-11-26T19:44:11Z by kyroking
kyroking
kyroking
4 Posts

Pinned topic return even if empty

‏2010-11-26T04:44:57Z |
I have a problem with a xpath where if a value is empty, it skips it, throwing my data out of order.. like so:
consider this, first name and age.

John 23
Joseph 35
Jan
Julie 28

I can get the first names because they are always there, however, some gaes might be blank, and when they are blank, their is no place holder, the whole element is missing.

so instead of the above example I get this:

John 23
Joseph 35
Jan 28
Julie

I need to keep it in order with the names, any idea how I can over come this.. this is not xml btw, but html.

Thanks!
Updated on 2010-11-26T19:44:11Z at 2010-11-26T19:44:11Z by kyroking
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: return even if empty

    ‏2010-11-26T08:38:08Z  
    Show us your code, and then we can tell you how to fix it.

    You should always try to reduce the problem to a simple example of a source document and query/stylesheet that anyone can run for themselves.
  • kyroking
    kyroking
    4 Posts

    Re: return even if empty

    ‏2010-11-26T08:53:11Z  
    Show us your code, and then we can tell you how to fix it.

    You should always try to reduce the problem to a simple example of a source document and query/stylesheet that anyone can run for themselves.
    
    //div[@class='myBox2' and position()>0]/table[@id='data']//tr[2]/td/table//tr[2]/td[4]
    


    the above is my xpath, below is a sample of what im trying to run it on, i need the age, but if the age isnt there the td is missing, I need to just return a null or blank value like in my example from the first post.
    
    <div class=
    "myBox2" style=
    "padding: 10px; font-size: 10px;"> <table cellpadding=
    "5" border=
    "0" width=
    "100%" id=
    "data"> <tbody><tr> <td width=
    "11%" rowspan=
    "3"><img height=
    "135" border=
    "0" width=
    "84" style=
    "margin-top: -5px;" src=
    "showfile.php?id=918730"></td> <!-- <td width=
    "11%" rowspan=
    "3"><img src=
    "images/FOUSTMARK_32542_07-01-1955_Front.jpg" width=
    "84" height=
    "135" border=
    "0" style=
    "margin-top:-5px" /></td> --> <td height=
    "51" bgcolor=
    "#cad6ec" width=
    "29%"><span class=
    "redbold">ATKINS, ROBERT JOSEPH</span></td> <td bgcolor=
    "#cad6ec" width=
    "16%"><span class=
    "redbold">D.O.B.</span><br>28-APR-81</td> <td bgcolor=
    "#cad6ec" width=
    "33%"><span class=
    "redbold">Address</span><br>1104  STONY POINT   </td> <td bgcolor=
    "#cad6ec" width=
    "11%"><span class=
    "redbold">Race:</span><br>A</td> </tr> <tr> <td colspan=
    "4"><table cellspacing=
    "5" cellpadding=
    "0" width=
    "100%"> <tbody><tr> <th width=
    "64" scope=
    "col" class=
    "redbold">Vendor #</th> <th width=
    "164" scope=
    "col" class=
    "redbold">Document Type</th> <th width=
    "150" scope=
    "col" class=
    "redbold">Notes</th> <th width=
    "306" scope=
    "col" class=
    "redbold">Age</th> </tr> <tr> <td>932500</td> <td>VOP (GS)</td> <td>None</td> <td>26</td> </tr>
    
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: return even if empty

    ‏2010-11-26T09:43:00Z  
    If you're in XPath 2.0, replace the
    
    td[4]
    
    at the end of the path with
    
    (td[4], 
    "")[1]
    
    . If you're in XPath 1.0, then I think that whatever code calls this XPath expression will need to check the result to see if it is empty.

    You didn't do what I asked, which is to show a complete working sample. You haven't shown how this XPath expression was invoked or how the output table was constructed. If you want help, you are going to have to be more open.
  • kyroking
    kyroking
    4 Posts

    Re: return even if empty

    ‏2010-11-26T18:19:25Z  
    If you're in XPath 2.0, replace the <pre class="jive-pre"> td[4] </pre> at the end of the path with <pre class="jive-pre"> (td[4], "")[1] </pre> . If you're in XPath 1.0, then I think that whatever code calls this XPath expression will need to check the result to see if it is empty.

    You didn't do what I asked, which is to show a complete working sample. You haven't shown how this XPath expression was invoked or how the output table was constructed. If you want help, you are going to have to be more open.
    Thanks, Im using PHP to invoke the xpaths. my code looks like this:

    
    foreach ($nodes = $x->query(
    "//div[@class='myBox2' and position()>0]/table[@id='data']//tr[2]/td/table//tr[2]/td[4]") as $node )
    { $age1 = convert_smart_quotes(str_replace(array(
    "\n", 
    "\r", 
    "\t"), 
    '',$node->nodeValue)) . 
    ""; $output[
    'age1'][] = $age1; 
    }
    


    There are 32 people in the list but only 24 of them have ages set. I just need the ages to line up with the names, which would happen if the td's where there and just had a null value, but this happens ALOT where certain td's are missing if the content is not available, so things get out of order. since Im using php and firefox, its XPATH 1.0..
  • kyroking
    kyroking
    4 Posts

    Re: return even if empty

    ‏2010-11-26T19:44:11Z  
    • kyroking
    • ‏2010-11-26T18:19:25Z
    Thanks, Im using PHP to invoke the xpaths. my code looks like this:

    <pre class="jive-pre"> foreach ($nodes = $x->query( "//div[@class='myBox2' and position()>0]/table[@id='data']//tr[2]/td/table//tr[2]/td[4]") as $node ) { $age1 = convert_smart_quotes(str_replace(array( "\n", "\r", "\t"), '',$node->nodeValue)) . ""; $output[ 'age1'][] = $age1; } </pre>

    There are 32 people in the list but only 24 of them have ages set. I just need the ages to line up with the names, which would happen if the td's where there and just had a null value, but this happens ALOT where certain td's are missing if the content is not available, so things get out of order. since Im using php and firefox, its XPATH 1.0..
    I just figured it out.. I used following::*[5] on the end of the xpath string, I had to do some pregmatch to get rid of the data that it replaced it with if it didnt find the value I was looking for, but now everything lines up.