Dale
Dale

Reputation: 1301

In XSLT 2.0 inside of a predicate how can I compare an attribute that has a namespace?

I have the following partial xpath expression

IAAXML:party[@xsi:type='IAAXML:Organization']

My source XML is:

<IAAXML:party xsi:type="IAAXML:Organization">

With the namespace declared as such:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

I get the following error:

The operand types are not compatible for the = operator

How do I do a compare on that attribute using the namespace?

Upvotes: 3

Views: 531

Answers (2)

JLRishe
JLRishe

Reputation: 101758

Michael Kay more thoroughly explained the cause of this issue, but as I conjectured, certain processors treat xsi:type attributes as references to a schema type (which, as Michael clarified, are identified by their QName). So that's why the processor won't let you just compare it against a string value. Assuming you are using the same namespace prefix for that type's namespace in both your source document and in that string value, it sounds like this works (and should work on any processor):

IAAXML:party[string(@xsi:type) = 'IAAXML:Organization']

But Michael's suggestion to use the QName with the namespace URI would be more a more robust approach in a schema-aware processor such as yours.

Upvotes: 2

Michael Kay
Michael Kay

Reputation: 163645

JLRishe has essentially given you the answer, but I'll amplify it. The xsi:type attribute is of type QName. In a schema-aware processor, your comparison is therefore a typed comparison between a QName and a string, which for very good reasons isn't allowed. After all, your application logic shouldn't really depend on the fact that the source document uses the namespace prefix IAAXML rather than some other prefix of the author's choosing. Given that you've got a type-aware processor, it would be better to do a QName comparison:

[@xsi:type = QName("http://the-iaaxml-namespace/", "Organization")]

Upvotes: 4

Related Questions