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

C

Colin Toal

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
 
D

DFN-CIS NetNews Service

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>
 
M

Martin Honnen

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.
 
C

Colin Toal

Martin Honnen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top