| re: xpath query query
David Gordon <nospam@nospam.com> writes:
.....[color=blue]
> <xml>
> <node name="a" type="a"/>
> <node name="b" type=""/>
> <node name="c"/>
> <node name="d" type="b"/>
> </xml>
>
> xpath test.xml '/xml/node/@type'
>
> which returns
>
> a
>
> b
>
> But passes over the node with no type attribute. Is there an xpath query
> which would give the result:
>
> a
>
>
> b
>
> So where no type attribute existed, an empty string is returned?
>[/color]
Since you are using Xpath1 (rather than xpath2 or xslt for example)
You have to return nodes that exist in the source, in document order,
or return a single atomic value such as a string.
So in this case you would need to return some node to repreentthe
missing type on c and that node must occur between th etype attribute on
b and the type attribute on c. The only possiblities really then are the
node <node name="c"/> or the white space text nodes either side of that.
Given that your XPath tool outputs attribute nodes @type as just (for
example) "a" I assume it prints the string value of a node, which is
empty for <node name="c"/>, which is what you want, so..
/xml/node/@type|/xml/node[not(@type)]
returns a node set of all type attribute nodes, and all node element
nodes that don't have a type attribute.
With a bit of luck your xpath tool will output that node set by printing
the string value of each node, in document order, but i can't test that.
David |