XSL - XML file Retain Comments

Posted on 2012-09-03
Last Modified: 2012-09-04
Hi Experts,

I am relatively new to XSL.
I am trying to do the transformation from 1 xml to a different format of xml.

<xsl:template match="/">
                                    <xsl:for-each select="(subElement/node())[boolean(self::text())]">
                                          <xsl:value-of select="string(.)"/>

Now my soource xml file also has comments for each subElement. Appreciate any assistance to proceed further in capturing the comments.
Question by:bkreddy
    1 Comment
    LVL 60

    Accepted Solution

    if you mean comment as in XML comments, you need to access the comment() axis in the for-each eg. xsl:value-of select="comment()"
    if you want to maintain the comment, copy it
    xsl:copy-of select="comment()"

    If you just mean child elements, you should not simply say string(.) but be more specific in the XPath
    Given it is a comment element, you can say xsl:value-of select="comment"

    It would help to see some source XML

    note that you would have an easier job if you removed the for each in favor of an apply templates and have a specific template

    <xsl:template match="/">
                                        <xsl:apply-templates select="(subElement/node())[boolean(self::text())]"/>

    <xsl:template match="subElement/text()">
                                              <xsl:value-of select="."/>

    if you relax the selct, you can add extra templates and they will get fired automatically

    note that you don't need string() in the value-of, it does that automatically

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now