473,569 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="objectnam e"/>

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

<xsl:template match="$objectn ame">
<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 7381
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:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="objectnam e"/>

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

<xsl:template match="$objectn ame">
<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*******@yaho o.de> wrote in message news:<40******* *@olaf.komtel.n et>...
Colin Toal wrote:

I have a stylesheet like this:

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

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

<xsl:template match="$objectn ame">
<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
2975
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 following statement fails to find a node (even though one exists in the XML) if the value of the LDAPServerURL attribute is \\LocalServer but works if the...
1
4204
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 work? Or it will also match "Ni"? author A more general question, how do a match against a regular expression? Thanks Li Xuejun
14
12145
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 line in xslt <xsl:value-of select="/blah/q" /> and my xml document does not have a node /blah/q. The output would
3
1624
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"/> <xsl:param name="myvar"/>
2
1877
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 parentheses then it is happy. This appears to be an error; see http://www.w3.org/TR/xpath#NT-Expr. The path through the grammar is quite long; if you...
1
4419
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 matching the filters, runs into the response.write and outputs nothing. I've used the dbgclr to verify that the filters are matching and it is hitting...
1
7735
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 declaration. Here's how the parameter is described in the context of my ObjectDataSource: <asp:ObjectDataSource ID="ObjectDataSource1"...
3
1727
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
2725
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 solved? ---- XML file --- <?xml version="1.0" encoding="ISO-8859-1"?>
0
8120
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...
1
7672
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...
0
7968
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
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...
1
5512
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...
0
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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...

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.