In XSLT 2.0 inside of a predicate how can I compar

2019-05-13 19:30发布

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?

标签: xslt
2条回答
虎瘦雄心在
2楼-- · 2019-05-13 20:03

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")]
查看更多
Juvenile、少年°
3楼-- · 2019-05-13 20:05

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.

查看更多
登录 后发表回答