DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Don. Guest

    Problem with XML


    Hi all,

    I've only just begun using XML and have run into a problemn that I can't
    find a way around. Sorry in advance for the length of this post.

    I want to apply an XSL file to an XML file I have that will display the result
    in HTML format (so far so good). The XSL file has to insert a pair of radio
    (option) buttons into the HTML to correspond with each element. The problem
    is that I want to name the option buttons using the 'name' attribute of the
    element, and as the value of the 'name' attribute has spaces in it I wish
    to replace them with underscores. I've tried using the 'translate' function,
    ie:

    <input type="radio" value="0" checked="checked">
    <xsl:attribute name="name">
    <xsl:value-of select="translate('name',' ','_')"/>
    </xsl:attribute>
    </input>

    but get the error; "unknown method-->'"translate('name'<--'

    I've also tried writing a Javascript function:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="http://www.itcg-advisor.co.uk" version="1.0">
    <msxsl:script language="javascript" implements-prefix="user">
    <![CDATA[
    function RemoveSpaces(sString){
    var newString=sString.match(" ","_");
    return newString;
    }
    ]]>
    </msxsl:script>

    and calling it using:

    <input type="radio" value="0" checked="checked">
    <xsl:attribute name="name">
    <xsl:value-of select="user:RemoveSpaces(name)"/>
    </xsl:attribute>
    </input>

    but the HTML when displayed in the browser simply prints out the above function
    at the top of the web-page and does not seem to get as far as attempting
    to alter the value of the 'name' attribute.

    I realise that this is most probably an elementary error on my part, but
    if anyone out there could point out to me where I am going wrong in the above
    code I'd be grateful.

    Sorry about the size of this post, and thanks in advance,

    Don.

  2. #2
    Rohit Wason Guest

    Re: Problem with XML


    Don,

    Try these two steps:

    1) Use templates for the attributes (I have assumes you have a tag called
    NODE and the attr called "name"). In this template, call your function RemoveSpaces
    using xsl:eval.
    -----------------------------------------------------------------------
    <xsl:template match="NODE/@name">
    <input type="radio" value="0" checked="checked">
    <xsl:attribute name="name">
    <xsl:eval>Removespaces(this)</xsl:eval>
    </xsl:attribute>
    </input>
    </xsl:template>
    -----------------------------------------------------------------------

    2) Make the function (you have done it right, but I use xsl:script)

    -----------------------------------------------------------------------
    <xsl:script language="javascript" implements-prefix="user">
    <![CDATA[
    function RemoveSpaces(sString){
    var newString=sString.match(" ","_");
    return newString;
    }
    ]]>
    </xsl:script>
    -----------------------------------------------------------------------

    It should work.
    I didn't try this example now, but I remember I used this technique sometime
    back

    Cheers,
    Rohit

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links