471,122 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

xsl:attribute w/variable as 'name' attribute?

Is there anyway to use the contents of a variable as the value of the
'name' attribute of an <xsl:attribute>?

Suppose I have this XML input:

<Foo>
<Bar>Baz</Bar>
</Foo>
What I'd like to generate (ignore any <xs:include> requirements) is:

<xs:element ref="Baz:Baz" xmlns:Baz="A:B:C:Baz"/>
Here's what I have so far:

<xsl:template match="Foo">
<xsl:element name="xs:element">
<xsl:attribute name="ref">
<xsl:value-of select="Bar"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="Bar"/>
</xsl:attribute>
<xsl:variable name="bazNamespace">
<xsl:text>xmlns:</xsl:text>
<xsl:value-of select="Bar"/>
</xsl:variable>
<xsl:attribute name="How-{$bazNamespace}-DoesntWork">
<xsl:text>A:B:C:</xsl:text>
<xsl:value-of select="Bar"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
Obviously I could manually build the <xs:element> with a bunch of
<xsl:text>, but that's crummy. Any ideas?

Apr 21 '06 #1
3 1703


da*******@gmail.com wrote:

Suppose I have this XML input:

<Foo>
<Bar>Baz</Bar>
</Foo>
What I'd like to generate (ignore any <xs:include> requirements) is:

<xs:element ref="Baz:Baz" xmlns:Baz="A:B:C:Baz"/>


Most can be simply done with

<xsl:template match="Foo">
<xs:element
xmlns:xs="http://www.w3.org/2001/XMLSchema"
ref="{Bar}:{Bar}" />
</xsl:template>

The xmlns:Baz="A:B:C:Baz" will be tricky however as namespace
declarations in terms of the XSLT/XPath tree model are not attributes.

I am not sure that there is a way with XSLT 1.0 to achieve what you
want, if the prefix and value are to be generated from the source input.
If it were static you could simply do e.g.

<xsl:template match="Foo">
<xs:element
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:Baz="A:B:C:Baz"
ref="{Bar}:{Bar}" />
</xsl:template>

See also <http://www.dpawson.co.uk/xsl/sect2/N5536.html#d6476e2293>
which suggests XSLT 2.0 has a solution.

There you could do e.g.

<xsl:template match="Foo">
<xs:element
xmlns:xs="http://www.w3.org/2001/XMLSchema"
ref="{Bar}:{Bar}">
<xsl:namespace name="{Bar}"><xsl:value-of select="concat('A:B:C:',
Bar)" /></xsl:namespace>
</xs:element>
</xsl:template>

and the result is

<xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:Baz="A:B:C:Baz" ref="Baz:Baz"/>
http://www.saxonica.com/ provides an XSLT 2.0 processor.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 21 '06 #2
Martin Honnen wrote:
I am not sure that there is a way with XSLT 1.0 to achieve what you
want, if the prefix and value are to be generated from the source input.
XSLT 1.0 doesn't have a way to generate namespace declarations
("namespace nodes") programmatically. You may be able to find an
extension function in your implementation that will do so. If not...
well, you can _try_ creating the declaration as an attribute node,
giving it the "namespace for namespaces", and see if your implementation
will let you get away with it.
See also <http://www.dpawson.co.uk/xsl/sect2/N5536.html#d6476e2293>
which suggests XSLT 2.0 has a solution.


It does. Known hole in the spec, closed in 2.0.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 21 '06 #3
Thanks for the suggestions, Martin. They didn't work as-is, as I'm
generating a schema that references additional external schemas, thus I
want <xsl:element>s, not <xs:elements>, which brings a few restrictions
that prefent your sample from working, and I wanted to output a "ref"
attribute, rather than use one inline to refer to another node;
however, the XSLT 2.0 <xsl:namespace> was indeed the key to getting
what I wanted.

Here's what I ended up with:

<xsl:template match="Foo">
<xsl:element name="xs:element">
<xsl:attribute name="ref">
<xsl:value-of select="concat(Bar, ':', Bar)"/>
</xsl:attribute>
<xsl:namespace name="{Bar}">
<xsl:value-of select="concat('A:B:C:', Bar)"/>
</xsl:namespace>
</xsl:element>
</xsl:template>
Thanks again for the help!

David

Apr 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Xeon | last post: by
4 posts views Thread by sylvain.loiseau | last post: by
10 posts views Thread by Tjerk Wolterink | last post: by
2 posts views Thread by sucheta.phatak | last post: by
5 posts views Thread by John Gordon | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.