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

Discussion in 'XML' started by Colin Toal, Jan 22, 2004.

  1. Colin Toal

    Colin Toal Guest

    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
    Colin Toal, Jan 22, 2004
    #1
    1. Advertising

  2. 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.
    DFN-CIS NetNews Service, Jan 22, 2004
    #2
    1. Advertising

  3. Re: Use Parameter value in match or select - 'Parameterized XPathexpressions ?'

    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/
    Martin Honnen, Jan 22, 2004
    #3
  4. Colin Toal

    Colin Toal Guest

    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 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
    Colin Toal, Jan 23, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,624
    Richard Tobin
    Nov 13, 2004
  2. Replies:
    8
    Views:
    37,419
  3. =?iso-8859-1?q?Erik_Wikstr=F6m?=

    Partial specialisation of method taking parameterized parameter

    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 20, 2006, in forum: C++
    Replies:
    3
    Views:
    287
    Victor Bazarov
    Dec 20, 2006
  4. mrstephengross
    Replies:
    5
    Views:
    374
    mrstephengross
    May 23, 2008
  5. Sean Dockery
    Replies:
    8
    Views:
    143
    Sean Dockery
    Jan 3, 2006
Loading...

Share This Page