• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

XSL - XML file Retain Comments

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="/">
<parentElement>
                              <subElement>
                                    <xsl:for-each select="(subElement/node())[boolean(self::text())]">
                                          <xsl:value-of select="string(.)"/>
                                    </xsl:for-each>
                              </subElement>

Now my soource xml file also has comments for each subElement. Appreciate any assistance to proceed further in capturing the comments.
0
bkreddy
Asked:
bkreddy
1 Solution
 
Geert BormansInformation ArchitectCommented:
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="/">
<parentElement>
                              <subElement>
                                    <xsl:apply-templates select="(subElement/node())[boolean(self::text())]"/>
                                                                  </subElement>
....
</xsl:template>

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

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
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now