Meaningless Drivel is fun!*
The moose likes XML and Related Technologies and the fly likes Comment element in XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Comment element in XML" Watch "Comment element in XML" New topic
Author

Comment element in XML

Amanda Pele
Greenhorn

Joined: Feb 05, 2007
Posts: 1
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

Joined: Apr 14, 2004
Posts: 10336

Why comment out an element via an XSL transformation? Could you not just not include that element in your transformed document?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you want to create a comment in the result tree, then the <xsl:comment> element is the way to do that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comment element in XML
 
Similar Threads
Hyper links Using XSL?
Unable to show data using XSL
Create XML Document
Retriving Data from XML data into JSP using JSTL.
xpath and java (SDK 5)