473,411 Members | 1,968 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,411 software developers and data experts.

How to use a variable in Xpath select expression

Hi mates,

I am having the following problem in XSLT. I have the following three
variables which I receive from an XML document.

$xmlfile = "doc.xml"
$repeatpath = "/root/person"
$path = "name"

Suppose, the file doc.xml containls the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<person>
<name>Murtaza</name>
<surname>tinwala</surname>
</person>
<person>
<name>Rishit</name>
<surname>udani</surname>
</person>
<person>
<name>Nilesh</name>
<surname>ruparelia</surname>
</person>
</root>

From $xmlfile, I am loading the external XML document ( <xsl:variable
name="xmldatabase" select="document($xmlfile)"/>). The variable
$repeatpath contains the Xpath expression on which I have to run a
XSLT for-each loop. The variable $path contains the node, which I
have to output somewhere.

Now, the problem is I want to express Xpath expression in string
format like the following way:

<xsl:for-each select="$xmldatabase/$repeatpath">

But, that is not possible, because we cannot use variables for select
expressions. How should I solve this problem?

Murtaza Tinwala.
Jul 20 '05 #1
1 15348
<xsl:for-each select="$xmldatabase/$repeatpath">

But, that is not possible, because we cannot use variables for select
expressions. How should I solve this problem?
That is not your problem. You can use variables in select expressions,
the above is perfectly legal syntax so long as the variables have the
correct types, which is node set in this case.

Your $xmldatabase variable is node set valued but $repeatpath is probably
string valued (you don't show how it is set) so the problem is not that
you have variables but that your expression is equivalent to

<xsl:for-each select="$xmldatabase/'/root/person'">

and you can't have a string after /.

Some systems have an evaluate() extension (saxon for example has such a
thing) which can evaluate a string as an Xpath, otherwise, if this is an
extrnal global parameter which it appears to be your API may allow you
to pass in a node set directly rather than a string.

David
Jul 20 '05 #2

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

Similar topics

0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
4
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...
3
by: axial | last post by:
Is there a way to define a variable by resolving other variables? I'm trying to build an "uber-variable" from user-selected components. I have control over the XML and can change it as needed. ...
3
by: gimme_this_gimme_that | last post by:
I once downloaded a shareware program that allowed you to open an xml file, click on a text or an attribute, an then see the xpath expression that would fetch that data. The program didn't...
7
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";...
6
by: david | last post by:
For example, I can call <xsl:value-of select="concat('name','(',value,')')"/>, is there anyway I can pass the expression concat('name','(',value,')') as a variable from outside the template? ...
2
by: Alistair Bayley | last post by:
(.Net framework version 1.1.4322) The following XSL test case is rejected by System.Xml.Xsl.XslTransform, with an XsltException: "($dummy)+0 is an invalid XPath expression." If you remove the...
5
by: laks | last post by:
Hi I have the following xsl stmt. <xsl:for-each select="JOB_POSTINGS/JOB_POSTING \"> <xsl:sort select="JOB_TITLE" order="ascending"/> This works fine when I use it. But when using multiple...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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
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
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,...
0
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...

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.