Say I have the following XML file:
<A xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:foo="http://foo"
xsi:type="foo:SomeSubtypeOfA">
boo
</A>
I wish to require that the XML instance is of a specific type derived from A, e.g. type {http://foo}SomeSubtypeOfA (as indeed the above example is). So I am using the following XPath:
/*[@xsi:type='foo:SomeSubtypeOfA']
... and check whether it succeeds in returning a node or not. However the XPath above is problematic as it relies on the arbitrary prefix so it fails on the following file:
<A xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:foo2="http://foo"
xsi:type="foo2:SomeSubtypeOfA">
boo
</A>
... and it produces a false match on the following file:
<A xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:foo="http://fooX"
xsi:type="foo:SomeSubtypeOfA">
boo
</A>
To get rid of the false match I could write the XPath as:
/*[namespace::foo[.='http://foo'] and @xsi:type='foo:SomeSubtypeOfA']
But that still fails to account for the possibility of some other prefix bound to the http://foo namespace. How can I write the above XPath so it checks the namespace of the type given in the xsi:type attribute rather than its prefix?
No comments:
Post a Comment