• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comment element in XML

 
Amanda Pele
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an XML (which I have taken from w3schools)

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>



Using XSLT 2.0, I want to convert this XML to another XML in which the <artist> element should be commented out. The output should be something like this


<catalog>
<cd>
<title>Empire Burlesque</title>
<!-- <artist>Bob Dylan</artist> -->
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<!-- <artist>Bonnie Tyler</artist> -->
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>


I have written the following XSLT for the same

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="artist">
<--<artist><xsl:apply-templates /></artist>-->
</xsl:template>
</xsl:stylesheet>

But when the old XML is transformed to a new XML, it displays the output as

& lt ;--<artist>Bonnie Tyler</artist>--& gt ;

How can I get the correct result?

Thanks in advance...
[ February 05, 2007: Message edited by: Amanda Pele ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why comment out an element via an XSL transformation? Could you not just not include that element in your transformed document?
 
Paul Clapham
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to create a comment in the result tree, then the <xsl:comment> element is the way to do that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic