Re: [xsl] Re: xsl:namespace

Subject: Re: [xsl] Re: xsl:namespace
From: Florent Georges <lists@xxxxxxxxxxxx>
Date: Sat, 21 Feb 2009 12:32:10 +0000 (GMT)
Jesper Tverskov wrote:
> Florent writes:

> "I used xsl:namespace almost exclusively in meta-stylesheets."
> I guess that means stylesheets creating stylesheets as
> output. To me that is exciting and novel stuff.

> If you can make a small example of such a meta-stylesheet using
> xsl:namespace, it would save my day.

  Ok.  Let's say we have a document type like the following
excerpt (inspired by a real example I created on a project, but I
can't provide real excerpts):

    <para i18n="total">
       <amount name="price" select="$var/to/amount"/>
    </para>

  All the documents of the enterprise (contracts, invoices, etc.)
are created using this document type.  But those documents are
not used as is in the system, they are transformed to stylesheets
first (by a stylesheet, of course) and those stylesheets are used
by the system to generate actual documents.

  At some point in the generated stylesheet, you will end up with
something like the following:

    <xsl:value-of select="impl:format-amount($var/to/amount)"/>

  I kept this example simple, but the expression can of course
accesses element in some namespaces, use functions in some
namespaces...  So you have to copy all the namespace bindings in
scope on the element 'amount' to the element you copy the
expression in the generated stylesheet (here the xsl:value-of.)

  So in the meta-stylesheet, you would have something like the
following:

    <!-- prefix 'a' is an alias for 'xsl' -->
    <xsl:template match="amount">
       ...
       <a:value-of select="impl:format-amount({ @select })">
          <xsl:sequence select="ms:copy-namespaces(.)"/>
       </a:value-of>
       ...
    </xsl:template>

where ms:copy-namespace() is something like:

    <!-- ...as="namespace-node()" -->
    <xsl:function name="ms:copy-namespace">
       <xsl:param name="ctxt" as="element()"/>
       <xsl:for-each select="in-scope-prefixes($ctxt)">
          <xsl:sequence select=""/>
          <xsl:namespace name="{ . }" select="
              namespace-uri-for-prefix(., $ctxt)"/>
       </xsl:for-each>
    </xsl:function>

  Disclaimer: I wrote those examples in my web browser, but the
idea is there.

  Regards,

--
Florent Georges
http://www.fgeorges.org/

Current Thread