You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Adam Flegman <ad...@yahoo.com> on 2003/07/24 00:30:07 UTC

dynamic node selection

Hi Guys,

I have been trying to re-factor some existing xslt code and wonder if it is
possible to select nodes based on a string.

The string is built up by parameters so the value of the target node is
determined each time the xsl template is called. I am having trouble using the
string to select the target node. In the sample below I can calculate the name
of the target node I am after, but I am unsure how to convert the name into an
actual node.


<xsl:template name="createOptionList">
   <xsl:param name="parentNode" select="string()" />
   <xsl:param name="childNode" select="string()" />

   <xsl:variable 
        name="targetNode" 
        select="concat(name($listNode),'/',$listItem)" />


   <xsl:for-each select="$targetNode" >
       ... do the processing ....
   </xsl:for-each>

        
Any advice, greatly appreciated.


Adam.

=====
Adam Flegman - Senior Software Engineer

Mobile:     (0414) 375 735
Phone:     (07) 5547 8530
Facsimile: (07) 5547 8532
Email #1: adam_flegman@yahoo.com 
Email #2: planemad@optusnet.com.au

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: dynamic node selection

Posted by Adam Flegman <ad...@yahoo.com>.
> Well, if you have your xpath in $targetnode, wouldn't it simply be:
> 
> <xsl:apply-templates select="$targetnode"/>    ?
> 
> or
> 
> <xsl:for-each select="$targetnode">
>     <xsl:apply-templates/>
> </xsl:for-each>
> 
> To go along with your code.   I haven't done XSLT in a while, so I'm a 
> little rusty.
> 
Hi Tony,

The value of $targetNode is some arbitrary string value that (hopefully) looks
like an xml node path. At design time I dont know all the possible values that
the $targetNode string may have. 

I have tried to use the <xsl:for-each select="$targetNode"> approach but it
seems that the 'select' part doesnt like using a string parameter. I get the
following error report from cocoon:

----------------------------------------------------
Exception in creating Transform Handler 

More precisely:

org.apache.cocoon.ProcessingException: Exception in creating Transform Handler:
java.lang.NullPointerException
----------------------------------------------------


If I could find a way to take the string value of $targetNode and actually
select the corresponding nodes then I think the problem is solved.


Ad.

=====
Adam Flegman - Senior Software Engineer

Mobile:     (0414) 375 735
Phone:     (07) 5547 8530
Facsimile: (07) 5547 8532
Email #1: adam_flegman@yahoo.com 
Email #2: planemad@optusnet.com.au

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: dynamic node selection

Posted by Tony Collen <co...@umn.edu>.
Adam Flegman wrote:

>For example if parameters parentNode = "foo" and childNode = "bar", then
>$targetNode would evaluate to the string "foo/bar". I would want the 'for-each'
>statement to select all the "bar" nodes under the "foo" parent node.
>
>I think the only step I am missing is how to write a xpath node selection query
>that can take a string parameter as its argument.
>  
>

Well, if you have your xpath in $targetnode, wouldn't it simply be:

<xsl:apply-templates select="$targetnode"/>    ?

or

<xsl:for-each select="$targetnode">
    <xsl:apply-templates/>
</xsl:for-each>

To go along with your code.   I haven't done XSLT in a while, so I'm a 
little rusty.

Tony


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: dynamic node selection

Posted by Joerg Heinicke <jo...@gmx.de>.
Hello Adam,

> My apologies for the lack of clarity. I was trying to use the value I have
> calcualted for the $targetNode variable to select a node in my XML document.
> 
> If I had an xml source like the following, I would like to be able to
> 'dynamically' select nodes for processing.

...

> The xslt template to do the processing:
> 
> <xsl:template name="createOptionList">
>    <xsl:param name="parentNode" select="string()" />
>    <xsl:param name="childNode" select="string()" />
> 
>    <xsl:variable 
>         name="targetNode" 
>         select="concat(name($listNode),'/',$listItem)" />
> 
>    <xsl:for-each select="$targetNode" >
>        ... do the processing ....
>    </xsl:for-each>
> </xsl:template>

if you know the path as exact as above (i.e. step by step) you can 
simply do something like

<xsl:for-each select="*[name() = $listNode]/*[name() = $listItem]">

> For example if parameters parentNode = "foo" and childNode = "bar", then
> $targetNode would evaluate to the string "foo/bar". I would want the 'for-each'
> statement to select all the "bar" nodes under the "foo" parent node.
> 
> I think the only step I am missing is how to write a xpath node selection query
> that can take a string parameter as its argument.

