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 September 1st, 2011, 07:01 AM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs down how to prevent xsl to output any particular element

Hi All,

I have a xml something like this:


Code:
<addres>
    <country>xxxx</country>
    <city>xxxx</city>
    <name>xxxxxxxx</name>
    <street>xxxxxxx</street>
    <!-- like this n number of element can come -->
    </addres>
Out of those all tag any of tag e.g. <name> I don't want to produce in out put xml for exp. output should be like below:


Code:
<addres>
    <country>xxxx</country>
    <city>xxxx</city>
    <street>xxxxxxx</street>
    <!-- like this n number of element can come -->
    </addres>

known way is to apply all templates except <name> element e.g. below


Code:
 <xsl:template match="address">
    <xsl:apply-templates select="country"/>
    <xsl:apply-templates select="city"/>
    <!-- let's not apply template for name-->
    <xsl:apply-templates select="street"/>
    <!-- like this n number of tag can come-->
    </xsl:template>

but the issue is there can n number of tag be come which is difficult to apply-template one by one for all. So my question is there any way to exclude that particular tag to be in output xml?

Thanks in advance
Mohan
 
Old September 1st, 2011, 07:04 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Simply create a template that matches the element you don't want to output that does nothing.

Code:
<xsl:template match="address">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="name">
   <!-- Do Nothing -->
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old September 1st, 2011, 07:15 AM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for quick reply samjudson,

Forgot to mention important note that element e.g. <name> required to display under other tag for exp. see in bold face:


Code:
<xsl:template match="address">
    <xsl:apply-templates select="country"/>
    <xsl:apply-templates select="city"/>
    <!-- let's not apply template for name-->
    <xsl:apply-templates select="street"/>
    <!-- like this n number of tag can come-->
    </xsl:template>

<xsl:template match="author">
    <xsl:apply-templates select="author-bio"/>
    <xsl:apply-templates select="name"/>    
    <xsl:apply-templates select="aff"/>   
    </xsl:template>
So i could not simply suppress that tag.

Thanks once again.
Mohan
 
Old September 1st, 2011, 07:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

So be more specific in your match expression:

Code:
<xsl:template match="address/name">
   <!-- Do Nothing -->
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old September 1st, 2011, 07:24 AM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops... failed to make you understand once again.
<name> applied in <author> is retrieved from <address> only

Code:
<xsl:template match="author">
    <xsl:apply-templates select="author-bio"/>
    <xsl:apply-templates select="address/name"/>    
    <xsl:apply-templates select="aff"/>   
    </xsl:template>
hope now you will be understand correctly.

Thanks
Mohan
 
Old September 1st, 2011, 08:46 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you want it to do something in one instance, and something else in another then you could use the "mode" attribute.

Code:
<xsl:template match="address">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="author">
  <xsl:apply-templates select="author-bio"/>
  <xsl:apply-templates select="address/name" mode="authormode"/>
  <xsl:apply-templates select="aff"/>
</xsl:template>

<xsl:template match="name">
  <!-- normal mode = do nothing -->
</xsl:template>

<xsl:template match="name" mode="authormode">
  <!-- author mode - do something -->
   Name = <xsl:value-of select="."/>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old September 1st, 2011, 12:12 PM
Authorized User
 
Join Date: Sep 2011
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great, working like charm.

Thanks a lot Sam





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT output to HTML - xml element text should look exactly as laid out in xml element DjGogga XSLT 4 July 21st, 2011 05:39 PM
Prevent displaying duplicate infromation in xsl Indy1304 XSLT 1 July 28th, 2008 11:13 AM
XSL(T) problems with xsl:element pan69 XSLT 3 December 13th, 2007 06:24 AM
Can display output as Element name HareshVani XSLT 2 March 22nd, 2006 03:19 AM
xsl:element not on its own line in output EstherMStrom XSLT 3 February 26th, 2005 06:08 AM





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