简体   繁体   中英

XPath: get element type

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)?

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'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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM