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 November 25th, 2008, 09:27 AM
Authorized User
 
Join Date: Nov 2008
Posts: 12
Thanks: 2
Thanked 1 Time in 1 Post
Send a message via Yahoo to mail4kaja
Default Exclude Elements in Apply Templates

Hi,

I just want to know how can I write templates for selectives nodes and no output is generated for other nodes ( for which I have not written templates ).

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

<xsl:template match="MyNode">
 <p>MyNode</p>
</xsl:template>

Even if my input XML contains elements other-tham MyNode, those nodes/texts should not appear in my output. How can i achieve that? I cannot write empty templates for the un-used elements as I don't know the elements that can appear in my input XML. My input XML is a dynamically generated one.

Thanks in advance,

With regards,
Kaja Mohideen
http://www.vhost4all.com/
__________________
R Kaja Mohideen
http://www.vhost4all.com/
 
Old November 25th, 2008, 09:37 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

xsl:apply-templates selects the nodes for processing so you could use
Code:
<xsl:template match="/">
 <xsl:apply-templates select="//MyNode"/>
</xsl:template>
if you want to ensure that only 'MyNode' elements are processed.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old November 25th, 2008, 09:46 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

As you are probably aware the default built-in template will output any text nodes it finds, so adding the following will stop that:

Code:
<xsl:template match="text()" />
You're root template does nothing that the built-in template doesn't do, so is actually superfluous in this example, so the complete stylesheet would be:

Code:
<xsl:template match="MyNode">
 <p>MyNode</p>
</xsl:template>
<xsl:template match="text()" />

/- Sam Judson : Wrox Technical Editor -/
 
Old November 25th, 2008, 09:56 AM
Authorized User
 
Join Date: Nov 2008
Posts: 12
Thanks: 2
Thanked 1 Time in 1 Post
Send a message via Yahoo to mail4kaja
Default

Thanks for your replies.

if I want to select only few nodes, may be MyNode1, MyNode2...MyNodeN. How to do that ?

R Kaja Mohideen
http://www.vhost4all.com/
 
Old November 25th, 2008, 10:06 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:template match="/">
  <xsl:apply-templates select="//MyNode1 | //MyNode2 | //MyNodeN"/>
</xsl:template>
is certainly possible. I am not sure it is naturally a good generic way or approach however, it all depends on how your XML input is structured exactly and how your transformation result should look.
So it might be better to show us more details of the input you have and the result you want to create, then it is easier to suggest a solution for that than trying to provide generic solutions.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old November 25th, 2008, 10:22 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>if my input XML contains elements other-tham MyNode, those nodes/texts should not appear in my output.

If you can write the rule in English, you can write it in XSLT:

<xsl:template match="*">

</xsl:template>



Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old November 26th, 2008, 01:09 AM
Authorized User
 
Join Date: Nov 2008
Posts: 12
Thanks: 2
Thanked 1 Time in 1 Post
Send a message via Yahoo to mail4kaja
Default

If I'm having

<xsl:template match="*"/>

I'm not getting any output.

R Kaja Mohideen
http://www.vhost4all.com/
 
Old November 26th, 2008, 02:12 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

As already Mike has said, <xsl:template match="*"/> will do nothing.
For your question, the template mentioned by Martin will work.
<xsl:template match="/">
  <xsl:apply-templates select="//MyNode1 | //MyNode2 | //MyNodeN"/>
</xsl:template>




------
Rummy
 
Old November 26th, 2008, 03:47 AM
Authorized User
 
Join Date: Nov 2008
Posts: 12
Thanks: 2
Thanked 1 Time in 1 Post
Send a message via Yahoo to mail4kaja
Default

I have templates for MyNode1 and MyNode2 (Note: My XML input is HIGHLY structured. MyNode1 and MyNode2 can appear at any level)

I have given "//MyNode1 | //MyNode2" for select attribute of apply-templates.

I'm not getting any output.

I'm a newbie for XSLT. Kindly help me.

Thanks,

R Kaja Mohideen
http://www.vhost4all.com/
 
Old November 26th, 2008, 04:35 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You'll have to show us your code. You're clearly doing something badly wrong, perhaps you simply don't understand template rules. If you show us your stylesheet and source document then we will probably be able to see where your misunderstanding lies.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Apply-templates combined with with-param ? Smirre XSLT 6 November 11th, 2008 08:25 AM
Difference between call-template ,apply-templates vikkiefd XSLT 4 March 12th, 2008 05:09 AM
apply-templates problem within for-each loop mister_mister XSLT 2 January 22nd, 2007 05:40 PM
Templates Won't Apply neilac333 XSLT 5 October 26th, 2006 01:39 PM
how to exclude elements in the result tree output ntmt XSLT 0 May 25th, 2006 10:33 AM





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