473,221 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Use apply-templates selectively in XSLT

Given an XML like:

<root>
<node>8</node>
<node>21</node>
<node>-7</node>
<node>13</node>
<node>43</node>
<node>2</node>
</root>
how might I select only the 2nd, 3rd and 4th nodes (or more
generically, any selective set of nodes like first 4, last 3, every
other node etc)? Note that since my XSLT obviously won't know the
value inside a node, I can't just pick a node with the values of 21,
-7 and 13 using xsl:if or something like it. I tried playing around
with some things like for-each and creating a template with some sort
of a recursive scenario, but to no avail.
Any help is appreciated.
Thanks
Abhishek

Feb 23 '07 #1
3 2749
ab**********@gmail.com wrote:
how might I select only the 2nd, 3rd and 4th nodes
Write a select expression that tests position().

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Feb 23 '07 #2
On Feb 23, 7:51 pm, "abhishek....@gmail.com"
<abhishek....@gmail.comwrote:
<root>
<node>8</node>
<node>21</node>
<node>-7</node>
<node>13</node>
<node>43</node>
<node>2</node>
</root>

how might I select only the 2nd, 3rd and 4th nodes
<xsl:template match="node"/>
<xsl:template
match="node[position() &gt; 1 and position() &lt; 5]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
(or more generically, any selective set of nodes like
first 4,
<xsl:template match="node"/>
<xsl:template
match="node[count(preceding-sibling::node) &lt; 4]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
last 3,
<xsl:template match="node"/>
<xsl:template
match="node[count(following-sibling::node) &lt; 3]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
every other node
<xsl:template match="node"/>
<xsl:template
match="node[position() mod 2=0]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>

Note that depending on what exactly you are trying to do,
it might be a good idea to use xsl:key/key() combo together
with selecting the nodesets you need instead of
template-based matching of the nodes you need processed.
Note that since my XSLT obviously won't know the value
inside a node
I beg your pardon? Of course it will. Use string() or just
'.'. Note that this is a bit esoteric and often not a very
good practice if you stop and think about it for a moment.
Anyway, processing based on a value of the node is
definitely possible.
I can't just pick a node with the values of 21, -7 and 13
Of course you can.

<xsl:template match="node"/>
<xsl:template
match="node[.=21 or .=-7 or .=13]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>

Or did you mean that your problem requires matching of
nodes based on their position in the document, not on the
plain data they contain? If that is the case, the solutions
are described above.

Another thing to keep in mind is that if you're processing
a homogeneous nodeset using node position to switch
processing modes, it is often an indicator of poor XML
design of your source document; but whatever suits you.
using xsl:if or something like it. I tried playing around
with some things like for-each
As a rule of the thumb, if and choose should be replaced
with predicate-based selection and matching where possible.
Where impossible, go ahead and use if or choose, but
remember that it's a good idea to feel somewhat dirty and
uncomfortable afterwards.

You should also realize that for-each is very different
from what you would expect out of similarly based language
constructs or library methods in imperative languages such
as Perl, PHP, C++ or Java. Use template-based processing
instead.
and creating a template with some sort of a recursive
scenario, but to no avail.
I'm not sure I can see how recursive processing would be
helpful in this particular case.

--
roy axenov

Feb 23 '07 #3
On Feb 23, 2:03 pm, "roy axenov" <r_axe...@mail.ruwrote:
On Feb 23, 7:51 pm, "abhishek....@gmail.com"

<abhishek....@gmail.comwrote:
<root>
<node>8</node>
<node>21</node>
<node>-7</node>
<node>13</node>
<node>43</node>
<node>2</node>
</root>
how might I select only the 2nd, 3rd and 4th nodes

<xsl:template match="node"/>
<xsl:template
match="node[position() &gt; 1 and position() &lt; 5]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
(or more generically, any selective set of nodes like
first 4,

<xsl:template match="node"/>
<xsl:template
match="node[count(preceding-sibling::node) &lt; 4]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
last 3,

<xsl:template match="node"/>
<xsl:template
match="node[count(following-sibling::node) &lt; 3]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
every other node

<xsl:template match="node"/>
<xsl:template
match="node[position() mod 2=0]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>

Note that depending on what exactly you are trying to do,
it might be a good idea to use xsl:key/key() combo together
with selecting the nodesets you need instead of
template-based matching of the nodes you need processed.
Note that since my XSLT obviously won't know the value
inside a node

I beg your pardon? Of course it will. Use string() or just
'.'. Note that this is a bit esoteric and often not a very
good practice if you stop and think about it for a moment.
Anyway, processing based on a value of the node is
definitely possible.
I can't just pick a node with the values of 21, -7 and 13

Of course you can.

<xsl:template match="node"/>
<xsl:template
match="node[.=21 or .=-7 or .=13]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>

Or did you mean that your problem requires matching of
nodes based on their position in the document, not on the
plain data they contain? If that is the case, the solutions
are described above.

Another thing to keep in mind is that if you're processing
a homogeneous nodeset using node position to switch
processing modes, it is often an indicator of poor XML
design of your source document; but whatever suits you.
using xsl:if or something like it. I tried playing around
with some things like for-each

As a rule of the thumb, if and choose should be replaced
with predicate-based selection and matching where possible.
Where impossible, go ahead and use if or choose, but
remember that it's a good idea to feel somewhat dirty and
uncomfortable afterwards.

You should also realize that for-each is very different
from what you would expect out of similarly based language
constructs or library methods in imperative languages such
as Perl, PHP, C++ or Java. Use template-based processing
instead.
and creating a template with some sort of a recursive
scenario, but to no avail.

I'm not sure I can see how recursive processing would be
helpful in this particular case.

--
roy axenov
WOW !!!

Thank you so much. Since I am a beginner at this, I had never even
heard of the node and position things. This is absolute magic to me.

And yes, I did mean that my "problem requires matching of nodes based
on their position in the document, not on the plain data they
contain."

Once again, thanks a lot.

Abhishek

Feb 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Troy Heber | last post by:
Can anyone tell me how to force an immediate apply of system based registry changes. I have an application written in C# that updates the registry. However I’m forced to logout and log back in for...
1
by: Christopher Benson-Manica | last post by:
IE 5.0 apparently does not support the apply() method for function objects - the following fails: function foo() { alert( this.length ); } foo.apply( ); Is there a convenient way to add an...
1
by: Trent | last post by:
Hello,everyone. I'm setting a db2 replication environment using UDB version 8.1.5 running on Windows 2000 servers. The source server is on a Windows server with the capture program running while...
3
by: almousawi | last post by:
I am working on a DB2 replication program that used to work. I ran the "warm" capture program to get a cold start and I am running the apply program, but, the apply does not replicate the data. I...
4
by: esmith2112 | last post by:
Having a bear of a time trying to implement replication under 8.2 environment. I've created all of the control structures on both source and target database and can actually see data being staged...
0
by: mdb_1974 | last post by:
Hello I tried to do an initial full refresh but I failed - nothing happens (no error at all). Apply works without errors. Details of my environment: I have the following replication scenario:...
3
by: Jeff Stewart | last post by:
I've been working with the JavaScript Shell in Firefox on a mad-scientist problem I've had in my head. Assume a function named 'object' that is designed to create an object based on a prototype...
1
by: mattscho | last post by:
Re: Filter By From, Apply Filter, Remove Filter Buttons in a Form. -------------------------------------------------------------------------------- Hi All, Trying to create a set of 3 buttons in...
6
by: exhuma.twn | last post by:
This is something that keeps confusing me. If you read examples of code on the web, you keep on seeing these three calls (super, apply and __init__) to reference the super-class. This looks to me...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.