Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT : getting the prefix of an element?

In XSLT 1.0, you can get the local name or the namespaceUri of an XML element using the functions:

string local-name (node)

and

string namespace-uri(node)

but is there a standard function to get the prefix of an element having a qualified name ?

like image 804
Pierre Avatar asked Dec 22 '22 06:12

Pierre


2 Answers

Not as far as I know. If you're sure the node name has a prefix, you can use this:

substring-before(name(), ':')

or this, if you are not sure:

substring-before(
  name(), 
  concat(':', local-name())
)

The latter expression is based on the fact that substring-before() returns the empty string when the searched string is not found. This way it will work correctly for prefixed and unprefixed names.

like image 174
Tomalak Avatar answered Dec 24 '22 19:12

Tomalak


Generally speaking, if you care about an element's prefix, you're doing it wrong. You should only care about what namespace it belongs to.

In your comment, you note that the API you're talking to requires you to provide a namespace and a namespace prefix. That's true, but the API you linked to doesn't actually care what the namespace prefix is - in fact, it will generate a namespace prefix randomly if you don't provide one.

If you're generating entire XML documents, it's nice if you have a one-to-one mapping of prefixes to namespace URIs; it makes the output easier to understand. But it really doesn't matter what those prefixes are.

like image 44
Robert Rossney Avatar answered Dec 24 '22 19:12

Robert Rossney