473,480 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Am I the first child using an if question

Hi,
I have an xml file with the following structure:
<Dictionary>
....
<Nested>
<Entry>
....
</Entry>
<Entry>
....
</Entry>
</Nested>
<Entry>
</Entry>
</Dictionary>

I want to iterate over all entries and print them however there is a
special printing for entries that reside in <Nestedtags and are not
the first one.

I'm using the following code:
<xsl:for-each select="//Entry">
<p>
<!--Check if entry is part of a nested entry.-->
<xsl:if test="name(..)='Nested' and position() 1">
<xsl:text>Print:123456</xsl:text>
</xsl:if>

The name(..)='Nested' part is working however the position part does
not, I tried to find some info on the subject with no luck.

Do you have an idea ?

Thank,
Efi

Feb 18 '07 #1
4 3498
On Feb 18, 6:01 pm, "Efi Merdler" <foo...@gmail.comwrote:
I have an xml file with the following structure:
<Dictionary>
...
<Nested>
<Entry>
...
</Entry>
<Entry>
...
</Entry>
</Nested>
<Entry>
</Entry>
</Dictionary>

I want to iterate over all entries and print them however
there is a special printing for entries that reside in
<Nestedtags and are not the first one.

I'm using
[for-eachs and ifs]
The name(..)='Nested' part is working however the
position part does not,
Of course it doesn't. It returns the position in the
node-set.
I tried to find some info on the subject with no luck.
Well, here's some info on the subject: you're doing it all
wrong anyway. Think about your transformations in terms of
node-sets and templates.

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Dictionary">
<result>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="Nested">
<xsl:apply-templates select="Entry[1]"/>
</xsl:template>
<xsl:template match="Entry">
<xsl:call-template name="Entry"/>
</xsl:template>
<xsl:template
match="Nested/Entry[not(preceding-sibling::Entry)]">
<xsl:call-template name="Entry"/>
<xsl:apply-templates
select="following-sibling::Entry"/>
</xsl:template>
<xsl:template
match="Nested/Entry[preceding-sibling::Entry]">
<special-entry>
<xsl:apply-templates/>
</special-entry>
</xsl:template>
<xsl:template name="Entry">
<normal-entry>
<xsl:apply-templates/>
</normal-entry>
</xsl:template>
</xsl:stylesheet>

--
roy axenov

Feb 18 '07 #2
Efi Merdler wrote:
Hi,
I have an xml file with the following structure:
<Dictionary>
...
<Nested>
<Entry>
...
</Entry>
<Entry>
...
</Entry>
</Nested>
<Entry>
</Entry>
</Dictionary>

I want to iterate over all entries and print them however there is a
special printing for entries that reside in <Nestedtags and are not
the first one.

I'm using the following code:
<xsl:for-each select="//Entry">
Don't use for-each unless you mean it (ie you want to process the
elements out of order or out of context).

If the Entry elements are already in order, then a template matching
them is all you need. If you want to sort them, however, or if you're
already processing them in document order elsewhere in your XSLT and you
now need to handle them again for some other reason, then for-each will
do it.
<p>
<!--Check if entry is part of a nested entry.-->
<xsl:if test="name(..)='Nested' and position() 1">
<xsl:text>Print:123456</xsl:text>
</xsl:if>

The name(..)='Nested' part is working however the position part does
not, I tried to find some info on the subject with no luck.
If Entry is the context node, then

<xsl:if test="parent::Nested and count(preceding-sibling::Entry)>0">
...
</xsl:if>

will detect if the Entry is inside a Nested element and this is not the
first Entry of its group. The position() function is problematic because
it returns the position in the node-set, which may or may not be the
position in the markup. This is a usability error in XSL: position()
really ought to have been called node-position(), and position() should
have been defined with its expected meaning of "position in markup".

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Feb 18 '07 #3
Don't use for-each unless you mean it (ie you want to process the
elements out of order or out of context).

If the Entry elements are already in order, then a template matching
them is all you need. If you want to sort them, however, or if you're
already processing them in document order elsewhere in your XSLT and you
now need to handle them again for some other reason, then for-each will
do it.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Thanks,
One more question, when using template matching only once will the
xslt engine run it in a loop when there are several matches.

For example:
<ExampleCtn>
<Example>He abandoned his family.</Example>
<Example>She was afraid of being abandoned.</Example>
</ExampleCtn>

<xsl:template match="ExampleCtn">
<xsl:text>:</xsl:text>
<xsl:value-of select="Example"/>
</xsl:template>

will value-of run in a loop on all the Example tag ?

Thanks,
Efi

Feb 19 '07 #4
Please don't quote the sigs and don't remove the
attributions.

On Feb 19, 11:44 am, "Efi Merdler" <foo...@gmail.com>
wrote:
Don't use for-each unless you mean it (ie you want to
process the elements out of order or out of context).
For that matter, xsl:apply-templates works just nice with
xsl:sort, too. In my opinion, the rule of the thumb is:
don't use for-each unless you're dead certain you're not
pointing it at your foot. (Which you normally are.)
One more question, when using template matching only once
will the xslt engine run it in a loop when there are
several matches.
Not in a loop, there's no guaranteed order-of-execution.
But yes, if you *are* using template matching, the XSLT
processor naturally will apply templates to every node in
the node-set you've provided.
<ExampleCtn>
<Example>He abandoned his family.</Example>
<Example>She was afraid of being abandoned.</Example>
</ExampleCtn>

<xsl:template match="ExampleCtn">
<xsl:text>:</xsl:text>
<xsl:value-of select="Example"/>
</xsl:template>

will value-of run in a loop on all the Example tag ?
Why don't you stuff it in your XSLT processor and see what
happens? No, it's won't 'run in a loop' or anything like
that, because you're most emphatically *not* using template
matching in this example. Replace value-of with
apply-templates.

--
Pavel Lepin

Feb 19 '07 #5

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

Similar topics

41
2804
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
3
4591
by: Drulli Rokk | last post by:
Hi, Here's a question that has cost this newbie two days of headache already: How can I get my XSLT stylesheet to specify a maximum number of elements to process? I'm now using <xsl:for-each>...
6
16595
by: Veerle | last post by:
Hi, Somewhere in my html page, I have the following code: <div id="main-body"> <h2>Header text</h2> <p>Some other text ...</p> ..... <h2>Another header text</h2> <p>Some more other text...
1
4209
by: Martin Douglas | last post by:
Hey guys, maybe someone can help me with some MDI issues I have. A co-worker asked me a very simple question, one that I blew off as trivial, and it has become a time-consuming issue. Simply...
1
5521
by: Eric | last post by:
Consider the following: class ParentClass { public: void FunctionOne( void ); protected: void FunctionTwo( void ); private: void FunctionThree( void );
3
1710
by: Tony Young | last post by:
Hi, I am very interested to know which one of the two functions below is used more often by people. The 1st way intends to let B prepare the data and A perform the action. The 2nd way intends...
3
2217
by: William Krick | last post by:
Given this XML <parent> <child name="billy"> <child name="sue"> </parent> I can use this to process each child... <xsl:for-each select="child">
4
3544
by: Richard Lewis Haggard | last post by:
What is the mechanism by which a child window can notify its parent that it has been clicked on? -- Richard Lewis Haggard www.Haggard-And-Associates.com
13
14518
by: howa | last post by:
e.g. <div class="main"> <p>ssssss</p> <p>ssssss</p> <p>ssssss</p> <p>ssssss</p>
1
6500
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
0
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.