0

I have a namespace that is used both dynamically and statically.

I'd like to define it only once.

Dynamic use is like this:

<xsl:variable name="fpml-ns" select="'http://www.fpml.org/2005/FpML-4-2'"/>
<xsl:function name="qt:some-function">
    <xsl:param name="pd"/>
    <xsl:sequence select="qt:other-function($fpml-ns, $pd)"/>
</xsl:function>

This is ultimately being used to set a namespace on an element tag, which is then returned as the function result. The other-function can be used with many namespaces, hence it is dynamic.

In the same XSLT file, static use is like this - the namespace only applies to a specific result document within the XSLT:

<SWBML xmlns="http://www.fpml.org/2005/FpML-4-2" xsl:use-attribute-sets="swbml.ir">

So the string "http://www.fpml.org/2005/FpML-4-2" appears twice in my XSLT file - and whilst not a disaster I find the duplication suboptimal.

I've tried to make the SWBML element dynamic too, i.e:

<element name="SWBML" namespace="${fpml-ns}" use-attribute-sets="swbml.ir">

However as per this post: How can I dynamically set the default namespace declaration of an XSLT transformation's output XML?

Children do not inherit dynamic namespaces as they would with the static definition.

I have considered the rather ugly workaround of referencing the current document and reading it like so:

<xsl:variable name="fpml-ns" select="namespace-uri(document('')//node()[local-name()='SWBML'])"/>

This works, but causes problems when trying to do schema-aware valuation of the inputs, as the current document is then validated using the namespace of the result leading to this problem - Saxon Prematurely Evalutes xsl:attribute-set

Is there a sensible way to achieve a single defintion of the namespace that I can then reference both statically and dynamically?

Phil
  • 592
  • 6
  • 15

1 Answers1

1

The only solution that comes to mind is to use an XML entity:

<!DOCTYPE xsl:stylesheet [
  <!ENTITY ns "http://www.fpml.org/2005/FpML-4-2">
]>

....

<SWBML xmlns="&ns;" xsl:use-attribute-sets="swbml.ir">

....

<xsl:variable name="fpml-ns" select="'&ns;'"/>

I'm no great fan of XML entities but some people swear by them...

Michael Kay
  • 156,231
  • 11
  • 92
  • 164