Forum Moderators: open

Message Too Old, No Replies

XPath wont return requested element

         

Holloway

10:15 am on Jun 19, 2006 (gmt 0)

10+ Year Member



Hi All,
I have a problem when trying to query (using xpath) the xml returned by my web service.

I can query and get a result returned for part of the xml i.e. if I run the expression //diffgr:diffgram I get this element and can loop through the node list using properties like firstChild. But I cannot seem to use xpath to get the next child i.e. //diffgr:diffgram/ProgressDS. Also the expression /DataSet/diffgr:diffgram/ does not work either.

Here is the XML:


<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://example.org/MyProgress/">
<xs:schema id="ProgressDS" targetNamespace="http://CheckPoint.org/Progress/" xmlns:mstns="http://CheckPoint.org/Progress/" xmlns="http://CheckPoint.org/Progress/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="ProgressDS" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="attempts" type="xs:int" minOccurs="0" />
<xs:element name="best_score" type="xs:decimal" minOccurs="0" />
<xs:element name="last_attempt_date" type="xs:string" minOccurs="0" />
<xs:element name="course_title" type="xs:string" minOccurs="0" />
<xs:element name="module_title" type="xs:string" minOccurs="0" />
<xs:element name="lesson_status" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<ProgressDS xmlns="http://CheckPoint.org/Progress/">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<attempts>1</attempts>
<best_score>0</best_score>
<last_attempt_date>09/02/2006</last_attempt_date>
<course_title>Access XP</course_title>
<module_title>Introducing Access</module_title>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<lesson_status>completed</lesson_status>
</Table>
</ProgressDS>
</diffgr:diffgram>
</DataSet>

Any help would be great, thanks.

Andy.

[edited by: tedster at 3:23 am (utc) on June 21, 2006]
[edit reason] use example.com in code [/edit]

choster

3:52 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A couple of things:

  • /DataSet/diffgr:diffgram/ is not a valid XPath. The last part of an XPath should be a node (whether an element, attribute, or text).
  • Can you provide a sample of your XSLT? It is hard otherwise to figure out what you are trying to retrieve. For instance, if you have already matched //diffgr:diffgram then subordinate templates should work on ProgressDS, not //diffgr:diffgram/ProgressDS .

Holloway

8:04 am on Jun 20, 2006 (gmt 0)

10+ Year Member



Hi Choster,
Thanks for the speedy reply. I know a bit about XML, namespaces and XPath but I am still a novice to most of it. Would you be able to explain a few things for me?

I am using Stylus Studio 2006 and it allows me to create XPath querys quite easily. This is a query it generated to access the <attempt> node:

/*[local-name()='DataSet' and namespace-uri()='http://example.com/MyProgress/']/*[local-name()='diffgram' and namespace-uri()='urn:schemas-microsoft-com:xml-diffgram-v1']/*[local-name()='ProgressDS' and namespace-uri()='http://example.com/Progress/'][1]/*[local-name()='Table' and namespace-uri()='http://example.com/Progress/'][1]/*[local-name()='attempts' and namespace-uri()='http://CheckPoint.org/Progress/'][1]

As you can see it is quite large. It can be simplified I know, but what I am not too sure about is the concept of namespaces being used to access nodes. Why is it that //ProgressDS does not return the <ProgressDS> node as I have requested XPath to return it regardless of where it is in the doc?

I understand namespaces are an identifier and the url notation is best to ensure unique qualification but what is the meaning behind:

<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">

and isn't diffgr:diffgram just a node with an assigned NS and therefore not the default xmlns?

I don't have an XSLT as the XML is just generated from my DataSet and pushed via a web service that my page calls.

I understand some of this might not be that clear (novice, sorry) but any explanation would be great.

Thanks for your help.

Andy.

[1][edited by: tedster at 3:26 am (utc) on June 21, 2006]
[edit reason] use example.com in code [/edit]