Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2005, 08:16 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default name attribute for <xsl:element>

Hi everyone!

I came across a little problem while trying to output an <xsl:element>. I think the easiest way to make myself clear is an example.

--- XML source document ---
<?xml version="1.0"?>
<root>
  <node name="example">
    <child name="baby1">Some text here</child>
  </node>
  <node name="example">
    <child name="baby2">Some text here</child>
  </node>
</root>
--- End XML source document ---

--- Desired XML output ---
<?xml version="1.0"?>
<examples>
  <example>
    <child name="baby1">Some text here</child>
  </example>
  <example>
    <child name="baby2">Some text here</child>
  </example>
</examples>
--- End desired XML output ---

So I tried to use the following stylesheet to get the name of the element from the @name attribute of the node.

--- XSL used ---
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:apply-templates select="root"/>
  </xsl:template>
  <xsl:template match="root">
    <examples>
      <xsl:for-each select="node">
        <xsl:apply-templates select="."/>
      </xsl:for-each>
    </examples>
  </xsl:template>
  <xsl:template match="node">
    <!--
         HERE I try to name the <xsl:element>
         using the @name of the current node
    -->
    <xsl:element name="@name">
    <xsl:apply-templates select="child::node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="child">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>
--- End XSL used ---

Sadly, this fails... So I also tried to put the @name in an <xsl:variable name="var"> and use $var to name the <xsl:element> with no success either.

I found a workaround using <xsl:text> but I just wonder if there'a a more elegant way to make this work.

Thanks in advance,
RushMan


Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
__________________
Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
 
Old June 9th, 2005, 08:39 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You want

<xsl:element name="{@name}">


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 9th, 2005, 09:04 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Dear Mister Kay,

You are my guru. Thanks a million. I feel so stupid...

Rushman

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
create <ul> with alternating class on <li> element Brian Campbell XSLT 2 November 3rd, 2006 06:07 PM
<xsl:attribute> vs {} berna_isct XSLT 1 April 6th, 2006 03:30 PM
<xsl:for-each> with a variable select="attribute" BeneathClouds XSLT 1 August 1st, 2005 03:36 AM
problem using <xsl:attribute> arvin XSLT 2 July 21st, 2003 03:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.