0

Hi all i am trying to use xpathQuery for the below xml data.

My xml file starts with assessmentItem. There is not even single character before assessmentItem

<assessmentItem adaptive="false" identifier="wordsearch" timeDependent="false" title="Word Search" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsqti_v2p1 
http://www.imsglobal.org/xsd/imsqti_v2p1.xsd" 
xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">



<itemBody>


<div id="heading">
  <p><![CDATA[<TextFlow columnCount="inherit" columnGap="inherit" columnWidth="inherit" fontFamily="Verdana" fontSize="9" lineBreak="inherit" paddingBottom="inherit" paddingLeft="2" paddingRight="2" paddingTop="6" verticalAlign="inherit" whiteSpaceCollapse="preserve" xmlns="http://ns.adobe.com/textLayout/2008"><p><span>Word search</span></p></TextFlow>]]></p>
</div>
<div id="direction">
  <p><![CDATA[<TextFlow columnCount="inherit" columnGap="inherit" columnWidth="inherit" fontFamily="Verdana" fontSize="9" lineBreak="inherit" paddingBottom="inherit" paddingLeft="2" paddingRight="2" paddingTop="6" verticalAlign="inherit" whiteSpaceCollapse="preserve" xmlns="http://ns.adobe.com/textLayout/2008"><p><span>Find the words in the grid</span></p></TextFlow>]]></p>
</div>
<div id="mask">
  <p><![CDATA[Yes]]></p>
</div>
<div id="layout">
  <p><![CDATA[13*13]]></p>
</div>
<div id="numOfGuesses">
  <p><![CDATA[10]]></p>
</div>
<matchInteraction maxAssociations="2" responseIdentifier="RESPONSE" shuffle="true">
  <simpleMatchSet>
    <simpleAssociableChoice identifier="W_1" matchMax="1">
      <p><![CDATA[harsha]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_2" matchMax="1">
      <p><![CDATA[dinakar]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_3" matchMax="1">
      <p><![CDATA[hurixsystems]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_4" matchMax="1">
      <p><![CDATA[aspire]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_5" matchMax="1">
      <p><![CDATA[india]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_6" matchMax="1">
      <p><![CDATA[kitab]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_7" matchMax="1">
      <p><![CDATA[google]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_8" matchMax="1">
      <p><![CDATA[australia]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_9" matchMax="1">
      <p><![CDATA[elephant]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_10" matchMax="1">
      <p><![CDATA[mumbai]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_11" matchMax="1">
      <p><![CDATA[hyderabad]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_12" matchMax="1">
      <p><![CDATA[chennai]]></p>
    </simpleAssociableChoice>
    <simpleAssociableChoice identifier="W_13" matchMax="1">
      <p><![CDATA[dictera]]></p>
    </simpleAssociableChoice>
  </simpleMatchSet>
</matchInteraction>
//bla bla..

form the above xml i am trying to get array which contains "p-tag"values as a child of simpleMatchSet. my xpath query is like this

  NSString *xmlFilePath=[[NSBundle mainBundle]pathForResource:@"wordsearchmain" ofType:@"xml"];
NSData *data=[[NSData alloc]initWithContentsOfFile:xmlFilePath];

NSString *xpathQuery = [[NSString alloc] initWithFormat:@"assessmentItem/itemBody/matchInteraction/simpleMatchSet/../simpleAssociableChoice/p/text()"];
NSDictionary *results = PerformXMLXPathQuery(data, xpathQuery);
[xpathQuery release];

i dont know where i did mistake to get data from the xml.can any one suggest me to correct my xpathQuery.Thanks all.All suggestions acceptable.

ajay
  • 3,245
  • 4
  • 31
  • 59
  • 1
    `simpleMatchSet/../simpleAssociableChoice`: `../` will take you to again back to `` and under that node there is no immediate `

    ` tag. This could be one probable issue with this xpath

    – Vaman Kulkarni May 19 '11 at 14:58
  • Thanks for your reply.can you suggest me how can i modify my query. – ajay May 19 '11 at 16:06
  • even not getting for this also assessmentItem/itemBody/matchInteraction/simpleMatchSet/simpleAssociableChoice/p/text() – ajay May 19 '11 at 16:09

1 Answers1

1

The elements you are trying to match in your XPath query are in the namespace whose URI is "http://www.imsglobal.org/xsd/imsqti_v2p1". This is because on the top-level element there is a default namespace declaration:

xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" 

To match these elements, you have two choices:

  1. define a namespace prefix for "http://www.imsglobal.org/xsd/imsqti_v2p1" and then use that prefix in your XPath expression before each element name, e.g.: "imsq:assessmentItem/imsq:itemBody/imsq:matchInteraction/imsq:simpleMatchSet/imsq:simpleAssociableChoice/imsq:p/text()"

    or

  2. modify your XPath expression to be namespace-insensitive, e.g.

    "//*[local-name() = 'simpleAssociableChoice']//text()"

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • Thanks for your answer.i am trying to use first one.please tell me is imsq normal nsstring type or any other type. – ajay May 20 '11 at 05:25
  • @AAA: Sorry, I don't know anything about the `PerformXMLXPathQuery()` or how to pass namespace definitions to it. I guess this is part of libxml2? or a wrapper function specific to iOS? – LarsH May 20 '11 at 16:28
  • @AAA: OK, I see you're using Matt Gallagher’s XPath wrappers from http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html. Unfortunately the wrappers make things so simple, they apparently don't provide a way to pass in namespace declarations. You may have to skip the wrapper functions and use the underlying libxml2 functions. – LarsH May 20 '11 at 16:51
  • @AAA: see this page for some examples of how to declare and use namespaces with libxml2: http://philwilson.org/blog/2007/11/parsing-atom-with-libxml2 – LarsH May 20 '11 at 16:55