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. Advertisements

  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. Advertisements

  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. Advertisements

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. wolf
    Replies:
    0
    Views:
    676
  2. Replies:
    0
    Views:
    1,923
  3. Johan Åhlén
    Replies:
    1
    Views:
    3,575
    Johan Åhlén
    Aug 15, 2003
  4. Pat Roy
    Replies:
    2
    Views:
    4,566
    backslashzero
    Mar 4, 2008
  5. =?Utf-8?B?RGFuIE5hc2g=?=

    adding subnodes to a tree recursively

    =?Utf-8?B?RGFuIE5hc2g=?=, Oct 16, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    887
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Oct 18, 2004
  6. Tjerk Wolterink
    Replies:
    2
    Views:
    1,726
    Dimitre Novatchev
    Aug 24, 2006
  7. Pat Roy

    IE Treeview - Checking & Unchecking All Subnodes

    Pat Roy, Apr 20, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    267
    Pat Roy
    Apr 20, 2004
  8. Chris Angelico
    Replies:
    2
    Views:
    166
    Chris Angelico
    Feb 19, 2014
Loading...