RE: [xsl] Adding an element of a specific type

Subject: RE: [xsl] Adding an element of a specific type
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 18 Oct 2006 15:44:27 +0100
> How do I add an element of a specific type to the output.

Your real question is, how do I add:

(a) an xsi:type attribute whose value is a QName, together with
(b) a namespace node that binds the prefix used in that QName.

If the prefix used in the QName is the same as the prefix used in the
containing element, which is the case in your example, then I would expect
the namespace to be declared automatically, so you shouldn't need to do
anything special. Perhaps you over-simplified the example? What was the
actual XML output?

If the namespace is different, then it's still quite easy so long as you
know the namespace in advance. Just use a literal result element instead of
xsl:element. That is, change

<xsl:element name="ns1:Q">
        <xsl:attribute name="xsi:type">ns2:Q_Type</xsl:attribute>
        <xsl:apply-templates select="ns1:C" >
</xsl:element>

to

<ns1:Q xsi:type="ns2:Q_Type" xmlns:ns2="some-namespace">
  <xsl:apply-templates select="ns1:C" >
</xs1:Q>

That's because literal result elements copy namespaces from the stylesheet,
while xsl:element doesn't.

> (sorry, but I am not sure which processor I have, since it is 
> embedded in an existing application)

It's a good idea to find out. Use

<xsl:comment>XSLT Processor: <xsl:value-of
select="system-property('xsl:vendor')"/></xsl:comment>

> This e-mail and any attachment is for authorised use by the 
> intended recipient(s) only. It may contain proprietary 
> material

Please tell your lawyers to stop adding such daft nonsense to your emails,
it brings their profession and your employers into disrepute.

Michael Kay
http://www.saxonica.com/

Current Thread