Re: [xsl] return type of a function returning namespaces?

Subject: Re: [xsl] return type of a function returning namespaces?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 9 Jun 2022 04:41:34 -0000
Maybe just use:

   as="node()"

This is still better than  not declaring the return type of the function at
all.

Here is a contrived example of using such a function:

<xsl:stylesheet version="2.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform";
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:element name="{concat(name(my:fun()), ':'[name(my:fun())])}myElem"
                 namespace="{my:fun()}"/>
  </xsl:template>

  <xsl:function name="my:fun" as="node()">
    <xsl:namespace name="xyz" >http://example.com</xsl:namespace>
  </xsl:function>
</xsl:stylesheet>

Cheers,
Dimitre

On Wed, Jun 8, 2022 at 8:31 PM Graydon graydon@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hello!
>
> So as part of an incumbent process, it turns out that the namespaces all
> have to be declared on the document element or the downstream processing
> fails.  That's not correct, but I can't fix the process, so I wrote the
> following to make sure all the required namespaces could be declared on
> the document element.
>
>
> <xsl:function name="local:getNamespaces">
>   <xsl:param name="thisDoc" as="document-node()"/>
>   <!-- group descendant elements by namespace URI because we don't
> completely trust the prefix to be unique -->
>   <xsl:for-each-group select="$thisDoc/descendant-or-self::element()"
>     group-by="namespace-uri-from-QName(resolve-QName(name(.), .))">
>     <xsl:variable name="QName" as="xs:QName"
> select="current-group()[1]/resolve-QName(name(.), .)"/>
>     <xsl:variable name="prefix" as="xs:string?"
> select="prefix-from-QName($QName)"/>
>     <xsl:variable name="namespace" as="xs:anyURI?"
> select="namespace-uri-from-QName($QName)"/>
>     <!-- don't create a namespace for the default namespace; only if
> there's a defined prefix (empty string = false) -->
>     <xsl:if test="$prefix">
>       <xsl:namespace name="{$prefix}" select="$namespace"/>
>     </xsl:if>
>   </xsl:for-each-group>
> </xsl:function>
>
>
> It works, but I find myself wondering how I could declare the return
> type of the function.
>
> as="namespace()" does not work; namespace() isn't a node test.
>
> I realize this is a weird case that's a workaround for a bug elsewhere
> in the process, but wanting to be able to extract and manipulate a
> sequence of namespaces seems like a reasonable thing for a function to
> do.
>
> What am I missing?
>
>
> --
> Graydon Saunders  | graydonish@xxxxxxxxx
> CC&s oferC)ode, C0isses swC! mC&g.
> -- Deor  ("That passed, so may this.")
>
>
>

--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread