Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Use Parameter value in match or select - 'Parameterized XPath expressions ?'

Reply
Thread Tools

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

 
 
Colin Toal
Guest
Posts: n/a
 
      01-22-2004
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 xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslaram 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
 
Reply With Quote
 
 
 
 
DFN-CIS NetNews Service
Guest
Posts: n/a
 
      01-22-2004
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.

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-22-2004


Colin Toal wrote:


> I have a stylesheet like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
> <xslaram 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/

 
Reply With Quote
 
Colin Toal
Guest
Posts: n/a
 
      01-23-2004
Martin Honnen <> wrote in message news:<4010040f$>...
> Colin Toal wrote:
>
>
> > I have a stylesheet like this:
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
> > version="1.0">
> > <xslaram 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 6.12 Can I use Perl regular expressions to match balanced text? PerlFAQ Server Perl Misc 0 01-09-2011 05:00 PM
Can XPath expressions use a default namespace? Arndt Jonasson XML 1 02-09-2007 12:42 PM
How to use XPath to select element with text match sehiser@gmail.com XML 8 09-29-2006 07:32 AM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments