SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XPath not returning correct number of nodes

    I'm trying to parse an RSS2 feed (using Delphi and MSXML)
    and this code is not returning the correct number of itemnodes:


    oChannelNode := RSSXML.selectsinglenode('rss').firstchild;
    oItemNodes := oChannelNode.selectNodes('//item[category="Tech Tips"]');

    oItemsNodes.length is 2 when it should be 3.

    Any ideas as to why that xpath query is not working correctly?


    The XML Below is what I'm parsing:

    <?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
    <channel>
    <title>OurBlog Ľ Tech Tips</title>
    <atom:link href="http://www.mysite.com/blog/?cat=21&feed=rss2" rel="self" type="application/rss+xml" />
    <link>http://www.mysite.com/blog</link>
    <description>Our Blog</description>
    <pubDate>Fri, 08 May 2009 17:54:31 +0000</pubDate>
    <generator>http://wordpress.org/?v=2.7</generator>
    <language>en</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
    <title>Let the Infrastructure Rebuild Commence!</title>
    <link>http://www.mysite.com/blog/?p=131</link>
    <comments>http://www.mysite.com/blog/?p=131#comments</comments>
    <pubDate>Wed, 04 Mar 2009 21:07:03 +0000</pubDate>
    <dc:creator>admin</dc:creator>
    <category><![CDATA[Stimulus Package & Construction]]></category>
    <category><![CDATA[Tech Tips]]></category>
    <category><![CDATA[bonding]]></category>
    <guid isPermaLink="false">http://www.mysite.com/blog/?p=131</guid>
    <description><SNIP></description>
    <content:encoded><SNIP></content:encoded>
    <wfw:commentRss>http://www.mysite.com/blog/?feed=rss2&amp;p=131</wfw:commentRss>
    </item>
    <item>
    <title>Where Does All Your Time Go?</title>
    <link>http://www.mysite.com/blog/?p=67</link>
    <comments>http://www.mysite.com/blog/?p=67#comments</comments>
    <pubDate>Fri, 20 Feb 2009 16:10:17 +0000</pubDate>
    <dc:creator>admin</dc:creator>
    <category><![CDATA[Tech Tips]]></category>
    <category><![CDATA[better business]]></category>
    <category><![CDATA[reconciling your time]]></category>
    <category><![CDATA[time management]]></category>
    <guid isPermaLink="false">http://www.mysite.com/blog/?p=67</guid>
    <description></description>
    <content:encoded></content:encoded>
    <wfw:commentRss>http://www.mysite.com/blog/?feed=rss2&amp;p=67</wfw:commentRss>
    </item>
    <item>
    <title>Spyware, Ugh! Donít Let It Slow You Down</title>
    <link>http://www.mysite.com/blog/?p=49</link>
    <comments>http://www.mysite.com/blog/?p=49#comments</comments>
    <pubDate>Mon, 16 Feb 2009 17:12:16 +0000</pubDate>
    <dc:creator>admin</dc:creator>
    <category><![CDATA[Tech Tips]]></category>
    <category><![CDATA[Ad-Aware]]></category>
    <category><![CDATA[Internet security]]></category>
    <category><![CDATA[Microsoft Defender]]></category>
    <category><![CDATA[spyware]]></category>
    <category><![CDATA[ways to avoid spyware]]></category>
    <guid isPermaLink="false">http://www.mysite.com/blog/?p=49</guid>
    <description></description>
    <content:encoded></content:encoded>
    <wfw:commentRss>http://www.mysite.com/blog/?feed=rss2&amp;p=49</wfw:commentRss>
    </item>
    </channel>
    </rss>
    MrBaseball34
    Hook'Em Horns!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    I wonder if the parser is choking on the "fancy" single quote? I made some changes to the XML file.
    Code XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
    <channel>
    <!-- replaced invalid char with > -->
    <title>OurBlog > Tech Tips</title>
    <!-- changed &feed to &amp;feed -->
    <atom:link href="http://www.mysite.com/blog/?cat=21&amp;feed=rss2" rel="self" type="application/rss+xml" />
    <link>http://www.mysite.com/blog</link>
    <description>Our Blog</description>
    <pubDate>Fri, 08 May 2009 17:54:31 +0000</pubDate>
    <generator>http://wordpress.org/?v=2.7</generator>
    <language>en</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
    <title>Let the Infrastructure Rebuild Commence!</title>
    <link>http://www.mysite.com/blog/?p=131</link>
    <comments>http://www.mysite.com/blog/?p=131#comments</comments>
    <pubDate>Wed, 04 Mar 2009 21:07:03 +0000</pubDate>
    <dc:creator>admin</dc:creator>
    <category><![CDATA[Stimulus Package & Construction]]></category>
    <category><![CDATA[Tech Tips]]></category>
    <category><![CDATA[bonding]]></category>
    <guid isPermaLink="false">http://www.mysite.com/blog/?p=131</guid>
    <!-- removed <SNIP> tags from description + content -->
    <description></description>
    <content:encoded></content:encoded>
    <wfw:commentRss>http://www.mysite.com/blog/?feed=rss2&amp;p=131</wfw:commentRss>
    </item>
    <item>
    <title>Where Does All Your Time Go?</title>
    <link>http://www.mysite.com/blog/?p=67</link>
    <comments>http://www.mysite.com/blog/?p=67#comments</comments>
    <pubDate>Fri, 20 Feb 2009 16:10:17 +0000</pubDate>
    <dc:creator>admin</dc:creator>
    <category><![CDATA[Tech Tips]]></category>
    <category><![CDATA[better business]]></category>
    <category><![CDATA[reconciling your time]]></category>
    <category><![CDATA[time management]]></category>
    <guid isPermaLink="false">http://www.mysite.com/blog/?p=67</guid>
    <description></description>
    <content:encoded></content:encoded>
    <wfw:commentRss>http://www.mysite.com/blog/?feed=rss2&amp;p=67</wfw:commentRss>
    </item>
    <item>
    <!-- replaced invalid char with ' -->
    <title>Spyware, Ugh! Don't Let It Slow You Down</title>
    <link>http://www.mysite.com/blog/?p=49</link>
    <comments>http://www.mysite.com/blog/?p=49#comments</comments>
    <pubDate>Mon, 16 Feb 2009 17:12:16 +0000</pubDate>
    <dc:creator>admin</dc:creator>
    <category><![CDATA[Tech Tips]]></category>
    <category><![CDATA[Ad-Aware]]></category>
    <category><![CDATA[Internet security]]></category>
    <category><![CDATA[Microsoft Defender]]></category>
    <category><![CDATA[spyware]]></category>
    <category><![CDATA[ways to avoid spyware]]></category>
    <guid isPermaLink="false">http://www.mysite.com/blog/?p=49</guid>
    <description></description>
    <content:encoded></content:encoded>
    <wfw:commentRss>http://www.mysite.com/blog/?feed=rss2&amp;p=49</wfw:commentRss>
    </item>
    </channel>
    </rss>
    and tested with this javascript MSXML 4.0 page
    HTML Code:
    <html><head><title>node code</title>
    <script type="text/javascript">
    var getNodeLength = function ()
    {
    	var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
    	xmlDoc.async = false;
    	xmlDoc.load("baseball.xml");
    
    	var bodytag = document.getElementsByTagName("body");
    	var ptag = document.createElement("p");
    	var objNodes = xmlDoc.selectNodes("//item[category='Tech Tips']");
    	var textnode = document.createTextNode(objNodes.length);
    	ptag.appendChild(textnode);
    	bodytag(0).appendChild(ptag);
    
            var styletag = document.createElement("style");
            styletag.setAttribute("type", "text/css");
            var styles = document.getElementsByTagName("head").item(0).appendChild(styletag);
    	styles.styleSheet.addRule("p", "width: 3em; border: 1px solid #000; padding: 1em; text-align: center");
    };
    window.onload = getNodeLength;
    </script>
    </head>
    <body>
    </body></html>
    and it gave "3"
    Last edited by Mittineague; May 13, 2009 at 02:05. Reason: improved code


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •