473,654 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

templates and xpath expr with predicate and first and last functions

HI,

I have some data like:
<x>
<y Position='Top'/>
<y/>
<x/>

and a template like:
<xsl:template match="/x/y[@Position='Top']">
<li>
<xsl:if test="position( )=last()">
<xsl:attribut e name="class">la st</xsl:attribute>
</xsl:if>
</li>
</xsl:template>

I want to catch the last y with a position of Top
unfortunately position and last work on a list of y elements, not a list of
y[@Position='Top'] elements.

In fact it's even worse than that.
If my data was
<x>
<y Position='Top'/>
<y/>
<z/>
<x/>

last() would be 3, not 2

Does any one have a work around for this bug?

Thanks
Martin
Mar 7 '06 #1
5 1305


Martin wrote:
I have some data like:
<x>
<y Position='Top'/>
<y/>
<x/>
Where is the closing </x> then? It is not clear whether that first x
element has two y child elements as well as an x child element.
and a template like:
<xsl:template match="/x/y[@Position='Top']">
<li>
<xsl:if test="position( )=last()">
<xsl:attribut e name="class">la st</xsl:attribute>
</xsl:if>
</li>
</xsl:template>

I want to catch the last y with a position of Top
unfortunately position and last work on a list of y elements, not a list of
y[@Position='Top'] elements.


The template does not change the type and size of the current node list.
You simply need to use xsl:apply-templates to filter out nodes first e.g.
<xsl:apply-templates select="y[@Position = 'Top']" />

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 7 '06 #2
Oops sorry, the data should read (1 x root, 2 y children)
<x>
<y Position='Top'/>
<y/>
</x>

The rest of the problem description remains.
Thanks
Martin

"Martin Honnen" <ma*******@yaho o.de> wrote in message
news:eG******** ******@TK2MSFTN GP09.phx.gbl...

Martin wrote:
I have some data like:
<x>
<y Position='Top'/>
<y/>
<x/>


Where is the closing </x> then? It is not clear whether that first x
element has two y child elements as well as an x child element.
and a template like:
<xsl:template match="/x/y[@Position='Top']">
<li>
<xsl:if test="position( )=last()">
<xsl:attribut e name="class">la st</xsl:attribute>
</xsl:if>
</li>
</xsl:template>

I want to catch the last y with a position of Top
unfortunately position and last work on a list of y elements, not a list
of y[@Position='Top'] elements.


The template does not change the type and size of the current node list.
You simply need to use xsl:apply-templates to filter out nodes first e.g.
<xsl:apply-templates select="y[@Position = 'Top']" />

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Mar 7 '06 #3
Having read some more, I think it's correct xslt behaviour, but it's still
not what I want.

I need to control the context size to be what I require it to be, maybe with
an apply-templates select clause.

Here is my new xslt which seems to fit the bill:
<xsl:template match="/x/y[1]">
<xsl:apply-templates select="/x/y[@Position='Top']" mode="test"/>
</xsl:template>

<xsl:template match="y" mode="test">
p=<xsl:value-of select="positio n()"/>
l=<xsl:value-of select="last()"/>
x
</xsl:template>

Martin

"Martin" <x@y.z> wrote in message
news:OM******** ******@TK2MSFTN GP11.phx.gbl...
Oops sorry, the data should read (1 x root, 2 y children)
<x>
<y Position='Top'/>
<y/>
</x>


The rest of the problem description remains.
Thanks
Martin

"Martin Honnen" <ma*******@yaho o.de> wrote in message
news:eG******** ******@TK2MSFTN GP09.phx.gbl...


Martin wrote:
I have some data like:
<x>
<y Position='Top'/>
<y/>
<x/>


Where is the closing </x> then? It is not clear whether that first x
element has two y child elements as well as an x child element.
and a template like:
<xsl:template match="/x/y[@Position='Top']">
<li>
<xsl:if test="position( )=last()">
<xsl:attribut e name="class">la st</xsl:attribute>
</xsl:if>
</li>
</xsl:template>

I want to catch the last y with a position of Top
unfortunately position and last work on a list of y elements, not a list
of y[@Position='Top'] elements.


The template does not change the type and size of the current node list.
You simply need to use xsl:apply-templates to filter out nodes first e.g.
<xsl:apply-templates select="y[@Position = 'Top']" />

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Mar 7 '06 #4


Martin wrote:
Oops sorry, the data should read (1 x root, 2 y children) The rest of the problem description remains.


Well I suggested a change in my earlier post


The template does not change the type and size of the current node list.
You simply need to use xsl:apply-templates to filter out nodes first e.g.
<xsl:apply-templates select="y[@Position = 'Top']" />


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 7 '06 #5
Thanks Martin.

Very tired today.

Martin
"Martin Honnen" <ma*******@yaho o.de> wrote in message
news:ex******** ******@TK2MSFTN GP15.phx.gbl...


Martin wrote:
Oops sorry, the data should read (1 x root, 2 y children)

The rest of the problem description remains.


Well I suggested a change in my earlier post


The template does not change the type and size of the current node list.
You simply need to use xsl:apply-templates to filter out nodes first e.g.
<xsl:apply-templates select="y[@Position = 'Top']" />


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Mar 7 '06 #6

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

Similar topics

1
6817
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
5
2528
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element of the currently matched element in the template, so that XPath expressions inside would be relative to that node? Specifically, I want to do this, in order to be able to issue xsl:call-template to apply some templates to some
7
1805
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange"; XmlElement crsElement = (XmlElement)m_colorRangeSwatchDoc.SelectSingleNode(expr); bool fr = bool.Parse(crsElement.GetAttribute("fixed").ToString()); The element returned is always the 1st, All Blue Colors, why doesn't the expression...
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
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 we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.