How to replace a tag in xml document with xslt

Discussion in 'XML' started by sandls816@gmail.com, Nov 17, 2005.

  1. Guest

    Hi, I am quite new to xsl, so maybe there is a pretty easy solution to
    this, but I couldn't find a solution anywhere.
    Basically I am trying to replace a pattern of tags with other tag. For
    example if my xml looks like this:

    <a>
    <b>
    <c>
    <c>some text</c>
    </c>
    </b>
    <a>

    And I want to replace <c><c> with <d> so my output would be
    <a>
    <b>
    <d>some text</d>
    </b>
    </a>

    The problem is that I don't know an absolute path to c/c it could be
    anything, I just want to match on <c><c> no matter how far down it is.
    Can this be done with xslt.
    Thank you in advance
    , Nov 17, 2005
    #1
    1. Advertising

  2. > The problem is that I don't know an absolute path to c/c it could be
    > anything, I just want to match on <c><c> no matter how far down it is.
    > Can this be done with xslt.


    This isn't heavily tested, but it might get you started in the right
    direction. Someone here in the group with more XSL experience can
    probably provide a better solution:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!-- This matches <c> elements that are parents of another <c>
    element, and replaces them with a <d> element containing the text
    nodes of the child <c> element. -->
    <xsl:template match="c[child::c]">
    <d><xsl:value-of select="c/text()"/></d>
    </xsl:template>

    <!-- This matches everything else and just copies it to
    the output. -->
    <xsl:template match="*">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

    -- Lars

    --
    Lars Kellogg-Stedman <>
    This email address will expire on 2005-11-23.
    Lars Kellogg-Stedman, Nov 18, 2005
    #2
    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. shruds
    Replies:
    1
    Views:
    779
    John C. Bollinger
    Jan 27, 2006
  2. Tony Prichard
    Replies:
    0
    Views:
    722
    Tony Prichard
    Dec 12, 2003
  3. Manish Hatwalne
    Replies:
    1
    Views:
    397
    Martin Honnen
    Jul 13, 2004
  4. Replies:
    4
    Views:
    667
  5. Replies:
    18
    Views:
    2,753
    Joseph Kesselman
    Oct 4, 2006
Loading...

Share This Page