p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   select="node1", select="node2"... (http://p2p.wrox.com/showthread.php?t=10437)

Baldo March 12th, 2004 08:30 AM

select="node1", select="node2"...
 
I've got (XML):
Code:

<root>
   <node1> bla bla </node1>
   <node2> bla bla </node1>
   <node3> bla bla </node1>
   <node4> bla bla </node1>
   <node5> bla bla </node1>
</root>

I've got (XSLT):
Code:

<xsl:call-template name="FOO">
   <xsl:with-param name="number" select="1"/>
</xsl:call-template>

and the template:
Code:

<xsl:template name="FOO">
   <xsl:param name="number"/>
   <xsl:copy-of select="concat(node,$number)"/>
</xsl:template>

I wanna copy-of node1, node2, node3
but the concat(node,$number) is not the right way
(Can not convert #STRING to a NodeList!)

is there a solution?

xslt learner :)

pgtips March 12th, 2004 08:48 AM

I don't know of any way to dynamically create selection paths. If you had written your xml like this instead:
Code:

<root>
<node num="1"> bla bla </node>
<node num="2"> bla bla </node>
<node num="3"> bla bla </node>
<node num="4"> bla bla </node>
<node num="5"> bla bla </node>
</root>

then you could just use
Code:

<xsl:copy-of select="node[@num=$number]"/>

joefawcett March 12th, 2004 08:54 AM

Try:
Code:

<xsl:template name="FOO">
   <xsl:param name="number"/>
   <xsl:copy-of select="/root/*[name() = concat('node', $number)]"/>
</xsl:template>

or
Code:

<xsl:template name="FOO">
   <xsl:param name="number"/>
   <xsl:copy-of select="//*[name() = concat('node', $number)]"/>
</xsl:template>

if you don't the exact position of the nodes to find. If yur source document has namespaces you will need to change name() to local-name() or alter the XPath to include the namespace.


Joe (MVP - xml)

Baldo March 12th, 2004 09:39 AM

Quote:

quote:Originally posted by pgtips
 I don't know of any way to dynamically create selection paths. If you had written your xml like this instead:
tnx, but source XML was like I wrote (and it's a problem since it's not the classic <node id="3"/>

xslt learner :)

Baldo March 12th, 2004 09:49 AM

Quote:

quote:Originally posted by joefawcett
 Try:
Code:

<xsl:template name="FOO">
   <xsl:param name="number"/>
   <xsl:copy-of select="/root/*[name() = concat('node', $number)]"/>
</xsl:template>


GREAT. it worked!
what is the "*" ?

(try to find it in my wrox manual)

xslt learner :)

Baldo March 12th, 2004 09:50 AM

page 394 :)
NameTest

xslt learner :)

joefawcett March 12th, 2004 10:33 AM

Quote:

quote:GREAT. it worked!
what is the "*" ?
It is short for all nodes of the current type on the child axis, in this case the type is element so it's short for all child elements.

Joe (MVP - xml)

Baldo March 12th, 2004 10:38 AM

Quote:

quote:Originally posted by joefawcett
 
Quote:

quote:GREAT. it worked!
Quote:

what is the "*" ?
It is short for all nodes of the current type on the child axis, in this case the type is element so it's short for all child elements.
I'm so sorry. I already knew "*". It's an important "element" of xslt. :)


xslt learner :)


All times are GMT -4. The time now is 04:59 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.