xslt: how to use value from xml data as an attribute value in output html?

G

Guest

Hello. I am a newbie trying to get my first XLST script working. I already
know how to do this:

Source XML:
<category id="a0104">
<name>Oil and Gas</name>
</category>

XSLT:
<xsl:for-each select="category">
<p>
<input type="checkbox" />
<xsl:value-of select="name"/>
</p>
</xsl:for-each>

The output is:
<p><input type="checkbox">Oil and Gas</p>

But what if I wish to get the output?

<p><input type="checkbox" id="a0104"/>Oil and Gas</p>

I have experimented several times, it seems <xsl:value-of> can be only
used inside an element but cannot be used in element attribute string.

e.g. This will not work:

<xsl:for-each select="category">
<p>
<input type="checkbox" id="<xsl:value-of select=\"@id\"/>"/>
<xsl:value-of select="name"/>
</p>
</xsl:for-each>

P.S. I tried many tutorials and none of them cover this topic. The last
idea I can think of is to use

<input type="checkbox"><xsl:value-of select="name"/></input>

And later use a javascript:

inputs = document.getElementsByTagName("input");
for (var i=0; i < length(inputs); i++)
inputs.name = inputs.innerText;
 
R

roy axenov

??? said:
Source XML:
<category id="a0104">
<name>Oil and Gas</name>
</category>

XSLT:
<xsl:for-each select="category">
<p>
<input type="checkbox" />
<xsl:value-of select="name"/>
</p>
</xsl:for-each>

Is there a good reason you're using for-each? If not (as I
suspect is the case here), drop it. You'll save yourself a
lot of trouble down the road if you get into the right
mindset from the start. Before doing something right away,
ask yourself: 'Can I do that using templates/predicates?'
<p><input type="checkbox">Oil and Gas</p>

But what if I wish to get the output?

<p><input type="checkbox" id="a0104"/>Oil and Gas</p>

<input type="checkbox">
<xsl:attribute
name="id"><xsl:value-of
select="@id"/></xsl:attribute>
</input>

or:

<input type="checkbox" id="{@id}"/>

But attribute value templates is another thing you
shouldn't fall in love with. (The reason being that there
are quite a few places where you might expect them to work.
And where they don't.)
P.S. I tried many tutorials and none of them cover this
topic. The last idea I can think of is to use

Actually, some of them do.
 
D

DeepthiShri

Hello. I am a newbie trying to get my first XLST script working. I already
know how to do this:

Source XML:
<category id="a0104">
<name>Oil and Gas</name>
</category>

XSLT:
<xsl:for-each select="category">
<p>
<input type="checkbox" />
<xsl:value-of select="name"/>
</p>
</xsl:for-each>

The output is:
<p><input type="checkbox">Oil and Gas</p>

But what if I wish to get the output?

<p><input type="checkbox" id="a0104"/>Oil and Gas</p>

I have experimented several times, it seems <xsl:value-of> can be only
used inside an element but cannot be used in element attribute string.

e.g. This will not work:

<xsl:for-each select="category">
<p>
<input type="checkbox" id="<xsl:value-of select=\"@id\"/>"/>
<xsl:value-of select="name"/>
</p>
</xsl:for-each>

P.S. I tried many tutorials and none of them cover this topic. The last
idea I can think of is to use

<input type="checkbox"><xsl:value-of select="name"/></input>

And later use a javascript:

inputs = document.getElementsByTagName("input");
for (var i=0; i < length(inputs); i++)
inputs.name = inputs.innerText;


Use element and attribute pair.
p
xsl:element name="input"
xsl:attribute name="type" checkbox /xsl:attribute
xsl:attribute name="id" xsl:value-of select="@id"/
/xsl:attribute
/xsl:element
xsl:value-of select="name"/
/p.
Removed "<" and ">" symbols for tags because of some application error while posting the reply.

Posted from http://www.topxml.com/renntp using reNNTP: the website based NNTP reader.
 
D

DeepthiShri

Hello. I am a newbie trying to get my first XLST script working. I already
know how to do this:

Source XML:
<category id="a0104">
<name>Oil and Gas</name>
</category>

XSLT:
<xsl:for-each select="category">
<p>
<input type="checkbox" />
<xsl:value-of select="name"/>
</p>
</xsl:for-each>

The output is:
<p><input type="checkbox">Oil and Gas</p>

But what if I wish to get the output?

<p><input type="checkbox" id="a0104"/>Oil and Gas</p>

I have experimented several times, it seems <xsl:value-of> can be only
used inside an element but cannot be used in element attribute string.

e.g. This will not work:

<xsl:for-each select="category">
<p>
<input type="checkbox" id="<xsl:value-of select=\"@id\"/>"/>
<xsl:value-of select="name"/>
</p>
</xsl:for-each>

P.S. I tried many tutorials and none of them cover this topic. The last
idea I can think of is to use

<input type="checkbox"><xsl:value-of select="name"/></input>

And later use a javascript:

inputs = document.getElementsByTagName("input");
for (var i=0; i < length(inputs); i++)
inputs.name = inputs.innerText;


Hi try using element and attribute xsl tags :

p
xsl:element name="input"
xsl:attribute name="type" checkbox /xsl:attribute
xsl:attribute name="id" xsl:value-of select="@id"/
/xsl:attribute
/xsl:element
xsl:value-of select="name"/
/p


Removed "<" and ">" symbols as it was giving error while posting the reply.


Posted from http://www.topxml.com/renntp using reNNTP: the website based NNTP reader.
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top