XSLT: preserving an element with all its attributes

Discussion in 'XML' started by Martin Plantec, Oct 28, 2005.

  1. This is a very simple question, from a beginner in XSLT.

    Suppose my XML says

    <para>The word <a href="there.html">link</a> goes there.</para>

    What XSLT rule would preserve the a element with all attributes?

    By now I have learned I can write:

    <xsl:template match="a">
    <a><xsl:apply-templates /></a>
    </xsl:template>

    but this looses the attribute. And including no special rule for the a
    element will suppress it from output. And also: if I have a 10 elements
    for which this is what I want to do, is there a shorthand to treat all
    of them in this way (i.e. keeping everything as in source).

    Thanks!

    Martin
    Martin Plantec, Oct 28, 2005
    #1
    1. Advertising

  2. Hey! I just found the answer by reading another post in this group.
    Thanks.

    PS: here it is:

    <xsl:template match="a">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>
    Martin Plantec, Oct 28, 2005
    #2
    1. Advertising

  3. Martin Plantec wrote:


    > <para>The word <a href="there.html">link</a> goes there.</para>
    >
    > What XSLT rule would preserve the a element with all attributes?


    The identity transformation is even mentioned in the XSLT 1.0 specification:
    <http://www.w3.org/TR/xslt#copying>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Oct 28, 2005
    #3
  4. Thanks a lot. And to answer my original question entirely, several
    elements can be treated that way at once, by using:

    <xsl:template match="a|element2|element3">
    Martin Plantec, Oct 28, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. longinus
    Replies:
    1
    Views:
    507
    Anton Spaans
    Nov 12, 2004
  2. TTroy
    Replies:
    16
    Views:
    776
    Peter Nilsson
    Jan 31, 2005
  3. Replies:
    4
    Views:
    498
  4. hisachu
    Replies:
    0
    Views:
    1,523
    hisachu
    Sep 1, 2010
  5. thunk
    Replies:
    1
    Views:
    290
    thunk
    Mar 30, 2010
Loading...

Share This Page