Is this impossible in XSLT?

R

Richard Tobin

I want to generate an element with a namespace binding that does not
occur in the input or the stylesheet (but which depends on something
in the input), so that a qname in one of its attributes will be
interpreted correctly.

A simplified example: from

<declare targetNamespace="http://example.org" name="foo" type="bar"/>

I want to generate

<schema targetNamespace="http://example.org" xmlns:p="http://example.org">
<element name="foo" type="p:bar"/>
</schema>

As far as I can tell there is no reliable way to do this in XSLT 1.0;
is that right?

-- Richard
 
J

Janwillem Borleffs

Richard said:
I want to generate

<schema targetNamespace="http://example.org"
xmlns:p="http://example.org"> <element name="foo" type="p:bar"/>
</schema>

As far as I can tell there is no reliable way to do this in XSLT 1.0;
is that right?

<xsl:template match="declare">
<xsl:element name="schema">
<xsl:attribute name="targetNamespace">
<xsl:value-of select="@targetNamespace" />
</xsl:attribute>

<xsl:attribute name="xmlns:p">
<xsl:value-of select="@targetNamespace" />
</xsl:attribute>

<xsl:element name="element">
<xsl:attribute name="name">
<xsl:value-of select="@name" />
</xsl:attribute>

<xsl:attribute name="type">
<xsl:value-of select="concat('p:', @type)" />
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:template>


JW
 
R

Richard Tobin

Janwillem Borleffs said:
<xsl:attribute name="xmlns:p">
<xsl:value-of select="@targetNamespace" />
</xsl:attribute>

This doesn't work; you can't create namespace declarations with xsl:attribute.

Section 7.1.3 says:

XSLT processors may make use of the prefix of the QName specified in
the name attribute when selecting the prefix used for outputting the
created attribute as XML; however, they are not required to do so
and, if the prefix is xmlns, they must not do so. Thus, although it
is not an error to do:

<xsl:attribute name="xmlns:xsl" namespace="whatever">http://www.w3.org/1999/XSL/Transform</xsl:attribute>

it will not result in a namespace declaration being output.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top