Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2009, 12:33 PM
Authorized User
 
Join Date: Sep 2009
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
Default Perform test then add comment

I want to test a value of an element then if there the result is true, I want to add a comment just inside the element in the result xml. How can I accomplish this?

For example in the code below I want to return <!-- "Last name not smith" --> if person/last <> "Smith"

INPUT

Code:
<root>
<people>
<person>
<first>Jane<first>
<last>Doe</last>
</person>

<person>
<first>Joe<first>
<last>S</last>
</person>

<person>
<first>Bob<first>
<last>Smith</last>
</person>


</people>
</root>


DESIRED OUTPUT

Code:
<root>
<people>
<person>
<first>Jane<first>
<last><!-- "Last name not smith" -->Doe</last>
</person>

<person>
<first>Joe<first>
<last><!-- "Last name not smith" -->S</last>
</person>

<person>
<first>Bob<first>
<last>Smith</last>
</person>

</people>
</root>
 
Old October 23rd, 2009, 12:39 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:template match="person/last[not(. = 'Smith')]">
    <xsl:copy>
      <xsl:comment> "Last name not smith" </xsl:comment>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
perform multiple things with one button danastasio Classic ASP Databases 2 January 29th, 2007 10:44 AM
based upon parameter perform action warsha_14 Struts 4 August 5th, 2006 04:51 AM
Connection cannot be used to perform this operatio LibrarianMo Access ASP 1 February 24th, 2006 01:18 PM
How to perform set related operations? hualin_yip Javascript 1 November 9th, 2004 08:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.