Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Newbie XSLT question

  • 03-12-2011 01: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, Registered Users 2 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,688 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