473,323 Members | 1,551 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,323 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 2755
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.