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

Discussion in 'XML' started by =?UTF-8?b?5byg6Z+h5q2m?=, Oct 22, 2006.

  1. 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;
     
    =?UTF-8?b?5byg6Z+h5q2m?=, Oct 22, 2006
    #1
    1. Advertising

  2. =?UTF-8?b?5byg6Z+h5q2m?=

    roy axenov Guest

    ??? wrote:
    > 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.

    --
    roy axenov
     
    roy axenov, Oct 22, 2006
    #2
    1. Advertising

  3. =?UTF-8?b?5byg6Z+h5q2m?=

    DeepthiShri 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;


    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.
     
    DeepthiShri, Oct 23, 2006
    #3
  4. =?UTF-8?b?5byg6Z+h5q2m?=

    DeepthiShri 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;


    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.
     
    DeepthiShri, Oct 23, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jordan Willms
    Replies:
    2
    Views:
    849
    Richard Tobin
    Nov 9, 2004
  2. Bostonasian
    Replies:
    1
    Views:
    830
    Joris Gillis
    Sep 18, 2005
  3. bprimuslevy
    Replies:
    1
    Views:
    462
    Soren Kuula
    Aug 23, 2006
  4. Replies:
    1
    Views:
    501
    Joseph Kesselman
    Nov 5, 2007
  5. Stefan_W
    Replies:
    1
    Views:
    8,339
    Stefan_W
    Nov 5, 2008
Loading...

Share This Page