"Adrian Grigore" <adrian(_at_)lobstersoft(_dot_)com> wrote in message
news:5(_dot_)1(_dot_)1(_dot_)6(_dot_)0(_dot_)20030601225755(_dot_)00bdfe18(_at_)lobstersoft(_dot_)com(_dot_)(_dot_)(_dot_)
Hi,
I am currently working with xsl-metaprogramming (an xsl template is
compiling another language into xsl) and need to generate a template that
behaves differently when it's current node is an attribute node than when
it's current node is any other kind of node.
In other words, I am looking for an easy way to check within the
template example if the template's match attribute is something like
/somenode/@id
in which case it matched an attribute node or something like
/somenode/somechildnode
in which case it matched a element node.
This problem is rather artificial -- you can just have two templates -- one
matching elements and one matching attributes. In this way you do not have
to code anything in order to determine the type of node -- the XSLT
processor does this for you. If the template matching attributes is
instantiated, this means that the current node is an attribute.
If due to some (difficult to justify) reasons you have decided to process
both elements and attributes (and other types of nodes excluding only
namespace nodes, which cannot be matched) within a single template, then the
way to determine the type of node is as follows:
- element node
self::*
- comment node
self::comment()
- processing instruction node
self::processing-instruction()
- text node
self::text()
- root node
not(..)
- attribute node
count(. | ../@*) = count(../@*)
OR
count(. | ../@*[name() = name(current())]) = 1
OR
using variations of the above with generate-id()
- namespace node
count(. | ../namespace::*) = count(../namespace::*)
You may find these expressions used in a snippet that builds one of the many
possible XPath expressions that select a specific node:
http://www.topxml.com/code/default.asp?p=3&id=v20010323001030
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list