Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Newbie XSLT question

Options
  • 03-12-2011 1:36pm
    #1
    Closed Accounts Posts: 324 ✭✭


    Hi
    Can anyone help me with this XSLT problem?

    In my XML I have a tag called class, and it has a name attribute. How do I get this name attribute
    <class name="Ds0Bundle">
                  ... stuff processed by XSLT in here 
     </class>
    

    I am doing this in the xsl file:
    <xsl:template match="class">
     			<class name="@name">	
     				<xsl:apply-templates />
     			</class> 			
     </xsl:template>
    
    but it's giving me literally <class name="@name&quot;> in the output file. How do I get the actual value of name?

    If anyone knows XSLT would be very grateful!
    Thanks!


    PS I can get it by doing
    <class>
    <xsl:value-of select="@name" />
    </class>
    

    but I need to have it in the format e.g. <class name="myClass">, there's a program that takes this as input and it would be much better to have it in that format.


Comments

  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    It could be that 'name' is a reserved attribute in xsl or something like that. Maybe using a namespace would help.

    Try workarounds like using a variable of referencing the attribute name.
    <xsl:template match="class"> 
    <class> 
    <xsl:for-each select="*"> 
    <xsl:variable name="LocalName" select="local-name()"/>
    <xsl:attribute name="{$LocalName}"> 
    <xsl:value-of select="."/> 
    </xsl:attribute> 
    </xsl:for-each> 
    </class> 
    </xsl:template> 
    


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    IIRC when you have it in quotation marks as part of a tag like that you need to use {} brackets.

    So I think something like
    <xsl:template match="class">
     			<class name="{@name}">	
     				<xsl:apply-templates />
     			</class> 			
     </xsl:template>
    

    should do it.


  • Closed Accounts Posts: 324 ✭✭radioactiveman


    Thanks a million guys
    Sorry for the late reply.

    I actually got around it by copying the whole element. But it's not really the solution to the problem - this does it properly - appreciate it


Advertisement