If you want to evaluate more complex XPathes and can't find a work 
around the are processor specific extension functions like 
http://xml.apache.org/xalan-j/extensionslib.html#evaluate or a bit more 
common http://www.exslt.org/dyn/functions/evaluate/index.html.

Regards,

Joerg


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: dynamic node selection

Posted by Adam Flegman <ad...@yahoo.com>.
Hi Tony,

> ><xsl:template name="createOptionList">
> >   <xsl:param name="parentNode" select="string()" />
> >   <xsl:param name="childNode" select="string()" />
> >
> >   <xsl:variable 
> >        name="targetNode" 
> >        select="concat(name($listNode),'/',$listItem)" />
> >
> >
> >   <xsl:for-each select="$targetNode" >
> >       ... do the processing ....
> >   </xsl:for-each>
> >
> >        
> >Any advice, greatly appreciated.
> >  
> >
> 
> 
> 
> Adam,
> 
> I'm not too sure what you're trying to do. Are you trying to create an 
> element named with the value of $targetNode? Or are you trying to apply 
> your templates to the nodes named in the value of $targetNode?
> 
> If all you're trying to do is create a new named $targetNode, all you'd 
> have to do is:
> 
>  <xsl:element name="$targetNode"">foo</xsl:element>
> 
> So, again, I'm not to sure what you're trying to accomplish.. perhaps an 
> xslt list can help.
> 
> Tony
> 

My apologies for the lack of clarity. I was trying to use the value I have
calcualted for the $targetNode variable to select a node in my XML document.

If I had an xml source like the following, I would like to be able to
'dynamically' select nodes for processing.

<root>
   <foo>
      <bar />
      <bar />
      <bar />
   </foo>

   <dog>
      <eatcat />
      <eatcat />
      <eatcat />
   </dog>
</root>


The xslt template to do the processing:

<xsl:template name="createOptionList">
   <xsl:param name="parentNode" select="string()" />
   <xsl:param name="childNode" select="string()" />

   <xsl:variable 
        name="targetNode" 
        select="concat(name($listNode),'/',$listItem)" />

   <xsl:for-each select="$targetNode" >
       ... do the processing ....
   </xsl:for-each>
</xsl:template>

For example if parameters parentNode = "foo" and childNode = "bar", then
$targetNode would evaluate to the string "foo/bar". I would want the 'for-each'
statement to select all the "bar" nodes under the "foo" parent node.

I think the only step I am missing is how to write a xpath node selection query
that can take a string parameter as its argument.


Thanks for your time Tony.


Ad.


=====
Adam Flegman - Senior Software Engineer

Mobile:     (0414) 375 735
Phone:     (07) 5547 8530
Facsimile: (07) 5547 8532
Email #1: adam_flegman@yahoo.com 
Email #2: planemad@optusnet.com.au

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: dynamic node selection

Posted by Tony Collen <co...@umn.edu>.
Adam Flegman wrote:

>Hi Guys,
>
>I have been trying to re-factor some existing xslt code and wonder if it is
>possible to select nodes based on a string.
>
>The string is built up by parameters so the value of the target node is
>determined each time the xsl template is called. I am having trouble using the
>string to select the target node. In the sample below I can calculate the name
>of the target node I am after, but I am unsure how to convert the name into an
>actual node.
>
>
><xsl:template name="createOptionList">
>   <xsl:param name="parentNode" select="string()" />
>   <xsl:param name="childNode" select="string()" />
>
>   <xsl:variable 
>        name="targetNode" 
>        select="concat(name($listNode),'/',$listItem)" />
>
>
>   <xsl:for-each select="$targetNode" >
>       ... do the processing ....
>   </xsl:for-each>
>
>        
>Any advice, greatly appreciated.
>  
>



Adam,

I'm not too sure what you're trying to do. Are you trying to create an 
element named with the value of $targetNode? Or are you trying to apply 
your templates to the nodes named in the value of $targetNode?

If all you're trying to do is create a new named $targetNode, all you'd 
have to do is:

 <xsl:element name="$targetNode"">foo</xsl:element>

So, again, I'm not to sure what you're trying to accomplish.. perhaps an 
xslt list can help.

Tony


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: dynamic node selection

Posted by Alex Romayev <ro...@yahoo.com>.
Hi Adam,

I don't think select="$parentNode/ will work.  You
cannot use variables in place of element names.

