Note that there are some explanatory texts on larger screens.

plurals
  1. POKissXML fails with XML containing processing instructions
    primarykey
    data
    text
    <p>I have encountered an issue where XPath Queries will fail in KissXML when the XML contains processing instructions, but will work fine if the exact same XML does not contain any processing instructions.</p> <p>As an example I have the following XML without processing instructions (demo1.xml):</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;tst:TstForm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tst="http://schemas.somewhere.com/tst/1" xml:lang="en-US"&gt; &lt;tst:TstDetails&gt; &lt;tst:Title&gt;Labelling error on boxes&lt;/tst:Title&gt; &lt;tst:Description&gt;Box labels&lt;/tst:Description&gt; &lt;/tst:TstDetails&gt; &lt;/tst:TstForm&gt; </code></pre> <p>I am parsing the XML and executing an XPath query as follows:</p> <pre><code>// Parse the DEMO XML - no processing instructions, // so this will work NSLog(@"**** About to parse %@ ****\n\n", xmlFilename); NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:xmlFilename]; NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath]; NSError *err=nil; NSURL *furl = [NSURL fileURLWithPath:XMLPath]; if (!furl) { NSLog(@"Can't create an URL from file: %@.", XMLPath); return; } xmlViewDoc = [[DDXMLDocument alloc] initWithData:XMLData options:0 error:&amp;err]; // Prove that the XML was parsed correctly... NSLog(@"Root node'%@' found\n\n", xmlViewDoc.rootElement.name); NSLog(@"About to iterate through elements 2 levels down from the root node..."); NSArray *nodes = xmlViewDoc.rootElement.children; for (DDXMLElement *node in nodes) { NSArray *childNodes = node.children; for (DDXMLElement *childNode in childNodes) { NSLog(@" XPath: %@", childNode.XPath); } } NSLog(@"Root node elements END\n\n"); // Show that the namespaces are parsed correctly NSLog(@"Root node contains the following namespaces..."); nodes = xmlViewDoc.rootElement.namespaces; for (DDXMLElement *node in nodes) { NSLog(@" Found NS: '%@' = '%@'", node.name, node.stringValue); } NSLog(@"Namespaces END\n\n"); // Now execute an XPath query using the namespace NSString *xPathQuery = @"/tst:TstForm/tst:TstDetails"; NSLog(@"Based on the above namespace and the XML structure, we should be able to execite the following XPath query:\n %@", xPathQuery); NSLog(@"The XPath query should return two elements (Title &amp; Description)..."); nodes = [xmlViewDoc.rootElement nodesForXPath:xPathQuery error:nil]; for (DDXMLElement *node in nodes) { NSLog(@" XPath: %@", node.XPath); } NSLog(@"XPathQuery END\n\n"); </code></pre> <p>This code provides the following log output as expected:</p> <pre><code>2012-09-03 13:04:25.662 NDPad[37359:c07] **** About to parse demo1.xml **** 2012-09-03 13:04:25.690 NDPad[37359:c07] Root node'tst:TstForm' found 2012-09-03 13:04:25.690 NDPad[37359:c07] About to iterate through elements 2 levels down from the root node... 2012-09-03 13:04:25.691 NDPad[37359:c07] XPath: /TstForm[1]/TstDetails[1]/Title[1] 2012-09-03 13:04:25.691 NDPad[37359:c07] XPath: /TstForm[1]/TstDetails[1]/Description[1] 2012-09-03 13:04:25.691 NDPad[37359:c07] Root node elements END 2012-09-03 13:04:25.691 NDPad[37359:c07] Root node contains the following namespaces... 2012-09-03 13:04:25.692 NDPad[37359:c07] Found NS: 'xsi' = 'http://www.w3.org/2001/XMLSchema-instance' 2012-09-03 13:04:25.692 NDPad[37359:c07] Found NS: 'tst' = 'http://schemas.somewhere.com/tst/1' 2012-09-03 13:04:25.692 NDPad[37359:c07] Namespaces END 2012-09-03 13:04:25.692 NDPad[37359:c07] Based on the above namespace and the XML structure, we should be able to execite the following XPath query: /tst:TstForm/tst:TstDetails 2012-09-03 13:04:25.692 NDPad[37359:c07] The XPath query should return two elements (Title &amp; Description)... 2012-09-03 13:04:25.693 NDPad[37359:c07] XPath: /TstForm[1]/TstDetails[1] 2012-09-03 13:04:25.693 NDPad[37359:c07] XPathQuery END </code></pre> <p>However, if I then use the following XML which contains a single processing instruction (demo1-5.xml):</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;?tst-example-instruction?&gt; &lt;tst:TstForm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tst="http://schemas.somewhere.com/tst/1" xml:lang="en-US"&gt; &lt;tst:TstDetails&gt; &lt;tst:Title&gt;Labelling error on boxes&lt;/tst:Title&gt; &lt;tst:Description&gt;Box labels&lt;/tst:Description&gt; &lt;/tst:TstDetails&gt; &lt;/tst:TstForm&gt; </code></pre> <p>The same code will fail, and only provide the following output:</p> <pre><code>2012-09-03 13:04:25.693 NDPad[37359:c07] **** About to parse demo1-5.xml **** 2012-09-03 13:04:25.756 NDPad[37359:c07] Root node'tst:TstForm' found 2012-09-03 13:04:25.756 NDPad[37359:c07] About to iterate through elements 2 levels down from the root node... 2012-09-03 13:04:25.756 NDPad[37359:c07] XPath: /TstForm[1]/TstDetails[1]/Title[1] 2012-09-03 13:04:25.756 NDPad[37359:c07] XPath: /TstForm[1]/TstDetails[1]/Description[1] 2012-09-03 13:04:25.756 NDPad[37359:c07] Root node elements END 2012-09-03 13:04:25.757 NDPad[37359:c07] Root node contains the following namespaces... 2012-09-03 13:04:25.757 NDPad[37359:c07] Found NS: 'xsi' = 'http://www.w3.org/2001/XMLSchema-instance' 2012-09-03 13:04:25.757 NDPad[37359:c07] Found NS: 'tst' = 'http://schemas.somewhere.com/tst/1' 2012-09-03 13:04:25.757 NDPad[37359:c07] Namespaces END 2012-09-03 13:04:25.757 NDPad[37359:c07] Based on the above namespace and the XML structure, we should be able to execite the following XPath query: /tst:TstForm/tst:TstDetails 2012-09-03 13:04:25.758 NDPad[37359:c07] The XPath query should return two elements (Title &amp; Description)... 2012-09-03 13:04:25.758 NDPad[37359:c07] XPathQuery END </code></pre> <p>I can't see what would be wrong with this XML that would cause the XPath queries to fail, especially when iterating through the DOM shows that it parsed correctly.</p> <p>Thanks, Paul</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload