output node with subnodes "as-is"

Discussion in 'XML' started by Kristofer Andersson, Dec 3, 2004.

  1. I am using xslt to transform some parts of a document but want to
    output some parts of the document just like they are (node with all
    subnodes and attributes).

    Below are the templates I am currently using. The only problem I have
    is that the value of parent nodes will be the value of all subnodes ie
    "<parentnode><childnode>value</childnode></parentnode>" will become
    "<parentnode>value<childnode>value</childnode></parentnode>" after
    transformation. Can I avoid this? Or can this be done in a more simple
    way than the somewhat clumsy templates below?

    <xsl:template match="mySubtree">
    <xsl:element name="subtreeWrapperNode">
    <xsl:apply-templates mode="subNodes" select="." />
    </xsl:element>
    </xsl:template>

    <xsl:template mode="subNodes" match="*">
    <xsl:for-each select="*">
    <xsl:element name="{name()}">
    <xsl:for-each select="@*">
    <xsl:attribute name="{name()}"><xsl:value-of select="."
    /></xsl:attribute>
    </xsl:for-each>
    <xsl:value-of select="." />
    <xsl:apply-templates mode="subNodes" select="." />
    </xsl:element>
    </xsl:for-each>
    </xsl:template>
    Kristofer Andersson, Dec 3, 2004
    #1
    1. Advertising

  2. Use xsl:copy-of.

    <!-- Category: instruction -->
    <xsl:value-of
    select = string-expression
    disable-output-escaping = "yes" | "no"/>

    From: <http://www.w3.org/TR/xslt#value-of>

    In article <>,
    (Kristofer Andersson) wrote:

    > I am using xslt to transform some parts of a document but want to
    > output some parts of the document just like they are (node with all
    > subnodes and attributes).
    >
    > Below are the templates I am currently using. The only problem I have
    > is that the value of parent nodes will be the value of all subnodes ie
    > "<parentnode><childnode>value</childnode></parentnode>" will become
    > "<parentnode>value<childnode>value</childnode></parentnode>" after
    > transformation. Can I avoid this? Or can this be done in a more simple
    > way than the somewhat clumsy templates below?
    >
    > [code snipped...]
    The Square Root of Negative One, Dec 3, 2004
    #2
    1. Advertising

  3. as another respondentsaid, if you just want the whole subtree
    unchanged then you can just do

    <xsl:template match="mySubtree">
    <xsl:copy-of select="."/>
    </xsl:template>

    and need nothing else. A more usual case is where you want templates
    that do an identity transform but then have higher priority templates
    for certain special attributes that do something other than copy.
    even there your code is very verbose

    <xsl:element name="{name()}">

    you can use
    <xsl:copy>

    for that
    <xsl:for-each select="@*">
    <xsl:attribute name="{name()}"><xsl:value-of select="."
    /></xsl:attribute>

    that's

    <xsl:copt-of select="@*"/>

    <xsl:value-of select="." />
    <xsl:apply-templates mode="subNodes" select="."
    />

    That would mangle your input
    the value-of gives the string value of the current node, which is all
    the character content of all the descendants then you process all the
    child elements again (so you'll get much of teh content again in a
    typical case)

    The XSLT spec has an example identity transform.

    David
    David Carlisle, Dec 3, 2004
    #3
    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. Replies:
    0
    Views:
    1,408
  2. Chris Angelico
    Replies:
    2
    Views:
    62
    Chris Angelico
    Feb 19, 2014
  3. Mark Lawrence
    Replies:
    0
    Views:
    43
    Mark Lawrence
    Feb 19, 2014
  4. Chris Angelico
    Replies:
    0
    Views:
    40
    Chris Angelico
    Feb 19, 2014
  5. Dan Goodman
    Replies:
    0
    Views:
    57
    Dan Goodman
    Feb 19, 2014
Loading...

Share This Page