Alex

--- Adam Flegman <ad...@yahoo.com> wrote:
> > Have you tried using name() function like:
> > 
> > <xsl:apply-templates
> >
>
select="/*[name()=$parentNode]/*[name()=$childNode]"/>
> > 
> > However, as far as I know name() function is there
> for
> > "emergency" cases, rather than normal usage.
> > 
> > If you know what the nodes might be, you might
> want to
> > use several stylesheets and pick the right
> stylesheet
> > dynamically.
> 
> Hi Alex,
> 
> In some cases I actually had the node corresponding
> to the parent and tried
> using the name() function to match the $childNode
> portion but it still didn't
> work.
> 
> select="$parentNode/*[name()=$childNode]"
> 
> ($parentNode is an actual node, $childNode was the
> name of the desired target
> node below the parent).
> 
> 
> I was hoping to use this approach to help
> cleanup/refactor some verbose xslt
> code I have inheirited. Its not really a critical
> problem as the existing code
> is functionally correct, I was just trying to tidy
> it up a bit.
> 
> 
> 
> Ad.
> 
> 
> =====
> Adam Flegman - Senior Software Engineer
> 
> Mobile:     (0414) 375 735
> Phone:     (07) 5547 8530
> Facsimile: (07) 5547 8532
> Email #1: adam_flegman@yahoo.com 
> Email #2: planemad@optusnet.com.au
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site
> design software
> http://sitebuilder.yahoo.com
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail:
> users-help@cocoon.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: dynamic node selection

Posted by Adam Flegman <ad...@yahoo.com>.
> Have you tried using name() function like:
> 
> <xsl:apply-templates
> select="/*[name()=$parentNode]/*[name()=$childNode]"/>
> 
> However, as far as I know name() function is there for
> "emergency" cases, rather than normal usage.
> 
> If you know what the nodes might be, you might want to
> use several stylesheets and pick the right stylesheet
> dynamically.

Hi Alex,

In some cases I actually had the node corresponding to the parent and tried
using the name() function to match the $childNode portion but it still didn't
work.

select="$parentNode/*[name()=$childNode]"

($parentNode is an actual node, $childNode was the name of the desired target
node below the parent).


I was hoping to use this approach to help cleanup/refactor some verbose xslt
code I have inheirited. Its not really a critical problem as the existing code
is functionally correct, I was just trying to tidy it up a bit.



Ad.


=====
Adam Flegman - Senior Software Engineer

Mobile:     (0414) 375 735
Phone:     (07) 5547 8530
Facsimile: (07) 5547 8532
Email #1: adam_flegman@yahoo.com 
Email #2: planemad@optusnet.com.au

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: dynamic node selection

Posted by Alex Romayev <ro...@yahoo.com>.
Hi Adam,

Have you tried using name() function like:

<xsl:apply-templates
select="/*[name()=$parentNode]/*[name()=$childNode]"/>

However, as far as I know name() function is there for
"emergency" cases, rather than normal usage.

If you know what the nodes might be, you might want to
use several stylesheets and pick the right stylesheet
dynamically.

Cheers,
-Alex


--- Adam Flegman <ad...@yahoo.com> wrote:
> Hi Guys,
> 
> I have been trying to re-factor some existing xslt
> code and wonder if it is
> possible to select nodes based on a string.
> 
> The string is built up by parameters so the value of
> the target node is
> determined each time the xsl template is called. I
> am having trouble using the
> string to select the target node. In the sample
> below I can calculate the name
> of the target node I am after, but I am unsure how
> to convert the name into an
> actual node.
> 
> 
> <xsl:template name="createOptionList">
>    <xsl:param name="parentNode" select="string()" />
>    <xsl:param name="childNode" select="string()" />
> 
>    <xsl:variable 
>         name="targetNode" 
>        
> select="concat(name($listNode),'/',$listItem)" />
> 
> 
>    <xsl:for-each select="$targetNode" >
>        ... do the processing ....
>    </xsl:for-each>
> 
>         
> Any advice, greatly appreciated.
> 
> 
> Adam.
> 
> =====
> Adam Flegman - Senior Software Engineer
> 
> Mobile:     (0414) 375 735
> Phone:     (07) 5547 8530
> Facsimile: (07) 5547 8532
> Email #1: adam_flegman@yahoo.com 
> Email #2: planemad@optusnet.com.au
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site
> design software
> http://sitebuilder.yahoo.com
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail:
> users-help@cocoon.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org