Subject: comma separated list of attributes
Posted By: rjonk Post Date: 9/27/2006 8:23:45 AM
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
Reply By: mhkay Reply Date: 9/27/2006 9:36:16 AM
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
Reply By: rjonk Reply Date: 9/27/2006 10:31:59 AM
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>
<!-- here the variable doesn't exist anymore -->
</xsl:element>
</xsl:template>
    
Reply By: mhkay Reply Date: 9/27/2006 11:52:54 AM
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

Go to topic 12852

Return to index page 163
Return to index page 162
Return to index page 161
Return to index page 160
Return to index page 159
Return to index page 158
Return to index page 157
Return to index page 156
Return to index page 155
Return to index page 154