tags:

views:

46

answers:

2

hi,

I am using xsl script to remove comments from all the portion of the xml. It is actually removing the comments which are in parent node but not from the other interior nodes.

[edited]

Updating the question. My requirement is to remove all the comments from entire XML document.

+3  A: 

It sounds like you want to copy only the comments from the input XML. And you're saying that your stylesheet copies certain comments but not others? When you say "comments which are in parent node", do you mean comments that are children of the root node (i.e. outside of all elements)?

When I try this stylesheet it works fine. Specifically the XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <comments>
         <xsl:for-each select="//comment()">
            <comment><xsl:value-of select="."/></comment>
         </xsl:for-each>
      </comments>
   </xsl:template>
</xsl:stylesheet>

run against the input

<?xml version="1.0" encoding="UTF-8"?>
<!-- foo -->
<a>
   <!-- bar -->
   <b>
      <c><!-- baz --></c>
   </b>
</a>

gives the output

<?xml version="1.0" encoding="utf-8"?>
<comments>
   <comment> foo </comment>
   <comment> bar </comment>
   <comment> baz </comment>
</comments>

If this is not the behavior you wanted, or if yours still doesn't work on your input, can you post your whole stylesheet and a sample of your input XML, and show what the current output is? Also what XSLT processor are you using?

LarsH
Thanks a lot for trying to understanding my question. My expectation is to remove all the comments irrespective of its location and not to put them in some place like specified. Sorry for poor questioning of this problem
thndrkiss
+1 for solving the problem as stated. :)
Dimitre Novatchev
@thndrkiss, ok, I guess you really did mean "remove" the comments and copy everything else. It's very confusing that your sample XSLT, which it is implied is attempting to "remove" the comments, explicitly selects comment nodes and intentionally puts comments into the output. ?! Anyway, looks like @Dimitre has you covered.
LarsH
@Dimitre your compassion is appreciated. :-)
LarsH
+5  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
   </xsl:template>
   <xsl:template match="comment()"/>
</xsl:stylesheet>

when applied on any XML document containing comment nodes, like this:

<!-- foo -->
<a>
   <!-- bar -->
   <b>
      <c><!-- baz --></c>
   </b>
</a>

produces the wanted result (the same document with the comment nodes stripped-off):

<a>
    <b>
        <c/>
    </b>
</a>

Do note: The use of the most fundamental and powerful XSLT design pattern -- the use and overriding of the identity rule.

Dimitre Novatchev
+1 for figuring out what the OP really meant. :-)
LarsH
@Dimitre: +1 good guessing! Same with just `*|@*` pattern: copy elements and attributes and built-in rules for other node type. But we like so match identity rule... ;)
Alejandro