getting the type of an arbitrary node
Friday, 9 November 2018 · 6 messages
CG
Hi Graydon,
Bridger has already given you a perfect reference.
The XQuery Working Group decided it’s a cleaner solution to do node tests with typeswitch. If you want to do different things based on the node type, it should be a good alternative to the string-based approach:
declare function local:node-type(
$node as node()
) as xs:string {
typeswitch($node)
case element() return 'element'
case comment() return 'comment'
case attribute() return 'attribute'
case text() return 'text'
case document-node() return 'document-node'
case processing-instruction() return 'processing-instruction' default return error()
};
for $node in (<a/>, <!-- x -->)
return local:node-type($node)
Cheers,
Christian
Quoted text · Graydon Saunders · 40 lines
On Fri, Nov 9, 2018 at 7:17 PM Graydon Saunders <graydonish at gmail.com> wrote: > > Hi Bridger -- > > Those are helpful, thanks! > > I was hoping for a built-in (extenstion!) function on the (possibly mistaken) supposition that BaseX just knows that things are in the internal representation and would nigh-certainly be quicker to have something that returns that value directly. > > -- Graydon > > On Fri, Nov 9, 2018 at 1:10 PM Bridger Dyson-Smith <bdysonsmith at gmail.com> wrote: >> >> HI Graydon - >> >> it isn't a builtin function, but maybe the functx:node-kind() and functx:sequence-type() functions are what you want[1,2]? >> Hope that helps. >> >> Best, >> Bridger >> [1] http://www.xqueryfunctions.com/xq/functx_node-kind.html >> [2] http://www.xqueryfunctions.com/xq/functx_sequence-type.html >> >> On Fri, Nov 9, 2018 at 12:40 PM Graydon Saunders <graydonish at gmail.com> wrote: >>> >>> Hi! >>> >>> I am overcome with the cabbage-nature today, because I can't find this in the docs. >>> >>> I am convinced there's a way to go: >>> >>> (//some-element/node()) ! fn:node-type(.) >>> >>> and get a sequence of "element(),element(),text()..." but do not know what the actual function is called. (it's not node-type()!) >>> >>> How ought I to be approaching this? >>> >>> Thanks! >>> Graydon >>> >>>
GS
Graydon Saunders9 Nov 2018 19:40 #
Hi!
I am overcome with the cabbage-nature today, because I can't find this in the docs.
I am convinced there's a way to go:
(//some-element/node()) ! fn:node-type(.)
and get a sequence of "element(),element(),text()..." but do not know what the actual function is called. (it's not node-type()!)
How ought I to be approaching this?
Thanks!
Graydon
BD
Bridger Dyson-Smith9 Nov 2018 20:11 #
HI Graydon -
it isn't a builtin function, but maybe the functx:node-kind() and functx:sequence-type() functions are what you want[1,2]?
Hope that helps.
Best,
Bridger
[1] http://www.xqueryfunctions.com/xq/functx_node-kind.html
[2] http://www.xqueryfunctions.com/xq/functx_sequence-type.html
Quoted text · Graydon Saunders · 21 lines
On Fri, Nov 9, 2018 at 12:40 PM Graydon Saunders <graydonish at gmail.com> wrote: > Hi! > > I am overcome with the cabbage-nature today, because I can't find this in > the docs. > > I am convinced there's a way to go: > > (//some-element/node()) ! fn:node-type(.) > > and get a sequence of "element(),element(),text()..." but do not know > what the actual function is called. (it's not node-type()!) > > How ought I to be approaching this? > > Thanks! > Graydon > > >
GS
Graydon Saunders9 Nov 2018 20:17 #
Hi Bridger --
Those are helpful, thanks!
I was hoping for a built-in (extenstion!) function on the (possibly mistaken) supposition that BaseX just knows that things are in the internal representation and would nigh-certainly be quicker to have something that returns that value directly.
-- Graydon
Quoted text · Bridger Dyson-Smith · 35 lines
On Fri, Nov 9, 2018 at 1:10 PM Bridger Dyson-Smith <bdysonsmith at gmail.com> wrote: > HI Graydon - > > it isn't a builtin function, but maybe the functx:node-kind() and > functx:sequence-type() functions are what you want[1,2]? > Hope that helps. > > Best, > Bridger > [1] http://www.xqueryfunctions.com/xq/functx_node-kind.html > [2] http://www.xqueryfunctions.com/xq/functx_sequence-type.html > > On Fri, Nov 9, 2018 at 12:40 PM Graydon Saunders <graydonish at gmail.com> > wrote: > >> Hi! >> >> I am overcome with the cabbage-nature today, because I can't find this in >> the docs. >> >> I am convinced there's a way to go: >> >> (//some-element/node()) ! fn:node-type(.) >> >> and get a sequence of "element(),element(),text()..." but do not know >> what the actual function is called. (it's not node-type()!) >> >> How ought I to be approaching this? >> >> Thanks! >> Graydon >> >> >>
BD
Graydon -
That makes sense. Maybe parsing the output of
index:facets() would work? If I'm understanding correctly, it will only work from the database level.
BridgerQuoted text · Christian Grün · 82 lines
On Fri, Nov 9, 2018 at 1:37 PM Christian Grün <christian.gruen at gmail.com> wrote: > Hi Graydon, > > Bridger has already given you a perfect reference. > > The XQuery Working Group decided it’s a cleaner solution to do node > tests with typeswitch. If you want to do different things based on the > node type, it should be a good alternative to the string-based > approach: > > declare function local:node-type( > $node as node() > ) as xs:string { > typeswitch($node) > case element() return 'element' > case comment() return 'comment' > case attribute() return 'attribute' > case text() return 'text' > case document-node() return 'document-node' > case processing-instruction() return 'processing-instruction' > default return error() > }; > for $node in (<a/>, <!-- x -->) > return local:node-type($node) > > Cheers, > Christian > > > > > On Fri, Nov 9, 2018 at 7:17 PM Graydon Saunders <graydonish at gmail.com> > wrote: > > > > Hi Bridger -- > > > > Those are helpful, thanks! > > > > I was hoping for a built-in (extenstion!) function on the (possibly > mistaken) supposition that BaseX just knows that things are in the internal > representation and would nigh-certainly be quicker to have something that > returns that value directly. > > > > -- Graydon > > > > On Fri, Nov 9, 2018 at 1:10 PM Bridger Dyson-Smith < > bdysonsmith at gmail.com> wrote: > >> > >> HI Graydon - > >> > >> it isn't a builtin function, but maybe the functx:node-kind() and > functx:sequence-type() functions are what you want[1,2]? > >> Hope that helps. > >> > >> Best, > >> Bridger > >> [1] http://www.xqueryfunctions.com/xq/functx_node-kind.html > >> [2] http://www.xqueryfunctions.com/xq/functx_sequence-type.html > >> > >> On Fri, Nov 9, 2018 at 12:40 PM Graydon Saunders <graydonish at gmail.com> > wrote: > >>> > >>> Hi! > >>> > >>> I am overcome with the cabbage-nature today, because I can't find this > in the docs. > >>> > >>> I am convinced there's a way to go: > >>> > >>> (//some-element/node()) ! fn:node-type(.) > >>> > >>> and get a sequence of "element(),element(),text()..." but do not know > what the actual function is called. (it's not node-type()!) > >>> > >>> How ought I to be approaching this? > >>> > >>> Thanks! > >>> Graydon > >>> > >>> >
GS
Hi Christian --
Thank you! That will be useful. (and might have been what dim memory was trying to recall, too.)
-- Graydon
Quoted text · Christian Grün · 82 lines
On Fri, Nov 9, 2018 at 1:37 PM Christian Grün <christian.gruen at gmail.com> wrote: > Hi Graydon, > > Bridger has already given you a perfect reference. > > The XQuery Working Group decided it’s a cleaner solution to do node > tests with typeswitch. If you want to do different things based on the > node type, it should be a good alternative to the string-based > approach: > > declare function local:node-type( > $node as node() > ) as xs:string { > typeswitch($node) > case element() return 'element' > case comment() return 'comment' > case attribute() return 'attribute' > case text() return 'text' > case document-node() return 'document-node' > case processing-instruction() return 'processing-instruction' > default return error() > }; > for $node in (<a/>, <!-- x -->) > return local:node-type($node) > > Cheers, > Christian > > > > > On Fri, Nov 9, 2018 at 7:17 PM Graydon Saunders <graydonish at gmail.com> > wrote: > > > > Hi Bridger -- > > > > Those are helpful, thanks! > > > > I was hoping for a built-in (extenstion!) function on the (possibly > mistaken) supposition that BaseX just knows that things are in the internal > representation and would nigh-certainly be quicker to have something that > returns that value directly. > > > > -- Graydon > > > > On Fri, Nov 9, 2018 at 1:10 PM Bridger Dyson-Smith < > bdysonsmith at gmail.com> wrote: > >> > >> HI Graydon - > >> > >> it isn't a builtin function, but maybe the functx:node-kind() and > functx:sequence-type() functions are what you want[1,2]? > >> Hope that helps. > >> > >> Best, > >> Bridger > >> [1] http://www.xqueryfunctions.com/xq/functx_node-kind.html > >> [2] http://www.xqueryfunctions.com/xq/functx_sequence-type.html > >> > >> On Fri, Nov 9, 2018 at 12:40 PM Graydon Saunders <graydonish at gmail.com> > wrote: > >>> > >>> Hi! > >>> > >>> I am overcome with the cabbage-nature today, because I can't find this > in the docs. > >>> > >>> I am convinced there's a way to go: > >>> > >>> (//some-element/node()) ! fn:node-type(.) > >>> > >>> and get a sequence of "element(),element(),text()..." but do not know > what the actual function is called. (it's not node-type()!) > >>> > >>> How ought I to be approaching this? > >>> > >>> Thanks! > >>> Graydon > >>> > >>> >
Written for BaseX 9.1. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery