473,769 Members | 5,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xpath dynamic selection

When I look at all the examples every selection like
select="child:: item[@value>3]" has a constant value to test against ("3" in
this case).
Can't this be done with a variable? eg:
select="child:: item[@value>$baseval ue]"

Because I'm trying to create a structure like this...
usecase
->item(s) (kind of ordered list)
->alt (with @number parameter)
->alternativecou rse
->alternative (with @number parameter)
->item(s)

the "alternativ e"s should get the itemnumber from the matching "alt"s.
It try to do this by calling the alts in item with:
<xsl:apply-templates match="alt">
<xsl:with-param name="step" select="$step"/>
</xsl:apply-templates>
with the $step number of the item
and in alternative:
<xsl:param name="start">
<xsl:value-of select="ancesto r::*/*/alt[@number=$anumbe r]"/>
</xsl:param>
with $anumber the number of the alternative.

But it returns nothing. If I remove the "[@number=$anumbe r]" part it always
returns 1, so I guess it returns the first match.

I also don't understand how I can get the @step value from the alt.
adding /attribute::step after the */*/alt doesn't seem to work.

If there are better ways to do this, please let me know, because I don't
completly understand the reasoning after the xsl structure.
Thanks,

Wim Lemkens
Jul 20 '05 #1
2 1955


Curantil wrote:
When I look at all the examples every selection like
select="child:: item[@value>3]" has a constant value to test against ("3" in
this case).
Can't this be done with a variable? eg:
select="child:: item[@value>$baseval ue]"
Sure, why not, any XPath expression is possible, as long as the
environment allows you to declare and initialize variables. XSLT does that.
Because I'm trying to create a structure like this...
usecase
->item(s) (kind of ordered list)
->alt (with @number parameter)
->alternativecou rse
->alternative (with @number parameter)
->item(s)
I am not sure what the above describes, is that meant to be the source
XML or part of your XSLT?
the "alternativ e"s should get the itemnumber from the matching "alt"s.
It try to do this by calling the alts in item with:
<xsl:apply-templates match="alt">
<xsl:with-param name="step" select="$step"/>
</xsl:apply-templates>
with the $step number of the item
and in alternative:
<xsl:param name="start">
<xsl:value-of select="ancesto r::*/*/alt[@number=$anumbe r]"/>
</xsl:param> with $anumber the number of the alternative.

But it returns nothing. If I remove the "[@number=$anumbe r]" part it always
returns 1, so I guess it returns the first match.

I also don't understand how I can get the @step value from the alt.
adding /attribute::step after the */*/alt doesn't seem to work.


I couldn't follow you here, please post the relevant snippets of the
source XML you have, and what output you are looking for. Then include
your attempts of XSLT to solve that.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
On Sunday 11 July 2004 11:09, Jeni Tennison wrote:
Ok, I'll clearify my vague question with some real code. But it is
rather long.

This is the relevant part of the input:
<usecase>
<title>Zoek boek op</title>
<description> De gebruiker heeft gegevens van een bepaald boek dat hij wilt
ontlenen en wilt weten waar het boek zich bevindt in de
bibliotheek.</description>
<actor>De gebruiker vult in eventueel verschillende velden gegevens in over
een boek dat hij zoekt.</actor>
<system>
<alt>1</alt>
Het systeem geeft een overzicht van de verschillende boeken die
voldoen aan
de zoekterm
</system>
<actor>
<alt>2</alt>
De gebruiker kiest het boek dat hij wilt opvragen
</actor>
<system>
Het systeem geeft de gegevens van het gevonden boek weer.
</system>
<alternativecou rses>
<alternative>
<system>
Er is maar 1 overeenkomstig resultaat : Het systeem geeft direct de
gegevens van het gevonden boek weer.
</system>
</alternative>
<alternative>
<actor>
De gebruiker kiest om te zoeken op nieuwe gegevens.
</actor>
<system>
Het systeem geeft de invulvelden terug weer. (terug van stap 1)
</system>
</alternative>
</alternativecour ses>
</usecase>

The required output would be: (without debug info):

De gebruiker heeft gegevens van een bepaald boek dat hij wilt ontlenen
en wilt
weten waar het boek zich bevindt in de bibliotheek.

1 De gebruiker vult in eventueel verschillende velden gegevens in over een
boek dat hij zoekt.
2 Het systeem geeft een overzicht van de verschillende
boeken die voldoen aan
de zoekterm
3 De gebruiker kiest het boek dat hij wilt opvragen
4 Het systeem geeft de gegevens van het gevonden boek
weer. Het systeem geeft
de gegevens van het gevonden boek weer.

Alternatieve Paden
2 Er is maar 1 overeenkomstig resultaat : Het systeem
geeft direct de
gegevens van het gevonden boek weer.

<<(note this is the second alternative)>>
3 De gebruiker kiest om te zoeken op nieuwe gegevens.
4 Het systeem geeft de invulvelden terug weer. (terug van
stap 1)
And here are the important parts of xsl:
<xsl:template match="usecase" >
<<snip: some numbering setup copied from orderedlist>>

<xsl:variable name="start">
<xsl:choose>
<xsl:when test="@continua tion='continues '">
<xsl:call-template name="usecase-starting-number"/>
</xsl:when>
<xsl:when test="$pi-start != ''">
<xsl:value-of select="$pi-start"/>
</xsl:when>
<xsl:otherwise> 1</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<<snip: some numbering setup copied from orderedlist>>

<div class="{name(.) }">
<xsl:call-template name="anchor"/>

<xsl:if test="title">
<xsl:call-template name="formal.ob ject.heading"/>
</xsl:if>
<table class="usecase" >
<xsl:if test="descripti on">
<xsl:apply-templates select="descrip tion"/>
</xsl:if>
<tr><td>
<ol>
<xsl:if test="$start != '1'">
<xsl:attribut e name="start">
<xsl:value-of select="$start"/>
</xsl:attribute>
</xsl:if>
<<snip: some numbering setup copied from orderedlist>>
<xsl:apply-templates select="*[(self::actor
or self::system)]"/>
</ol>
</td></tr>
</table>
<xsl:apply-templates select="alterna tivecourses"/>
</div>

<xsl:apply-templates select="*[not(self::actor
or self::title
or self::alternati vecourses
or self::descripti on
or self::system)]"/>
</xsl:template>

<xsl:template match="usecase/actor">
<xsl:param name="ucid"/>
<li>
<xsl:attribut e name="number">
<xsl:call-template name="usecase-step-number"/>
</xsl:attribute>
<xsl:variable name="step">
<xsl:call-template name="usecase-step-number"/>
</xsl:variable>

<div class="actor">
<xsl:apply-templates match="alt">
<xsl:with-param name="step" select="$step"/>
</xsl:apply-templates>
<xsl:apply-templates select="*[not(self::alt)]"/>
</div>
</li>
</xsl:template>

<xsl:template match="usecase/system">
<li>
<xsl:attribut e name="number">
<xsl:call-template name="usecase-step-number"/>
</xsl:attribute>
<xsl:variable name="step">
<xsl:call-template name="usecase-step-number"/>
</xsl:variable>
<div class="system">
<xsl:apply-templates match="alt">
<xsl:with-param name="step" select="$step"/>
</xsl:apply-templates>
<xsl:apply-templates/>
</div>
</li>
</xsl:template>

<xsl:template match="alternat ivecourses">
Alternatieve Paden
<xsl:param name="node" select="."/>

<div class="{name(.) }">
<xsl:call-template name="anchor"/>

<xsl:if test="title">
<xsl:call-template name="formal.ob ject.heading"/>
</xsl:if>
<table class="usecase" >
<xsl:if test="descripti on">
<xsl:apply-templates select="descrip tion"/>
</xsl:if>
<tr><td>
<ol>
<xsl:apply-templates match="alternat ivecourses/alternative">
</xsl:apply-templates>
</ol>
</td></tr>
</table>
</div>
</xsl:template>

<xsl:template match="alt">
<xsl:param name="step"/>

<xsl:param name="number">
<xsl:apply-templates/>
</xsl:param>
<!-- temporary debuginfo -->
ucid : <xsl:copy-of select="$ucid"/>
number : <xsl:copy-of select="$number "/>
step : <xsl:copy-of select="$step"/>
</xsl:template>

<xsl:template match="alternat ivecourses/alternative/actor">
<li>
<xsl:attribut e name="number">
<xsl:call-template name="alternati ve-step-number"/>
</xsl:attribute>
<div class="actor">
<xsl:apply-templates/>
</div>
</li>
</xsl:template>

<xsl:template match="alternat ivecourses/alternative/system">
<li>
<xsl:attribut e name="number">
<xsl:call-template name="alternati ve-step-number"/>
</xsl:attribute>
<div class="system">
<xsl:apply-templates/>
</div>
</li>
</xsl:template>

<xsl:template match="alternat ivecourses/alternative">
<xsl:param name="node" select="."/>
<xsl:param name="number">
<xsl:call-template name="alternati ve-number"/>
</xsl:param>

<!-- get the startignumber _problem is
here!!!!_ -->
<xsl:param name="start">
<xsl:value-of
select="ancesto r::../../*/alt[@number=current ()/@number]"/>
</xsl:param>

<!-- temporary debug info -->
anumber : <xsl:copy-of select="$number "/>
step : <xsl:copy-of select="$start"/>

<!-- generate the new list -->
<xsl:variable name="pi-start">
<xsl:call-template name="dbhtml-attribute">
<xsl:with-param name="pis"
select="process ing-instruction('db html')"/>
<xsl:with-param name="attribute " select="'start' "/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="numeratio n">
<xsl:call-template name="list.nume ration"/>
</xsl:variable>

<xsl:variable name="type">
<xsl:choose>
<xsl:when test="$numerati on='arabic'">1</xsl:when>
<xsl:when test="$numerati on='loweralpha' ">a</xsl:when>
<xsl:when test="$numerati on='lowerroman' ">i</xsl:when>
<xsl:when test="$numerati on='upperalpha' ">A</xsl:when>
<xsl:when test="$numerati on='upperroman' ">I</xsl:when>
<!-- What!? This should never happen -->
<xsl:otherwis e>
<xsl:message>
<xsl:text>Unexp ected numeration: </xsl:text>
<xsl:value-of select="$numera tion"/>
</xsl:message>
<xsl:value-of select="1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<div class="{name(.) }">
<xsl:call-template name="anchor"/>
<tr><td>
<ol>
<xsl:if test="$start != '1'">
<xsl:attribut e name="start">
<xsl:value-of select="$start"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$numerati on != ''">
<xsl:attribut e name="type">
<xsl:value-of select="$type"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@spacing= 'compact'">
<xsl:attribut e name="compact">
<xsl:value-of select="@spacin g"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="*[(self::actor
or self::system)]"/>
</ol>
</td></tr>
</div>

<xsl:apply-templates select="*[not(self::actor
or self::title
or self::descripti on
or self::system)]"/>
</xsl:template>

<xsl:template name="usecase-step-number">
<!-- context node must be a system or actor in a usecase -->
<xsl:param name="node" select="."/>

<xsl:choose>
<xsl:when test="$node/@override">
<xsl:value-of select="$node/@override"/>
</xsl:when>
<xsl:when test="$node/preceding-sibling::*[(self::system or
self::actor)]">
<xsl:variable name="pnum">
<xsl:call-template name="usecase-step-number">
<xsl:with-param name="node"
select="$node/preceding-sibling::*[(self::system or self::actor)][1]"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$pnum + 1"/>
</xsl:when>
<xsl:otherwis e>
<xsl:call-template name="usecase-starting-number">
<xsl:with-param name="list" select="parent: :*"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="usecase-starting-number">
<xsl:param name="list" select="."/>
<xsl:choose>
<xsl:when test="not($list/@continuation = 'continues')">1 </xsl:when>
<xsl:otherwis e>
<xsl:variable name="prevlist"
select="$list/preceding::usec ase[1]"/>
<xsl:choose>
<xsl:when test="count($pr evlist) = 0">2</xsl:when>
<xsl:otherwis e>
<xsl:variable name="prevlengt h"
select="count($ prevlist/listitem)"/>
<xsl:variable name="prevstart ">
<xsl:call-template name="usecase-starting-number">
<xsl:with-param name="list" select="$prevli st"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$prevst art + $prevlength"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="alternati ve-number">
<xsl:param name="node" select="."/>

