473,480 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1824


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4396
by: Xeon | last post by:
Hi, I'm trying this code snippet below but the parser (sablotron) returns error : <xsl:choose> <xsl:when test="some test case"> <xsl:variable name="test" select="0"/> </xsl:when>...
4
2567
by: sylvain.loiseau | last post by:
Hello Given a node set, I try to compute the total of the string-length value of each node. For instance, with : <xsl:for-each select="//q"> <!-- the length of each node is compute with:...
10
3790
by: Tjerk Wolterink | last post by:
The following code does not work: <xsl:variable name="width" select="form:image-width"/> <xsl:if test="$width>$max_image_width"> <xsl:variable name="width" select="$max_image_width"/> </xsl:if>...
1
1595
by: schaf | last post by:
Hello NG ! I have a big problem. I would like to go through a xml file in a xsl:for-each statement. for-each entry (ID) in the XML file i would like to call an xsl:function, which returns a...
2
1456
by: sucheta.phatak | last post by:
Hello, I am trying to use XSLT. Here is the problem that I am facing. My XML file: <Book level="1" name="Mapplicationtoc1"> ....... Some more tags </Book>
1
2149
by: Carlo Folini | last post by:
Hi, I have the following xsl: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template...
0
1613
by: pradip sonar | last post by:
Hello, I am having a problem with Xalan transforming a document containing Japanese characters. (An initial note: the problem does not occur on my Windows development machine, only on the...
0
1433
by: paragguptaiiita | last post by:
I have one problem regarding displaying text from XML file. I have a XSL variable named $p = (0 0 0) (0.82 0 0) (1.63 -0.01 0) (2.63 -0.01 0) (3.63 -0.01 0) (4.63 -0.01 0) (5.63 -0.02 0) (6.63 -0.02...
5
3881
by: John Gordon | last post by:
My XSLT files have many occurrences of this general pattern: <a> <xsl:attribute name="href"> <xsl:value-of select="xyz" /> </xsl:attribute> </a> When I execute an XSL transform, the...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.