473,399 Members | 3,401 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,399 software developers and data experts.

Use Parameter value in match or select - 'Parameterized XPath expressions ?'

Hi all,

I'm starting to learn XSLT - and have what I think is a very basic
question:

I have a stylesheet like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="objectname"/>

<xsl:template match="/Root">
<xsl:apply-templates select="./$objectname"/>
</xsl:template>

<xsl:template match="$objectname">
<Result>
<xsl:value-of select="."/>
</Result>
</xsl:template>

</xsl:stylesheet>

and XML like this:

<?xml version="1.0" ?>
<Root>
<Value1>1</Value1>
<Value2>2</Value2>
<Value3>3</Value3>
</Root>

Now, I'd like to pass a name of a child element of Root (like Value1
or Value2) from a Java class into this stylesheet as param
'objectname'.

I can pass the parameter fine (I have tested this), but I understand
that this won't work because the XSLT interpretor cannot resolve the
value of $objectname as part of an XPath expression representing a
nodeset.

What I'd like to know is if there is some way to accomplish the same
thing ? I apologize if this question has been asked a million times by
a million newbies in the past, but I haven't found a way to do it yet
- I've even considered resorting to something 'strange' like running
my XSLT template through another XSLT template that resolves all the
'parameterized XPath expressions' into their explicit forms - but that
feels 'terribly inelegant'.

Thanks in advance - please reply directly as above.
Colin
Jul 20 '05 #1
3 7373
On 22/01/2004, around 15:14, Colin Toal wrote:

CT> What I'd like to know is if there is some way to accomplish the same
CT> thing ?

Try

<xsl:template match="/Root">
<xsl:apply-templates select="./*[name() = $objectname]"/>
</xsl:template>

--
Stuart
The darkest hour is just before dawn. So if you're going to steal your
neighbour's milk, that's the time to do it.

Jul 20 '05 #2


Colin Toal wrote:

I have a stylesheet like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="objectname"/>

<xsl:template match="/Root">
<xsl:apply-templates select="./$objectname"/>
</xsl:template>

<xsl:template match="$objectname">
<Result>
<xsl:value-of select="."/>
</Result>
</xsl:template>

</xsl:stylesheet>

and XML like this:

<?xml version="1.0" ?>
<Root>
<Value1>1</Value1>
<Value2>2</Value2>
<Value3>3</Value3>
</Root>

Now, I'd like to pass a name of a child element of Root (like Value1
or Value2) from a Java class into this stylesheet as param
'objectname'.

I can pass the parameter fine (I have tested this), but I understand
that this won't work because the XSLT interpretor cannot resolve the
value of $objectname as part of an XPath expression representing a
nodeset.


XPath has a function local-name() so you could have an XPath alike
*[local-name() = $objectname]
to select elements with the name passed in.
--

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

Jul 20 '05 #3
Martin Honnen <ma*******@yahoo.de> wrote in message news:<40********@olaf.komtel.net>...
Colin Toal wrote:

I have a stylesheet like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="objectname"/>

<xsl:template match="/Root">
<xsl:apply-templates select="./$objectname"/>
</xsl:template>

<xsl:template match="$objectname">
<Result>
<xsl:value-of select="."/>
</Result>
</xsl:template>

</xsl:stylesheet>

and XML like this:

<?xml version="1.0" ?>
<Root>
<Value1>1</Value1>
<Value2>2</Value2>
<Value3>3</Value3>
</Root>

Now, I'd like to pass a name of a child element of Root (like Value1
or Value2) from a Java class into this stylesheet as param
'objectname'.

I can pass the parameter fine (I have tested this), but I understand
that this won't work because the XSLT interpretor cannot resolve the
value of $objectname as part of an XPath expression representing a
nodeset.


XPath has a function local-name() so you could have an XPath alike
*[local-name() = $objectname]
to select elements with the name passed in.


Thank you for the help -

Will that work for 'match' expressions also ?

Colin
Jul 20 '05 #4

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

Similar topics

1
by: Alastair Cameron | last post by:
VB6, MSXML 3.2 installed: Q1. I am having a problem selecting nodes with XPATH expressions when an attribute values contain backslashes (\\) in as part of its value: For example the...
1
by: Xuejun Li \(SH/RDC\) | last post by:
Hi, How do I select a value that starts with a certain prefix? For example I want all <author> elements that contain a <last-name> element with a value that begins with the letter Mi. Will this...
14
by: inquirydog | last post by:
Hi- One frusterating thing for me with xsl is that I don't know how to make xslt throw some sort of exception when a value-of path does not exist. For instance, suppose I have the following...
3
by: jose.jeria | last post by:
I have the following Code: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" encoding="UTF-8"/>...
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...
1
by: soupaman | last post by:
Im trying to output some filtered xml using the xmlTextReader I know the code and commenting needs cleaned up and eventually plan to add the values to a dataset. currently what this is doing is...
1
by: | last post by:
Hi, I've defined an ObjectDataSource and a parameterized DataSet. I would like it if I could pass the parameter value that describes the query that creates the DataSet as part of a user control...
3
by: sp | last post by:
the xml file i used <catalog> <cd> <title year="1990">Empire Burlesque</title> <artist>Bob Dylan</artist> <price>10.90</price> </cd> <cd>
2
by: Paul Verbelen | last post by:
When I run this XSL with IE6 then I get following error: "Reference to variable or parameter 'myset' must evaluate to a node list." Why is 'myset' not a node list? How can this easely be...
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
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,...
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
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,...
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
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,...

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.