Conditional attribute in xsl:element


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Conditional attribute in xsl:element

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    2

    Conditional attribute in xsl:element

    Hi,

    I'm trying to generate a <body> tag that will have a couple of attributes that will only be needed based on the presence of a variable.

    This is what I have:

    <xsl:element name="body">
    <xsl:variable name="jsString" select="$sourcingInsertJavascriptString"/>
    <xsl:attribute name="class">body</xsl:attribute>

    <xsl:if test="$jsString">
    <xsl:attribute name="onclick">dynOutline()</xsl:attribute>
    <xsl:attribute name="language">Javascript1.2</xsl:attribute>
    </xsl:if>


    <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <xsl:apply-templates select="@*|node()" />
    </table>
    </xsl:element>



    This is not working for me. I am using $sourcingInsertJavascriptString variable elsewhere and it works correctly. Is this conditional adding of attributes to the body tag possible like this? I only want the onclick attribute added to the body tag if that variable is present.

    Thanks!
    Gordie

  2. #2
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    49
    This looks fine to me. There is nothing wrong with creating attributes conditionally as long as they are created before nested elements, which they are.
    Have you tried debugging your XSLT to check the value of 'sourcingInsertJavascriptString'!?
    As this would suggest that this is the cause of the attributes not being present in your output.

  3. #3
    Join Date
    May 2009
    Posts
    60
    not enough information about the xml file to see the error
    Please show the original xml

  4. #4
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    49
    Quote Originally Posted by xml-looser View Post
    not enough information about the xml file to see the error
    Please show the original xml
    You do not need the XML to see that the issue lies within the XSLT.

  5. #5
    Join Date
    May 2009
    Posts
    60
    there are so many opportunities where the error could be

    namespace
    a forgotten bracket
    your new variable even though there is already a

    <xsl:variable name="jsString" select="$sourcingInsertJavascriptString"/>

    and your choice other nodes

    <xsl:apply-templates select="@*|node()" />

    if you are not xml I show can not help you
    you do not need to use original values ​​only example values

    I still try to understand that you want to do
    I suspect you have a variable defined param

    <xslaram name="sourcingInsertJavascriptString" select="true()"/>
    here is only when true () is set
    this is param then be a toplevel element
    all other values ​​not go

    it is nonsense of the step between
    <xsl:variable name="jsString" select="$sourcingInsertJavascriptString"/>
    then the same here
    <xsl:if test="$sourcingInsertJavascriptString">

    otherwise it will only consider sourcing insert contains a javascript string value in $sourcingInsertJavascriptString

    such
    <xsl:variable name="jsString" select="contains($sourcingInsertJavascriptString,'//javascript')"/>

    which makes a string a bool value

    and param with value
    <xslaram name="sourcingInsertJavascriptString" select=" '//javascript' "/>


    This is forum is to volunteer base
    not to earn money

    sorry for my bad english

Similar Threads

  1. Replies: 1
    Last Post: 02-06-2008, 10:18 AM
  2. Replies: 1
    Last Post: 10-31-2005, 02:57 PM
  3. Replies: 1
    Last Post: 04-26-2001, 04:20 AM
  4. support for namespaces on attribute value
    By Noga Atsil in forum XML
    Replies: 1
    Last Post: 11-21-2000, 01:04 PM
  5. Using VB's Conditional Compilation Arguments
    By Larry Rebich in forum vb.announcements
    Replies: 0
    Last Post: 05-22-2000, 01:49 AM

Tags for this Thread

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center