Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 27th, 2006, 08:23 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default comma separated list of attributes

I want to convert a list of attributes to a comma-separated list

a variable $choiceNode contains a tree fragment:
<xsd:choice maxOccurs="unbounded">
  <xsd:element ref="referential"/>
  <xsd:element ref="temporal"/>
  <xsd:element ref="privacy"/>
</xsd:choice>

when i use the syntax:
<xsl:value-of select="string-join($choiceNode/node()/xsd:element/string(@ref),',')"/>
 the output is "referential,temporal,privacy"
but when i want to put this in a variable, the variable stays empty. How come???????
I use the syntax:
<xsl:variable name="choicelist" select="string-join($choiceNode/node()/xsd:element/string(@ref),',')"/>
or
<xsl:variable name="choicelist">
<xsl:value-of select="string-join($choiceNode/node()/xsd:element/string(@ref),',')"/></xsl:variable>
The debugger in oxygen displays the value but doesn't recognise it as a string

even this doesn't return a variable with a value:
<xsl:variable name="choiceList" select="string-join(('a','b'),'|')" />
when using
<xsl:value-of select="string-join(('a','b'),'|')"/> this returns "a,b" but I can't seem to fill the variable with this value
 
Old September 27th, 2006, 09:36 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think it's one of those cases where the problem is in the part of the code that you haven't shown us. Try to construct a complete but minimal working example that illustrates the problem.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 27th, 2006, 10:31 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it working by replacing it to another part where i use the variable.

the problem I did not know was that the variables created with xsl:variables last only inside the tag it is called even inside a xsl:if tag and when placed under the param tags and not using it right away, the variable is not set.

I did not add a line directly under the variable that did something with the variable.
It seems that if I put the xsl:variable inside a <xsl:if > tag, the variable only is present inside the <xsl:if></xsl:if> tags. I thought the local variables added under the param of a template would last everywhere inside the template tags (like in scripting languages)but it doesn't

This doesn't work:
<xsl:template name="rule">
    <xsl:param name="choiceNode" select="node()" />
    <xsl:variable name="choiceList" select="string-join($choiceNode/node()/xsd:element[1]/@ref,'|')" />
    <xsl:element name="sch:pattern">..using $choiceList here doesnt work choiceList is empty here...</xsl:element>
</xsl:template>

This does:
<xsl:template name="rule">
    <xsl:param name="choiceNode" select="node()" />
    <xsl:element name="sch:pattern">
    <xsl:if test="$choiceNode/node()">
        <xsl:variable name="choiceList" select="string-join($choiceNode/node()/xsd:element[1]/@ref,'|')" />
        <xsl:if test="replace(.,'{$choiceList}','x') = 'x'">
            found in list ...
        </xsl:if>
    </xsl:if>

</xsl:element>
</xsl:template>
 
Old September 27th, 2006, 11:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, that's the way XSLT defines the scope of variables.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate a list box from comma separated values CoderNH Access VBA 3 October 20th, 2008 08:37 AM
Comma Separated values aldwinenriquez SQL Server 2005 3 June 1st, 2007 08:52 PM
comma separated list MunishBhatia SQL Server 2000 11 March 21st, 2007 03:56 PM
Comma Separated Column Value thru T-SQL subhendude SQL Server 2000 2 December 12th, 2006 01:42 PM
selecting elements from comma separated list rjonk XSLT 4 September 29th, 2006 08:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.