Is this XPath query on parsing XHTML wrong? using TouchXML - iphone

I have been trying to parse a XHTML doc via TouchXML, but it always can't find any tags via XPath query.
Below is the XHTML:
XHTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 25 March 2009), see www.w3.org" />
<title></title>
</head>
<body>
<p>
<a href="http://www.flickr.com/photos/55397648#N00/5987335786/"
title="casavermeer5.jpg by the style files, on Flickr">
<img src="http://farm7.static.flickr.com/6127/5987335786_abec990554_o.jpg"
width="500" height="750" border="0" alt="casavermeer5.jpg" />
</a>
</p>
</body>
</html>
So, we can see there are a "p" tag, "a" tag and "img" tag
What I did then is shown as the code below:
CXHTMLDocument *doc = [[[CXHTMLDocument alloc] initWithXHTMLString:XHTML options:0 error:&error] autorelease];
NSLog(#"error %#", [error localizedDescription]);
NSLog(#"doc children count = %d", [doc childCount]);
NSArray *imgNodeArray = [doc nodesForXPath:#"//img" error:&error];
NSLog(#"imgNodeArray = %d", [imgNodeArray count]);
NSLog(#"error %#", [error localizedDescription]);
The results are
error (null)
doc children count = 2
imgNodeArray = 0
error (null)
So, there are no error at all in parsing the XHTML doc and no error for the XPath query. Also this doc has two children under the root ("body" tag and "head" tag). But the problem is it cannot find the "img" tag. I have tried to replace "img" with other possible tag names (such as p, a, even body, head), no luck at all.
Can someone help me here?
P.S.
Actually the original doc is a HTML, I have used CTidy class in TouchXML lib to tidy the HTML to XHTML first. The XHTML above came from that CTidy results.
I also tried to add a namespace thing to the XPath query, like this
NSMutableDictionary *namespaceDict = [NSMutableDictionary dictionary];
[namespaceDict setValue:#"http://www.w3.org/1999/xhtml" forKey:#"xhtml"];
And change the XPath query to
NSArray *imgNodeArray = [doc nodesForXPath:#"//xhtml:img" namespaceMappings:namespaceDict error:&error];
Still no luck, can't find any results.

Try this //img.
When you use // it gets the img tag, no matter where it is in the page.
It is better than //xhtml:img - because sometimes the hierarchic tags change a bit in the code behind, so it is better to be global, and not too much specific.

I had a similar problem once that might help you. I had a document that I would parse and find certain landmarks and record their XPaths. Then, I would load the document into a UIWebView and run JavaScript to perform actions on the elements that I had previously marked. Problematically, the DOM structure was completely different after parsing the document and all my XPaths were invalid. One particular case related to tables.
<table>
<tr>
<td>Cell</td>
</tr>
</table>
The simple HTML above would always be converted to something like below. (The white space is only for readability and I'm going from memory.)
<table>
<thead></thead>
<tbody>
<tr>
<td>Cell</td>
</tr>
</tbody>
</table>
My point with this is that your parser may have injected elements into your HTML structure.

Related

need a good method to Page break and add Header

I am using itextsharp library.I design an HTML page and convert to PDF .in that case some table are not split perfectly and row also not split correctly.
I tried to put a comment in the HTML code writing based on the font how many rows the page can contains, then after reaching the limit i just add another page,
this works but doesnt seems a professional way to fix this.
After a search i found many discussions using this code
<style type="text/css">
table { page-break-inside:auto }
tr { page-break-inside:avoid; page-break-after:auto }
thead { display:table-header-group }
tfoot { display:table-footer-group }
</style>
But, what if i need to add a header on each page?
thanks
If you don't want to use events, a tricky method is to use this css style:
table.hrepeat {
repeat-header: yes;
}
and wrap all your content inside an html table adding header to repeat as head of the table.
Example:
<!DOCTYPE html>
<head>
<style type="text/css">
table.hrepeat {repeat-header: yes;}
</style>
</head>
<body>
<table class="hrepeat">
<thead>
<tr>
<th>Header on each page</th>
</tr>
</thead>
<tbody>
....
</tbody>
</table>
</body>

Accessing elements from within a iFrame -fails in watir-webdriver 0.6.6 - works fine in 0.6.4

I am trying to select a value in a dropdown which is inside a iFrame. The frame is nested within a table.
HTML:
<html class ="some text">
<head>...</head> <body id="htmlBody">
<div class= "modalWindow ui-dialog-content ui-widget-content ModalWindow containsFrame" id="ui-id-2">
<iframe src= "/MyApplicantPortal/Applicant/254471/SelectOrderTemplate?t=1">
#document
<html class = "some text"
<head id= "Head1">
<body id="htmlBody">
<form method= "post" action="SelectOrderTemplate?t=1" id="form1">
<div class = "ModalContent">
<Table>
<td class="label1">
<Select id= "MyselList" </td>
My code:
element(:select_frame, :frame, :src => MyApplicantPortal\/Applicant\/\d+\/SelectOrderTemplate\?t=1/)
select_list(:template) {select_frame_element.select_list_element(:id => 'MyselList')}
Error:
Watir::Exception::UnknownFrameException: unable to locate frame using {:src=>/MyApplicantPortal\/Applicant\/\d+\/SelectOrderTemplate\?t=1/, :tag_name=>"frame"}
Watir-webdriver 0.6.5 has changed the way locating frames works.
Before (ie pre-0.6.5), browser.frame located frame and iframe elements.
Now, browser.frame locates frame elements and browser.iframe locates iframe elements.
You will need to change your frame accessor to match the new rules (when you migrated to 0.6.5 or later). The second parameter needs to change from :frame to :iframe:
element(:select_frame, :iframe, :src => /MyApplicantPortal\/Applicant\/\d+\/SelectOrderTemplate\?t=1/)
I am in the process of releasing a new version of the page-object gem that addresses this issue. Should be out in a day or two.

Blue Dot Menu displaying HTML as text

After the app menu html is retrieved, it is displayed as text instead of html. Chrome complains Resource interpreted as Script but transferred with MIME type text/plain. I'm using MVC on the Force.com platform. I've tried specifying the content type of the response as "text/html" and "application/javascript", but neither worked.
[EDIT 1]
Code
<script>
intuit.ipp.anywhere.setup({
menuProxy: "https://c.na55.visual.force.com/"
+ "apex/bluedot",
grantUrl: "https://c.na55.visual.force.com/"
+ "apex/authpage"
});
</script>
<ipp:bluedot>
<div id="intuitPlatformAppMenu">
<a id="intuitPlatformAppMenuLogo" href="javascript:void(0);" title="Intuit App Center">
<span id="intuitPlatformAppMenuDot"> </span>
</a>
<div id="intuitPlatformAppMenuDropdown" style="display: none;">
<div id="intuitPlatformAppMenuDropdownTop"></div>
<div id="intuitPlatformAppMenuDropdownInner">
<<=======
</div>
</div>
</div>
</ipp:bluedot>
When dropdown is open, code is added at arrow location as string and a class 'open' is addedto #intuitPlatformAppMenuLogo.
Image
[EDIT 2]
Server side apex code
public with sharing class GetBlueDotMenu {
public String response {get; set;}
public GetBlueDotMenu() {
QbApiController api = new QbApiController ('GET', 'QB API' , null, null, 'https://appcenter.intuit.com/api/v1/Account/AppMenu');
response = api.execute();
}
}
api.execute() returns the response body and saves it to response which is then rendered on the page.
This issue arises from Visualforce's default rendering of strings as escaped. To fix it, the apex:outputText attribute escaped needs to be "false". See http://bit.ly/13CSXve
PFB link -
https://developer.intuit.com/docs/0025_quickbooksapi/0060_auth_auth/widgets/blue_dot_menu
For IE8, you should add (as mentioned in the above doc)
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ipp="">
You can clear the browser's cache and try it again.
Plz let me know how it goes.
Thanks

Get the right node from duplicates xml with NSXML

I'm trying to us NSXML to parse a user's channel from youtube. Parsing works ok, but I can't seem to figure out how to get the link from any specific movie as their are 5 exact the same nodes as following:
<link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=vLleTDikufefbk&feature=youtube_gdata" />
<link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/vLleTDikededubk/responses" />
<link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/videos/vLlededTDikubk/related" />
<link rel="http://gdata.youtube.com/schemas/2007#mobile" type="text/html" href="http://m.youtube.com/details?v=vLldedeeTDikubk" />
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/node/uploads/vLlgrgreTDikubk" />
I've figured out how to get the attribute href from the node link. But since their are 5 different links I don't know how to only select the first one. Anyone got an idea?
Thnx you guys!!!
Found the solution already. I'll check if the link node has an attribute with alternate in it. If it does it has the right link node. Here is the code:
NSMutableArray *link; if ([elementName isEqualToString:#"link"]) if (!link) link = [[NSMutableArray alloc] init]; NSString *alternate = [attributeDict objectForKey:#"rel"]; if([alternate isEqualToString:#"alternate"]){ NSString *href = [attributeDict objectForKey:#"href"]; alternate = NULL; }

Reading data from server script using URL

I am calling a login script on the server using http call from iphone.
the script returns a string "Invalid" or "valid" based on given username/pswd.
Here is what I am using:
NSString *myurlstr = [[NSString alloc] initWithFormat:#"http://www.mysite.com/iph/login.aspx?username=%#&password=%#",uname,pswd];
NSString *resultstr = [NSString stringWithContentsOfURL:[NSURL URLWithString:myurlstr] encoding:NSUTF8StringEncoding error:&error];
NSLog(#"HERE IS THE ONE: %#",resultstr);
Here is what the Console log prints:
TestingTrying[51514:207] HERE IS THE ONE: Invalid Login/Password
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml" >
head>title>
Untitled Page
/title>/head>
body>
form name="form1" method="post" action="login.aspx?username=abc&password=abc" id="form1">
input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTnVSKZt+HQfSlQeCketelC9X+47A==" />
div>
/div>
/form>
/body>
/html>
Can someone help on how to get the string only(Invalid Login/Password) and not the content that starts from DOCTYPE onwards?
If your simply asking how to get the first line from your server response, use a tokenizer. This should point you in the right direction, just use new line which is /n from memory as the delimiter.