<xsl:choose>
<xsl:when test="$node/@override">
<xsl:value-of select="$node/@override"/>
</xsl:when>
<xsl:when test="$node/preceding-sibling::altern ative">
<xsl:variable name="pnum">
<xsl:call-template name="alternati ve-number">
<xsl:with-param name="node"
select="$node/preceding-sibling::altern ative[1]"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$pnum + 1"/>
</xsl:when>
<xsl:otherwis e>
1
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="alternati ve-step-number">
<!-- context node must be a system or actor in alternative -->
<xsl:param name="node" select="."/>

<xsl:choose>
<xsl:when test="$node/@override">
<xsl:value-of select="$node/@override"/>
</xsl:when>
<xsl:when test="$node/preceding-sibling::*[(self::system or
self::actor)]">
<xsl:variable name="pnum">
<xsl:call-template name="usecase-step-number">
<xsl:with-param name="node"
select="$node/preceding-sibling::*[(self::system or self::actor)][1]"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$pnum + 1"/>
</xsl:when>
<xsl:otherwis e>
<xsl:call-template name="usecase-starting-number">
<xsl:with-param name="list" select="parent: :*"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

I hope this code helps more to comprehend the question.
The problem is locate somewhere around line 210 of the mail. (I marked
it in comment)
Thanks,

Wim Lemkens
Jul 20 '05 #3

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

Similar topics

4
21210
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a native English speaker, it's quite hard to make the problem clear. Please see the following example.
4
3954
by: Andreas Baier | last post by:
Hi, I want to select all "result" nodes from the following xml-document: ---- <results xmlns="http://schemas.microsoft.com/sharepoint/soap/"> <list id="{678737D9-2BF3-4FF7-A60E-5AA68595C8FB}"/> <versioning enabled="1"/> <settings url="http://racoon/TestDokumente/_layouts/1033/LstSetng.aspx?List={678737D9-
2
1515
by: Steve Jorgensen | last post by:
Working with the DOM (specifically, the MSXML DOM), I'm wondering if there's an efficient way to check whether it would be matched by a given XPath expression. I've made it work to just run the XPath select on the document, looping through all the nodes returned, and seeing if one of those is the same as the node in question. If I need to check many expressions, though, this would not be efficient.
6
15511
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
0
1345
by: pbb | last post by:
I have a web page on which I dynamically create controls based on the selection a user makes from a dropdownlist (this ddl is not dynamic). Depending on the user's selection, the controls could be any combination of textboxes, ddls, popup calendars, etc. The properties of the dynamic controls are stored in a SQL database so that my program can know what type of control to render and what items to put in the ddl etc. My problem is that I...
4
1593
by: John Henckel | last post by:
I have a data file like this..... <top> <graph> <part> <param name="mass">1 </param> </part> <body name="nut"> <link href="#/top/graph/part"/> </body>
0
1482
by: Thor W Hammer | last post by:
Is it possible to use dynamic xpath expressions with the <%#XPath("..")%> syntax and XmlDataSource? This is a couple of examples to illustrate what I think about: 1: <%#XPath("*")%> 2: <%#XPath("*%>]")%>
6
4939
by: dotnetnoob | last post by:
i would like to know how i can build xpath expression dynamiclly. let's say i have a following xml file: <EventEnrollment InstanceNumber = "675"> <EventSource> <ObjectReference ObjectKey="xxxxx"> .. .. .. <EventEnrollment InstanceNumber = "676">
0
1683
by: Alexandre Bergel | last post by:
Dear colleague, Please, note that after the workshop, best papers will be selected, and a second deadline will then be set regarding preparation of the Electronic Communications of the EASST. Note that we submission deadline has been extended. The important dates: - May 27: deadline for the workshop submissions. Submissions should follow LNCS format (www.springer.com/lncs) and should be sorter than
0
9589
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
10216
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
10049
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...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
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
7413
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
5309
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
3965
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
3
2815
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.