What's wrong with my xpath?

D

Datawich

Hi. I've been trying without success to query the below XML using
xpath. I'm doing so in the context of a c# method which uses
nodeiterator in the usual way. No matter what select statement I
submit, though, (even a query for the root "/"), I return one of the
<line> elements. Any idea what I'm doing wrong? My goal is to get
the number attribute out of the cell element. Should be queryable
as //cell/@number, but no success so far. Thanks for any help.

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<outputTree xmlns="http://xml.spss.com/spss/oms" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://
xml.spss.com/spss/oms http://xml.spss.com/spss/oms/spss-output-1.2.xsd">
<command command="CTables" displayOutlineValues="label"
displayOutlineVariables="label" displayTableValues="label"
displayTableVariables="label" text="Custom Tables" xml:lang="en">
<pivotTable subType="Notes" text="Notes">
<dimension axis="row" text="Contents">
<category text="Output Created">
<cell date="2007-12-04T14:48:37.22" format="datetime" text="04-
DEC-2007 14:48:37" />
</category>
<category text="Comments">
<cell text="" />
</category>
<group text="Input">
<category text="Data">
<cell text="C:\Documents and Settings\SUBSET2.SAV" />
</category>
<category text="Active Dataset">
<cell text="Captured" />
</category>
<category text="Filter">
<cell text="<none>" />
</category>
<category text="Weight">
<cell text="<none>" />
</category>
<category text="Split File">
<cell text="<none>" />
</category>
<category text="N of Rows in Working Data File">
<cell number="79219" text="79219" />
</category>
</group>
<category text="Syntax">
<cell text="CTABLES /VLABELS VARIABLES=Index_spec DISPLAY=DEFAULT /
TABLE Index_spec [UCOUNT F40.0]." />
</category>
<group text="Resources">
<category text="Elapsed Time">
<cell duration="PT00H00M02.08S" format="time" text="0:00:02.08" />
</category>
</group>
</dimension>
</pivotTable>
<textBlock text="Active Dataset">
<line>[Captured] C:\Documents and Settings\SUBSET2.SAV</line>
</textBlock>
<pivotTable subType="Custom Table" text="Table 1">
<dimension axis="row" displayLastCategory="true" text="Row">
<category text="Index_spec" varName="Index_spec" variable="true">
<dimension axis="column" displayLastCategory="true" text="Column">
<category text="Unweighted Count">
<cell number="79219" text="79219" />
</category>
</dimension>
</category>
</dimension>
</pivotTable>
</command>
</outputTree>
 
A

Arne Vajhøj

Datawich said:
Hi. I've been trying without success to query the below XML using
xpath. I'm doing so in the context of a c# method which uses
nodeiterator in the usual way. No matter what select statement I
submit, though, (even a query for the root "/"), I return one of the
<line> elements. Any idea what I'm doing wrong? My goal is to get
the number attribute out of the cell element. Should be queryable
as //cell/@number, but no success so far.

I find the two cell numbers at
//outputTree/command/pivotTable/dimension/group/category/cell/@number
and
//outputTree/command/pivotTable/dimension/category/dimension/category/cell/@number,
but maybe XPath is not optimal for yoy, maybe good old GetElementTagsByName
is better.

Arne
 
C

Christof Nordiek

Datawich said:
Hi. I've been trying without success to query the below XML using
xpath. I'm doing so in the context of a c# method which uses
nodeiterator in the usual way. No matter what select statement I
submit, though, (even a query for the root "/"), I return one of the
<line> elements. Any idea what I'm doing wrong? My goal is to get
the number attribute out of the cell element. Should be queryable
as //cell/@number, but no success so far. Thanks for any help.

You've got to use namespaces. This worked for me:

XmlDocument xml = new XmlDocument();
xml.Load(xmlFileName);
XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("x", "http://xml.spss.com/spss/oms");
XmlNodeList nodes = xml.SelectNodes("//x:cell/@number", ns);

PS: You cannot declare a default namespace in XPath.

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<outputTree xmlns="http://xml.spss.com/spss/oms" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://
xml.spss.com/spss/oms http://xml.spss.com/spss/oms/spss-output-1.2.xsd">
<command command="CTables" displayOutlineValues="label"
displayOutlineVariables="label" displayTableValues="label"
displayTableVariables="label" text="Custom Tables" xml:lang="en">
<pivotTable subType="Notes" text="Notes">
<dimension axis="row" text="Contents">
<category text="Output Created">
<cell date="2007-12-04T14:48:37.22" format="datetime" text="04-
DEC-2007 14:48:37" />
</category>
<category text="Comments">
<cell text="" />
</category>
<group text="Input">
<category text="Data">
<cell text="C:\Documents and Settings\SUBSET2.SAV" />
</category>
<category text="Active Dataset">
<cell text="Captured" />
</category>
<category text="Filter">
<cell text="<none>" />
</category>
<category text="Weight">
<cell text="<none>" />
</category>
<category text="Split File">
<cell text="<none>" />
</category>
<category text="N of Rows in Working Data File">
<cell number="79219" text="79219" />
</category>
</group>
<category text="Syntax">
<cell text="CTABLES /VLABELS VARIABLES=Index_spec DISPLAY=DEFAULT /
TABLE Index_spec [UCOUNT F40.0]." />
</category>
<group text="Resources">
<category text="Elapsed Time">
<cell duration="PT00H00M02.08S" format="time" text="0:00:02.08" />
</category>
</group>
</dimension>
</pivotTable>
<textBlock text="Active Dataset">
<line>[Captured] C:\Documents and Settings\SUBSET2.SAV</line>
</textBlock>
<pivotTable subType="Custom Table" text="Table 1">
<dimension axis="row" displayLastCategory="true" text="Row">
<category text="Index_spec" varName="Index_spec" variable="true">
<dimension axis="column" displayLastCategory="true" text="Column">
<category text="Unweighted Count">
<cell number="79219" text="79219" />
</category>
</dimension>
</category>
</dimension>
</pivotTable>
</command>
</outputTree>
 
C

Christof Nordiek

BTW:
The XML you gave is not wellformed. It contains markup in attribute values.
I cut that away, becuase it doesn't touch your question.
 
Top