|
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
|
|
|
November 25th, 2008, 09:27 AM
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 12
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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/
|
November 25th, 2008, 09:37 AM
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
November 25th, 2008, 09:46 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
November 25th, 2008, 09:56 AM
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 12
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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/
|
November 25th, 2008, 10:06 AM
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
November 25th, 2008, 10:22 AM
|
|
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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
|
November 26th, 2008, 01:09 AM
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 12
Thanks: 2
Thanked 1 Time in 1 Post
|
|
If I'm having
<xsl:template match="*"/>
I'm not getting any output.
R Kaja Mohideen
http://www.vhost4all.com/
|
November 26th, 2008, 02:12 AM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
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
|
November 26th, 2008, 03:47 AM
|
Authorized User
|
|
Join Date: Nov 2008
Posts: 12
Thanks: 2
Thanked 1 Time in 1 Post
|
|
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/
|
November 26th, 2008, 04:35 AM
|
|
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|
|