2

This is an XPath query which would print full path of every element in the file

for $boy in //*
return string-join(($boy/ancestor-or-self::*/name()), "::")

How do I get the element's type as well (I need to know primitive types foremost: xs:integer, xs:string, etc)?

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
paul424
  • 153
  • 2
  • 13

2 Answers2

3

There is no function to determine an element's type. As you're using flwor expressions, I guess your XPath processor supports XPath 2.0: then you can use instance of to determine the type, eg.

  • "foo" instance of xs:string
  • 1 instance of xs:integer
  • //* instance of element()*

You could use use an if/else-chain to determine the type (the example uses XQuery, in XPath you cannot define functions), but I cannot really come up with an XPath application example right now.

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
0

Jens Erat's answer is correct as far as the XPath 2.0 standard goes.

In Saxon (You need EE for schema awareness) there's an extension function

saxon:type-annotation()

which gives you the type annotation of a value as a QName, and another

saxon:type

which gives you the type as a schema component, represented as a function item; you can get specific properties of the type (including its name) by supplying the required property name to this function. Details at

http://www.saxonica.com/documentation/#!functions/saxon

But I see this isn't quite what you asked for; if an element has been schema validated as an integer it will give the result as xs:integer, not as element(). It does what you want for atomic values, though.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164